0
votes

I've been messing around with the twitter rails API and I'm having trouble getting a tweet's location. This always seems to fail and not return anything. I've also tried doing tweet.coordinates but it doesn't seem to work.

tweets = client.user_timeline("gems")
puts "size : #{tweets.size}"  
tweets.each do |tweet|
    if(tweet.place)
       puts tweet.place.attributes
    end
end

I'm using the following twitter gem. https://github.com/sferik/twitter

EDIT:

tweets = client.search("a", geocode: "40.7128,-74.0059,50mi").take(100)
tweets.each do |tweet|
    if tweet.place?
        puts tweet.place.full_name
     elsif tweet.geo?
        puts "geofound"
    elsif tweet.user.location?
        puts tweet.user.location
     end

So I tried the above code looking for tweets that have geocodes, but it seems that none of them have a place or geo field and it always returns the tweet.user.location, but that's not very accurate. The output just has a lot of New York's, but also a bunch from other cities as well so I don't really know how Twitter got those queries when the other cities don't exist/are far away from NY. Am I missing another location field? I also noted that the number of outputs doesn't equal the size of the tweet array.

https://pastebin.com/eW18Ri2S

Here's an example output

1
What is your output? Does it show the size correctly? You can try puts tweet.inspect on the first line of the loop body to see if the tweet is present. - mahemoff
the size is 20. I've tried outputting the text and they're all fine. - K Zeng
Do the tweets actually include geo metadata? Not all tweets do - need to choose the right account. Even in Twitter's example output, place is null dev.twitter.com/rest/reference/get/statuses/user_timeline - mahemoff
@KZeng can you do tweet.inspect and paste the result along with the question? - Pramod
Yea I noticed that not all of them had the correct metadata so I tried searching for things with a geocode option but still no luck. Are tweets with the geo metadata rare? - K Zeng

1 Answers

0
votes

As mentioned in the twitter gem documentation, you should user place? or geo? methods.

Null Objects

In version 4, methods you would expect to return a Twitter object would return nil if that object was missing. This may have resulted in a NoMethodError. To prevent such errors, you may have introduced checks for the truthiness of the response, for example:

status = client.status(55709764298092545)
if status.place   
  # Do something with the Twitter::Place object
elsif status.geo   
  # Do something with the Twitter::Geo object
end

In version 5, all such methods will return a Twitter::NullObject instead of nil. This should prevent NoMethodError but may result in unexpected behavior if you have truthiness checks in place, since everything is truthy in Ruby except false and nil. For these cases, there are now predicate methods:

 status = client.status(55709764298092545)
 if status.place?
   # Do something with the Twitter::Place object
 elsif status.geo?
   # Do something with the Twitter::Geo object
 end