Emergent Web Technologies Spring 2009 Class 8

esse quam videri
Revision as of 21:10, 18 March 2009 by Matthew.ephraim (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Back to EWT Spring 2009


This week, I want to give you a quick introduction to HTTP and then show you how a RESTful architecture takes advantage of HTTP to simplify the sharing of resources across the Web.

Activity 1

Make sure that you have the Firebug and Live Headers Firefox Add-ons installed. You can get them here and here.

Go to http://twitter.com and open up the Firebug Console by clicking on the little Firebug in the lower right corner of the browser.

Click on the checkbox next to Net and click the button that says "Apply settings for twitter.com".

Go to the Net tab and reload the page.

You should see a list of requests that were made to http://twitter.com, http://assets0-3.twitter.com and to http://google-analytics.com.

Expand one of the requests to see what the request looked like. Switch between the Headers and the Response tabs to see what the request and response headers and the entity body of the response looked like.

You can also open up the Live Headers add-on to see the requests as they take place.

Try the same thing on some other pages and see what the requests and responses look like.

Activity 2

We're going to create a really simple Twitter client using their RESTful API. You can find the documentation for the Twitter REST API here.

Open up InstantRails and open a new Terminal. Create a new file called "twitter.rb" using a text editor. Make sure that you can find the file using the Ruby console by typing "ruby twitter.rb".

Add the following lines to the very top of twitter.rb

require 'net/http'
require 'rexml/document'

The Net::HTTP library makes it easy to connect to remote server and send HTTP requests to it. REXML is a library for parsing XML. We'll use it to parse the responses from Twitter.

Next we'll query Twitter for the latest statuses from a user. First, we'll check to make sure we can get a response from Twitter. Add the following line to your twitter.rb.

Net::HTTP.get_print 'www.twitter.com', '/statuses/user_timeline/MCHammer.xml'

Run your Ruby file and you will see some XML scroll by representing the response from Twitter. If everything is working correctly you will see the latest status messages from MCHammer's twitter feed.

Let's try something a little more complicated. Replace everything except for the first two lines of the file with the following:

Net::HTTP.start('www.twitter.com') do |http|
       response = http.head('/statuses/user_timeline/MCHammer2.xml')
       puts response.class
       puts response['content-type']

Run the code again. You should see the following:

application/xml; charset=utf-8

The Net:HTTP.start command opens up a connection to http://twitter.com. One the connection is open, you can use the http variable to make requests to the site. The two lines that were printed show a response that returned "OK" and the content type of the response from twitter.

Now, change the path to


Run the script again and you should see the following:

application/xml; charset=utf-8

This time, the server returned a response code of "Not Found" because there is no user MCHammer2.

Now, replace everything but the first two lines with the follow:

Net::HTTP.start('www.twitter.com') do |http|
     puts http.get('/statuses/user_timeline/MCHammer.json').body

This time, when you run the code, you should see the JSON content for the user's Twitter feed go scrolling by. Change the path to '/statuses/user_timeline/MCHammer.xml' and you'll see the XML for the feed go by.

Finally, let's use the REXML to just get the latest message and print them out. Replace everything except for the first two lines with this function for printing user messages:

def print_user_messages(user_name)
    Net::HTTP.start('www.twitter.com') do |http|
        xml = REXML::Document.new http.get("/statuses/user_timeline/#{user_name}.xml").body
        xml.elements.each('//text') { |post| puts post.text }


You should be able to run your script and see all of the latest messages from MCHammer printed out. Change the user name to another user and you'll that user's messages printed out.