Skip to content

Instantly share code, notes, and snippets.

@iwoodruff
Created January 31, 2014 22:05
Show Gist options
  • Save iwoodruff/8744203 to your computer and use it in GitHub Desktop.
Save iwoodruff/8744203 to your computer and use it in GitHub Desktop.
SCARLET BROMANCEOM
class Tweet < ActiveRecord::Base
attr_accessible :body
def self.establish_connection
client = Twitter::REST::Client.new do |config|
config.consumer_key =
config.consumer_secret =
config.access_token =
config.access_token_secret =
end
return client
end
# the twitter gem method .establish_connection creates a link to the twitter database.
# The block acting on config draws information from our app’s registration with twitter.
def self.woeid_trends(woeid)
woeid_trend = Tweet.establish_connection.trends_place(woeid)
woeid_trends = woeid_trend.attrs[:trends]
return woeid_trends
end
# WOEID is a key returned from the Yahoo geolocation api, it’s a key unique to each country.
# .trends_place(woeid) draws a hash of tweet objects from a specific country.
# .attrs[:trends] specifies the key containing hashtags.
def self.coords_trends(country)
coords = Geocoder.search(country)[0].data["geometry"]["location"]
location = Tweet.establish_connection.trends_closest({lat: coords["lat"], long: coords["lng"]})
statement = "For political reasons or otherwise, #{country} does not have Twitter. Showing regional trends from the closest available location, #{location[0].attrs[:name]}, #{location[0].attrs[:country]}"
coords_woeid = location[0].attrs[:woeid]
woeid_trends = Tweet.woeid_trends(coords_woeid)
return [woeid_trends, statement]
end
# Not all countries have twitter and thus WOEID's would return nil and break the program.
# This function finds lat/long coords through geocoder and plugs them into the Twitter gem's .trends_closest method which returns an array of countries, each of which is a hash.
# From there, the woeid of that closest country is found and plugged into the woeid_trends function.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment