This course will become read-only in the near future. Tell us at if that is a problem.

Patience, Young Padawan

Time to assemble and run your code

Guest what? We now have all the pieces we need to assemble the code into a single script. 

Before we get started, some debugging wisdom: In the instructions below we'll be building up a script. Add new code slowly. Do each line one or two at a time, running the script at lots of intermediate stages to make sure it works and catch bugs quickly. Printing out intermediate values as you go can also be a helpful tool. 

If you haven't already, you're now going to put your code into script file, which is just a plain text file that contains lines of code. There's nothing special about this file, and the code yo uwere writing before, if you were using an interpreter, can be pretty much copied over to the script file. 

Running your Python script from the command line looks like this:

$ python

And running your Ruby script from the command line looks very similar:

$ ruby myscript.rb

The first part of this script is to start with the code you wrote in task 5 to make the API call. 

The output of that section was the json_string variable. Using the code you wrote in task 4, take the json_string and convert it to a native data object. Check your result, for example by using a print statement, to make sure it runs before continuing. For example, let's say you stored the output of the json function call in a variable called data.  If you print out that variable (print in Python; puts in Ruby), it should look something like this (although it likely won't be formatted nearly so nicely):









results: [


created_atFri, 27 Jan 2012 17:04:18 +0000




from_user_nameMaira Willets



The example above is just the beginning of the result you will see from a search query!

Now take that data object and extract the 'results' item. Remember, it's the data variable is a dictionary/hash, and "results" is a key in that dictionary. The value associated with the "results" key, is a list of, you guessed it, information about tweets that contain your search term. Let us assume you stored the results in a variable called results.

As you can see from the JSON above, each item in the results list is itself a dictionary, containing lots of interesting information, or metadata, about each tweet, plus the tweet itself.

How do we know what fields, or keys, the dictionary contains? A few places we can look: the documentation for Twitter's search API gives us quite a bit of information as a starting point. Another way is to print out an entire result, and look at the keys that are present.

Before moving on, print out the first item in the results list (Remember the index for accessing the first item in a list is 0). Pretty messy eh? What if you just wanted to see a list of the dictionary keys? Try out these methods:

in python: print results[0].keys() 

in ruby: puts results[0].keys

Question: how many keys are in each result? What function or method with tell you the answer? (There are a couple of different ways to do this, each fairly straightforward. Google for the answer if you don't know, and check out examples of how others have done it). 

Ok, now we're ready to process the result. We want to iterate over each item in the results list using... a for-loop! How convenient-- just like you did in task 3. Go ahead and write the first line of the for loop. Remember self-documenting code, and choose your loop variable so that the code is easy to read. 

Inside the body of the for-loop, for each result, pull out some specific values of iterest. Rememeber, because each "result" is a dictionary, we have to use the keys to retrieve a specific value. For example, if I was interested in the username of the person sending the tweet, I would access that item with the code:


The easiest way to work with these values is by storing them in variables, e.g.:

from_user = result['from_user']

Inside your for-loop, do this for however many other items interest you, one on each line. For each item, save it in a variable, and start off by just printing out the variables inside the for-loop. 

print from_user (python)

puts from_user (ruby)

Doing great-- and we're almost done! You've processed the API call response and extracted the information that is of interest to you. But instead of printing out raw data objects, let's make a nice sentence that is more appropriate for human consumption. We'll do this by using string formatting. Both Ruby and Python have ways to construct format strings. A format string is a string with variables dynamically inserted into the string at runtime. Here is an example in both languages:


name = "Superman"

occupation = "superhero"

print "my name is %s and my occupation is %s" % (name, occupation)

output: "my name is Superman and my occupation is superhero."



name = "Cat woman"

occupation = "supervillain"

puts "my name is #{name} and my occupation is #{occupation}."

output: "my name is Cat Woman and my occupation is supervillain."


Feel free to experiment with different formatting. One you have a sentence you're happy with, submit an example in the comments. Hooray! You're script is working! Just a little bit of tidying up to do...

Task Discussion