Almost "Go" time... construct your query request
Back in Task 2, you were channeling your vision of a beautiful query that would return interesting information. Part of that task was to identify the Twitter API call used to issue a search. In this task we're going to construct your API call, and make sure you have the right language-specific tools to talk with the Twitter API servers.
The search API call has a number of optional parameters, but only one required parameter. What is that parameter?
Run a test search with that single parameter in your browser.
Feel free to add in some of the optional parameters of this API call to your query. It's up to you! If there are challenges or tricks you run into, post them to the comments. What happens if you want to use a search term with a space in it, do you need to do anything special? (Try it!)
Now we're going to set up the function call necessary to make your API request "programmatically"-- ie, in your code. This can be a bit confusing at first and there are often several ways one can do this, so for the purposes of this challenge, we are providing suggestions for which libraries to use. You can always explore other approaches (please share them below if you do!) but learning the common approach is a great way to get started.
For both examples below, you'll need to define the api_call variable. api_call should contain the API call you constructed above (the same one you tested out in the browser). (Hint: it's just a string).
For Python, you'll be using the urllib2 python module. Don't forget to import it, by typing
at the top of your script, with your other imports. To issue the API call, use urlopen function (documented here). Note that urlopen returns a "file-like object." If you're unfamiliar with file input/output in Python, you can read more about it here. As a result, to get the contents of the response, we can simply call read() on that response, as follows:
resp = urllib2.urlopen(api_call)
json_string = resp.read()
For Ruby, we'll make use of the 'net/http' library. Put your require statements at the top of your script:
Now to issue the API call, we'll use the following two function calls. The first function, URI(), parses the url into a format that the following function, get(), can understand.
uri = URI(api_call)
json_string = Net::HTTP.get(uri)
In the examples above we stored the return value of the read() or get() functions in a variable called json_string. Hmm... why might that be? Of course, we could have called it whatever we wanted. But using informative variable names helps when reading the code. People sometimes refer to this as "self-documenting code." Print our your json_string variable. Does it look like what you expected?
What does it mean for something to be a "file-like object"?
Optional: Reading the documentation for urllib2 or net/http, can you extend you code above to check for success or error before attempting to read the HTTP response? This might be a little bit challenging, and there are lots of correct ways to do it. Feel free to ask questions and post the methods you used below. What kinds of things can happen when you don't check for errors?
Are there other places in your code that you could improve it's "self-documenting" nature? Post your improvements, if any - make sure to document what the old variable name was and the new, improved one.