0

In my ruby code I am running into a "cannot convert string to Integer" error. The code is:

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

# Now you will fetch /1.1/statuses/user_timeline.json,
# returns a list of public Tweets from the specified
# account.


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

# Print data about a list of Tweets
def print_timeline(tweets)
  tweets.each do |tweet|
  require 'date'
   d = DateTime.parse(tweet['created_at'])
    puts " #{tweet['text'].delete ","} , #{d.strftime('%d.%m.%y')} , #{tweet['user']   ['name']}, #{tweet['id']}"
  end
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(
    "")
access_token = OAuth::Token.new(
    "")

# 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
tweets = nil

puts "Text,Date,Name,id"
if response.code == '200' then
  tweets = JSON.parse(response.body)
  print_tweets(tweets)
end
nil

After debugging, I believe the error is coming from lines 19 or 20 (d = and puts statement). I am not sure what String is attempting to be converted to an integer.

2
  • What's the value of tweet at that point? Commented Jun 18, 2013 at 15:01
  • Tweet is a local variable that is a string in the loop. Commented Jun 18, 2013 at 15:11

1 Answer 1

1

It looks like you're trying to access tweet['created_at'] whereas tweet is not a Hash structure. You should try to inspect the structure of tweet ( and maybe of tweets as well ).

You can look at the class with tweet.class or directly inspect it : tweet.inspect

I'd guess tweet is an array, and its elements should therefore be called using an integer, not a string

Sign up to request clarification or add additional context in comments.

7 Comments

I am new to Ruby and writing on CodeAcademy so it is tough to debug. If tweet is an array, which would make sense due to all the information being stored, would there be away to change its elements to strings? Or another way to fix this issue?
Can you tell us what tweet.inspect yields ? It would be easier to answer your question more precisely
in the loop when i print tweet.inspect it returns(only part of return): ["statuses", [{"metadata"=>{"result_type"=>"recent", "iso_language_code"=>"en"}, "created_at"=>"Tue Jun 18 15:51:10 +0000 2013", "id"=>347018659639726082, "id_str"=>"347018659639726082", "text"=>"…then Obama came for the Downloaders t.co/IoBrQI37zQ @datechguy Hit the tip jar, if you're able."
well in this case you want to access tweet[1][0]['created_at'] or equivalently, tweet[1].first['created_at'] . tweet[1].first is supposed to access the Hash structure that you are trying to read. Hope this helps
careful, you'll also have to change tweet['text'], tweet['user']...etc using the same logic
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.