Parsing responses for the Reading a Tweet section


#1

Hi all,

I posted the following issue (http://discuss.codecademy.com/t/parsing-responses-for-the-reading-a-tweet-section/) in the twitter api section, and a helpful moderator who looked at it and received the same error suggested I post it here as well to get your guys' input. My post is copied below- I'd love to get anyone's advice. Thanks a lot!

Original post:

I've been having some problems retrieving the appropriate info in the Reading a Tweet section. I get the following error: Oops, try again You did not generate the correct output. Did you print the username, followed by ' - ', followed by the Tweet text?

However, In my console, I can see that I am properly spitting out the username and the tweet by using the precise parameters spelled out in the hint section, but I notice that the "[name]" parameter within the JSON is the user's name, but listed upside down. Likewise, it spits this out upside down too, so I'm wondering if that may be part of the problem. Is there some method we're supposed to run on it to turn it rightside up, perhaps?

I'm attaching my code below in case there's something wrong therein. Thanks a lot!

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 "<screen name> - <text>" FORMAT
  puts JSON.pretty_generate(tweet)
  screen_name = tweet["user"]["name"]
  tweet_text = tweet["text"]
  print "#{screen_name} - #{tweet_text}"
end

# 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 "ENTER IN EXERCISE 1", ""
access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""

# 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

#2

https://www.codecademy.com/en/courses/ruby-intermediate-en-rUwFe/0/3#

Confirmed issue dating back many months (or years).

https://www.codecademy.com/forums/ruby-intermediate-en-rUwFe/0/exercises/2


#3

The fix didn't work for me. Kinda lame.