Infinity
Infinity

Reputation: 11

Twitter gem - how to get user's country?

I know user's name in Twitter. How to get user's country using Twitter gem?

Upvotes: 1

Views: 1388

Answers (2)

Stefan
Stefan

Reputation: 114218

It's the very first usage example:

Twitter.user("sferik").location

However, location is just a string. The user's country isn't stored explicitly.

Upvotes: 3

Thronk
Thronk

Reputation: 655

The user object holds country information

    "user":{"statuses_count":3080, "favourites_count":22, "protected":false,
 "profile_text_color":"437792", "profile_image_url":"...", "name":"Twitter API", 
"profile_sidebar_fill_color":"a9d9f1", "listed_count":9252, "following":true, 
"profile_background_tile":false, "utc_offset":-28800, "description":"The Real Twitter API. 
I tweet about API changes, service issues and happily answer questions about Twitter and 
our API. Don't get an answer? It's on my website.", "location":"San Francisco, CA", 
"contributors_enabled":true, "verified":true, "profile_link_color":"0094C2", 
"followers_count":665829, "url":"http:\/\/dev.twitter.com", "default_profile":false, 
"profile_sidebar_border_color":"0094C2", "screen_name":"twitterapi", 
"default_profile_image":false, "notifications":false, "display_url":null, 
"show_all_inline_media":false, "geo_enabled":true, "profile_use_background_image":true, 
"friends_count":32, "id_str":"6253282", "entities":{"hashtags":[], "urls":[], 
"user_mentions":[]}, "expanded_url":null, "is_translator":false, "lang":"en", 
"time_zone":"Pacific Time (US & Canada)", "created_at":"Wed May 23 06:01:13 +0000 
2007", "profile_background_color":"e8f2f7", "id":6253282, "follow_request_sent":false, 
"profile_background_image_url_https":"...", "profile_background_image_url":"...", 
"profile_image_url_https":"..."}

It can also be determined by the Coordinates object - if you care to use a mapping service to try and determine where the latitude and longitude lines place the tweet in terms of country or you could delineate the corners of a country to try and get a general idea. For example, you could use

Northernmost point Northwest Angle, Minnesota (49°23'4.1" N)

Southernmost point Ballast Key, Florida ( 24°31′15″ N)

Easternmost point Sail Rock, just offshore of West Quoddy Head, Maine (66°57' W)

Westernmost point Bodelteh Islands offshore from Cape Alava, Washington (124°46' W)

as the contiguous 48 US States as both the places object and the location of the users object are frequently null or not specified in practice:

"coordinates":
{
    "coordinates":
    [
        -75.14310264,
        40.05701649
    ],
    "type":"Point"
}

or lastly by the places object

"place":
{
    "attributes":{},
     "bounding_box":
    {
        "coordinates":
        [[
                [-77.119759,38.791645],
                [-76.909393,38.791645],
                [-76.909393,38.995548],
                [-77.119759,38.995548]
        ]],
        "type":"Polygon"
    },
     "country":"United States",
     "country_code":"US",
     "full_name":"Washington, DC",
     "id":"01fbe706f872cb32",
     "name":"Washington",
     "place_type":"city",
     "url": "http://api.twitter.com/1/geo/id/01fbe706f872cb32.json"
}

Upvotes: 1

Related Questions