Twitter Client in Shoes


Prerequisites

  1. I hope you have Shoes installed on your machine, operating system does not matter :)
  2. oauth gem (gem install oauth will do the trick or sudo if you have to!)
  3. json gem as well(to parse the response) (gem install json)

Let’s start

  1. Register your application at Twitter, give some cool name like “Rubydubee”(not this x-( ).
  2. Lets first write our own library (don’t worry you can just copy the code for now :) ), But you can try to understand that… very small & simple if you know the OAuth protocol.(the oauth gem does all the magic.)
  3. Let’s write our Application
#Main file
 
require "Twitter.rb"
require 'json'
require 'uri'
 
Shoes.app :width=>300, :height=>400, :title=>"Rubydubee" do
  @twitter = Twitter.new "consumer_key",
                   "consumer_secret"
  @main_stack = stack do
  caption "Rubydubee : Update your status"
  image "twitter.png", :click => "#{@twitter.get_request_token.authorize_url}"
  inscription "Sign with Twitter first and then Authrize your PIN."
  button "Authorize" do
   pin = ask("Please allow access and enter the PIN : ")
   @access_token = @twitter.get_access_token(pin.chomp)
   data = @access_token.get("/account/verify_credentials.json").body
   debug("getting some data ")
   result = JSON.parse(data)
   username = result['name']
   @main_stack.append do
    caption "Howdy #{username}!"  
    edit_box :width=> 250, :height=>100 do |e|
     @count.text = e.text.size
     @str = e.text
    end
    @count = strong("0")
    para @count," characters"
    button "Tweet" do
     new_status = URI.escape(@str, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
     @access_token.post("/statuses/update.json?status=#{new_status}").body
     alert("Status Updated!")
    end
   end
  end
  end
end

The code is intuitive! isn’t it? if you need any help plz comment! After you run this “shoes main.rb” it will look somewhat like this :

UI

Tweet

Try adding error handling, better design and UI, and of course more features.

Keep reading!!! :)