Parsing responses for the Reading a Tweet section

ruby
api

#1

Hi all,

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!

#indent preformatted text by 4 spaces`
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

Trouble printing tweet in correct format
#2

I have reproduced this error issue, and have nothing to offer.

Oops, try again.
You did not generate the correct output. Did you print the username, followed by ' - ', followed by the Tweet text?

Will do some more digging. It might be a good idea to present this issue in the Platform category of the CC Community Lounge.

Update: Tried every workaround I could dig up in the Archive and still nothing doing. Will have to hand this one over the Platform folks.


#3

I can help. I looked at the tweet on twitter.com; one person replied back in May that the user's decision to display his name upside-down was breaking the Codecademy exercise (https://twitter.com/jcoste/status/600769001191567360)

This got me thinking that the code-tester has hard-coded the username, rather than making its own API call to the Twitter server. So I did this:

def print_tweet(tweet)
  ...
  tweet["user"]["name"] = "Raffi Krikorian"
  ...
end

This lets the code produce the "correct" output and let me proceed to the next exercise; it also confirms my suspicions that the system is expecting a hard-coded name. :smile:

Hope that helps!


Reading a tweet - getting rid of 'nil'
#4

Your solution didn't work for me immediately, but does work when this line is removed from the print_tweet function:

    puts JSON.pretty_generate(tweet)

Not sure, now, where I got that line from, unless it was from the OP.


#5

Yes, that line was suggested in the instructions, for us to see the full content of a tweet message. And yes, mine also didn't work until I took that line out. :+1:


#6

This solution fixed it for me, thanks for posting it.

I'd suggest changing this exercise to accepting tweet["user"]["screen_name"], since that can't be flipped (afaik), and the following comment in the code suggest that screen name is to be used there anyway:

# ADD CODE TO PRINT THE TWEET IN "<screen name> - <text>" FORMAT


#7

Thanks to all for your insightful responses, unfortunately none still work for me.
If anyone managed to make this exercise work could you please share your code?
Thanks!


#8
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
  screen_name = tweet["user"]["name"] = "Raffi Krikorian"
  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

#9

Thank you so much for that response! Hopefully this part of the tutorial can be updated.


#10

Hi,
Just hardcode the userNmae
tweet["user"]["name"] = "Raffi Krikorian"