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

794 points
533f3b3952f863e39b00025f_316073419
Submitted by
Alexander Leith-Ross
almost 3 years ago

Successfully passed, No tweets!

I managed to pass onto the next question with my code but don't seem to have received any Tweets from the timeline to read.

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

baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/user_timeline.json"
query   = URI.encode_www_form(
    "screen_name" => "twitterapi",
    "count" => 10,
)
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

def print_timeline(tweets)
  tweets.each do |tweet| print_timeline

end

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

consumer_key ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""

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

tweets = nil
if response.code == '200' then
  tweets = JSON.parse(response.body)
  print_timeline(tweets)
end
nil

Taking a quick look at what I entered:

baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/user_timeline.json"
query   = URI.encode_www_form(
    "screen_name" => "twitterapi",
    "count" => 10,

Which seems to have been fine however the next bit is what I think is causing me the problem:

def print_timeline(tweets)
  tweets.each do |tweet| print_timeline

end

Which then displays my green tick as well as this message in the console:

(ruby):47: syntax error, unexpected $end, expecting keyword_end

Thank you very much in advance to whoever can help me out with this, greatly appreciated.


2 votes

permalink

Yep - problem is in your method. Your block isn't closed, it should be like this:

tweets.each do |tweet|
  print_timeline
end

But your block is wrong too. Look what similar method returns:

irb(main):102:0> def my_method(arr)
irb(main):103:1>   arr.each { my_method }
irb(main):104:1> end
=> nil
irb(main):105:0> my_method([1,2])
ArgumentError: wrong number of arguments (0 for 1)

You call to print_timeline method without passing any argument.
Actually you need something like this:

tweets.each { |tweet| puts tweet['text'] }

We've got array of tweets passed to print_timeline method and then we just iterate through each of them.

715 points
Picture
Submitted by
Nikolai Volochaev
almost 3 years ago

1 Comment

Picture Liz Pullen almost 3 years ago

This resolved the problem for me. Thanks!