-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.rb
55 lines (46 loc) · 1.74 KB
/
environment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require "rubygems"
require "graticule"
require "active_support"
require "active_record"
db_config = YAML.load_file('config/database.yml')
ActiveRecord::Base.establish_connection(db_config["production"])
class Migrate < ActiveRecord::Migration
def self.up
create_table :tweets do |t|
t.string :user, :body, :overlay_body
t.string :screenname, :body, :overlay_body
t.integer :twitter_id
t.column "latitude", :decimal, :precision => 15, :scale => 12
t.column "longitude", :decimal, :precision => 15, :scale => 12
t.datetime :published_at, :created_at
end
end
def self.down
drop_table :posts
end
end
# This is only a "localhost"-Apikey:
#GOOGLE_API_KEY = "2ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA"
GOOGLE_API_KEY = "ABQIAAAA3hTMxebvnbMvuQzYVBzbKhSjSB4hbSIVmu7QqeR02zD68C5aUhSY0ElTBXNgn76EPHFLxoncuZZ4OQ"
GEOCODER = Graticule.service(:google).new(GOOGLE_API_KEY)
class Tweet < ActiveRecord::Base
validates_presence_of :twitter_id, :body, :latitude, :longitude, :user
validates_uniqueness_of :twitter_id
before_validation_on_create :extract_geocodes
def extract_geocodes
puts "extracting geocode for #{self.body}"
if query_match = self.body.match(/l:(.*)$/i)
puts "matching"
query = query_match[1]
location = GEOCODER.locate(query)
puts "#{location}"
self.latitude = location.latitude
self.longitude = location.longitude
# remove the L: ... part?
self.overlay_body = "<a href=\"http://twitter.com/#{self.screenname}\">#{self.user}</a>: #{self.body.gsub(/l:(.*)$/i,"").gsub(/^@gettingwasted/,"")}"
end
rescue
puts "Error extracting geo informations"
end
end
Migrate.up unless Tweet.table_exists?