3. Reading a Tweet


#1

Link:
https://www.codecademy.com/en/courses/ruby-intermediate-en-rUwFe/0/3?curriculum_id=5122d5f811fbdb5456005922#

Error Message:
Oops, try again.

My Code:
require 'rubygems'
require 'oauth'
require 'json'

Now you will fetch /1.1/statuses/show.json, which

takes an 'id' parameter and returns the

representation of a single Tweet.

baseurl = "https://api.twitter.com"
path = "/1.1/statuses/show.json"
query = URI.encode_www_form("id" => "266270116780576768")
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

Print data about a Tweet

def print_tweet(tweet)
# ADD CODE TO PRINT THE TWEET IN " - " FORMAT
puts JSON.pretty_generate(tweet["user"]["name"], tweet["text"])

Set up HTTP.

http = Net::HTTP.new address.host, address.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

If you entered your credentials in the first

exercise, no need to enter them again here. The

||= operator will only assign these values if

they are not already set.

consumer_key ||= OAuth::Consumer.new " HmorkXjzzWVFkZ5MBDYpjdRmy", "qeOPbYIHBMUaT4dSQSJh1miYOyzA69pZgrVfP9bS1LSuz1XGOE"
access_token ||= OAuth::Token.new "3087246671-WbswPB20WR7NH5kUgXmuptYDpmGHpEtKXMijIZZ", "ALR87xzCh9dDFTm1F64Napz5LNd0IXMjvN9yy8gJmn1Db"

Issue the request.

request.oauth! http, consumer_key, access_token
http.start
response = http.request request

Parse and print the Tweet if the response code was 200

tweet = nil
if response.code == '200' then
tweet = JSON.parse(response.body)
print_tweet(tweet)
end