Re.Mark

My Life As A Blog

Ruby Tuesday #11 : First Steps with Shoes

leave a comment »

Last week, I decided that Shoes was the toolkit I’d use to build a GUI for the Twitter client I’ve built in Ruby. So, I downloaded Shoes and installed it on a machine running Vista. There’s a bunch of samples that come with Shoes – to run the Timer, for example, type Shoes samples\timer.rb.

Before I got any further, I checked my feedreader and noticed that Scott Hanselman had blogged about Shoes – and said that it’d be a grat idea to build a Twitter client in Shoes. Timing is everything. Let’s put our shoes on.

I like to start simple. In fact I pretty much like to end simple, too. Anyway, I thought I’d start out with a button that downloads the public timeline when you press it. Here’s the code:

require 'Twitter'

translator = Translator.new
client = Client.new 

Shoes.app :height => 150, :width => 250 do
   background rgb(240, 250, 208 )
   stack :margin => 10 do
     button "Get Public Timeline" do
       timeline = ""
       translator.xml_to_tweets(client.download_public_timeline).each do |tweet|
         timeline << "#{tweet.user.screen_name} says #{tweet.text\n"
       end
       @label.replace timeline
     end
     @label = para ""
   end
end

OK, a couple of confessions. The first is that it looks pretty awful. The second is that on my machine the require was causing an exception in Shoes, so I replaced it with the code from Twitter.rb. I’ve shown the code here with require because it should work and it’s much simpler to read.

Let’s see if we can tidy that up a bit. There’s a couple of issues. There’s no scrollbar for the tweets and all the tweets run into each other. By putting stacks inside a containing stack, we should be able to sort that out. Here goes:

require 'Twitter'

translator = Translator.new
client = Client.new

Shoes.app :title => "Twitter Shoes", :height => 750, :width => 750 do
   background rgb(240, 250, 208 )   stack :width => 700, :margin => 20 do
     background "#eee", :radius => 12
     border "#00D0FF", :strokewidth => 3, :radius => 12
     stack :margin => 20 do
       button "Get Public Timeline" do
         timeline = ""
         translator.xml_to_tweets(client.download_public_timeline).each do |tweet|
           timeline << "#{tweet.user.screen_name} says #{tweet.text\n\n"
         end
         @label.replace timeline
       end
     end
     stack :height => 650, :margin => 20 do
        background "#eee"
        @label = para ""
     end
   end
end

I borrowed the colours and border from the jot example in the samples that come with Shoes. I think that looks much better. What would be even better is if it downloaded your friends timeline (i.e. you and those you follow.) I put some text boxes (edit_box) in to enter the username and password and even figured out how to mask the password (:secret => true) but started to have some issues with Shoes crashing. So, next week’s task will be to see if I can overcome the crashing and get a more functional client.

About these ads

Written by remark

June 24, 2008 at 8:10 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: