This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Introduction to Twitter's API Forum

View Course » View Exercise

259 points
Picture
Submitted by
Eric Butterfield
about 4 years ago

Lost

I'm really lost here. What is it that I am supposed to require and what am i parsing and where? The hint didn't really help me at all here.


36 votes

permalink

require 'rubygems'
require 'oauth'
require 'json'

def parseuserresponse(response)
user = response.body

# Check for a successful request
if response.code == '200'
# Parse the response body, which is in JSON format.
# ADD CODE TO PARSE THE RESPONSE BODY HERE
user = JSON.parse(user)

# Pretty-print the user object to see what data is available.
puts "Hello, #{user["screen_name"]}!"

else
# There was an error issuing the request.
puts "Expected a response of 200 but got #{response.code} instead"
end

user
end

baseurl = "https://api.twitter.com"

address = URI("#{baseurl}/1.1/account/verify_credentials.json")

http = Net::HTTP.new address.host, address.port
http.usessl = true
http.verify
mode = OpenSSL::SSL::VERIFY_PEER

consumerkey ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
access
token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""

request = Net::HTTP::Get.new address.requesturi
request.oauth! http, consumer
key, accesstoken
http.start
response = http.request(request)
user = parse
user_response(response)

1333 points
51245087cc380b4567000b63_511518481
Submitted by
Patrick Beatty
about 4 years ago

5 Comments

51245087cc380b4567000b63_511518481 Patrick Beatty about 4 years ago

Went through for me with this setup. I only removed some of the commenting in the coe because it was showing up as extra-large font in my response to your question.

Picture Chad Chabot about 4 years ago

I'll upvote only because it's the correct answer, but would downvote for not explaining anything.

4f0b2b9085a9d7000101d413_711447490 Max West about 4 years ago

Agree with Chad. Due to a complete lack of explanation of either the exercise or the hint, this exercise is useless for actually learning anything.

513a55cdc8aac3bd4500106b_949505497 dpen about 4 years ago

Response is an HTTP response, so you need to dig inside it to get to the body which holds the info you want. You do that with response.body.
This then returns a hash of a bunch of twitter info some of which is the Text of the tweet, some of it is user info. That's why user["screenname"} shows the user's name, "screenname" is the hash's key poitning to screen_name. To see what's coming back as the response body, do a puts user.

In fact, returned hashes can multilevel, you will see this in the next exercise where you'll need to print out tweet["user"]["name"], where user is a key to a second hash within the big hash and name is a key within that inner hash that points to the user's name...Once again you can add a puts tweet within your code to see the response.body. Make sense, or TMI?

Daeee9c48df121407cfceea9cffd2c0d?s=140&d=retro Philipp Tautz about 4 years ago

Once you see the solution it is rather easy to figure it out, but getting there is impossible. Until seeing the solution I was just lost.


12 votes

permalink

Thanks for the help but this exercise needs to redone. Talk about throwing you in at the deep end! I'm all for fast learning but there was no background at all to this exercise.

314 points
Picture
Submitted by
Dom Eden
about 4 years ago


2 votes

permalink

This will also work:

def parseuserresponse(response)
user = nil

Check for a successful request

if response.code == '200'

Parse the response body, which is in JSON format.

ADD CODE TO PARSE THE RESPONSE BODY HERE

user = JSON.parse(response.body)

1214 points
513a55cdc8aac3bd4500106b_949505497
Submitted by
dpen
about 4 years ago

1 Comment

E6d861591d76a151ea5044c81b6a2ba2?s=140&d=retro cssAce75402 almost 4 years ago

Getting rid of the user=nil and simply adding user = JSON.parse(response.body) is much more Rubyist. Thanks for the help!


0 votes

permalink

Cheers, Patrick.

446 points
Picture
Submitted by
Ian Jamieson
about 4 years ago

1 Comment

51245087cc380b4567000b63_511518481 Patrick Beatty about 4 years ago

Thanks :)


0 votes

permalink

wow I really wouldn't have done that without this forum, think is presumes to much and doesn't hint at MOST of the things the user needs to add/modify

686 points
502a52e9978c7d0002026d19_212517732
Submitted by
Ben Byford
about 4 years ago


0 votes

permalink

how are you supposed to know that the response variable (as discussed in the hint) is the user variable?

1231 points
Ada24db96304f7da4cb9fa55e088dc4a?s=140&d=retro
Submitted by
coe8888
about 4 years ago


0 votes

permalink

I was follow by Patrick's instructions but seems something does wrong, get error message: Expected a response of 200 but got 401 instead
"{\"errors\":[{\"message\":\"Invalid or expired token\",\"code\":89}]}". :(.

1399 points
521c8d1780ff33fc8400127b_599939997
Submitted by
galakti0n
about 4 years ago