From 04dab677f9b9c4ba64c529aa2f83f0f4ed331ea3 Mon Sep 17 00:00:00 2001 From: Scott Comer Date: Sun, 23 Feb 2014 17:24:25 -0600 Subject: [PATCH] model for geoiplocations --- ruby/lib/jam_ruby.rb | 1 + ruby/lib/jam_ruby/connection_manager.rb | 20 ++++++----- ruby/lib/jam_ruby/models/geo_ip_locations.rb | 20 +++++++++++ .../jam_ruby/models/geo_ip_locations_spec.rb | 36 +++++++++++++++++++ 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 ruby/lib/jam_ruby/models/geo_ip_locations.rb create mode 100644 ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 41ffebc8b..10eb7eef3 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -124,6 +124,7 @@ require "jam_ruby/models/recording_play" require "jam_ruby/models/feed" require "jam_ruby/models/jam_isp" require "jam_ruby/models/geo_ip_blocks" +require "jam_ruby/models/geo_ip_locations" include Jampb diff --git a/ruby/lib/jam_ruby/connection_manager.rb b/ruby/lib/jam_ruby/connection_manager.rb index 2320218b6..086b14a22 100644 --- a/ruby/lib/jam_ruby/connection_manager.rb +++ b/ruby/lib/jam_ruby/connection_manager.rb @@ -56,18 +56,20 @@ module JamRuby end if ip_address - # todo turn ip_address string into a number, then fetch the locid and ispid and the other stuff... + # todo turn ip_address string into a number, then fetch the isp and block records addr = JamIsp.ip_to_num(ip_address) - puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============") + #puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============") isp = JamIsp.lookup(addr) + #puts("============= JamIsp.lookup returns #{isp.inspect} for #{addr} =============") if isp.nil? then ispid = 0 else ispid = isp.coid end - puts("============= JamIsp.lookup returns #{ispid} for #{addr} =============") block = GeoIpBlocks.lookup(addr) + #puts("============= GeoIpBlocks.lookup returns #{block.inspect} for #{addr} =============") if block.nil? then locid = 0 else locid = block.locid end - puts("============= GeoIpBlocks.lookup returns #{locid} for #{addr} =============") + + location = GeoIpLocations.lookup(locid) locidispid = 0 latitude = 0.0 @@ -188,18 +190,20 @@ SQL ConnectionManager.active_record_transaction do |connection_manager| conn = connection_manager.pg_conn - # todo turn ip_address string into a number, then fetch the locid and ispid and the other stuff... + # todo turn ip_address string into a number, then fetch the isp and block records addr = JamIsp.ip_to_num(ip_address) - puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============") + #puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============") isp = JamIsp.lookup(addr) + #puts("============= JamIsp.lookup returns #{isp.inspect} for #{addr} =============") if isp.nil? then ispid = 0 else ispid = isp.coid end - puts("============= JamIsp.lookup returns #{ispid} for #{addr} =============") block = GeoIpBlocks.lookup(addr) + #puts("============= GeoIpBlocks.lookup returns #{block.inspect} for #{addr} =============") if block.nil? then locid = 0 else locid = block.locid end - puts("============= GeoIpBlocks.lookup returns #{locid} for #{addr} =============") + + location = GeoIpLocations.lookup(locid) locidispid = 0 latitude = 0.0 diff --git a/ruby/lib/jam_ruby/models/geo_ip_locations.rb b/ruby/lib/jam_ruby/models/geo_ip_locations.rb new file mode 100644 index 000000000..28dcfa0ef --- /dev/null +++ b/ruby/lib/jam_ruby/models/geo_ip_locations.rb @@ -0,0 +1,20 @@ +module JamRuby + class GeoIpLocations < ActiveRecord::Base + + self.table_name = 'geoiplocations' + + def self.lookup(locid) + GeoIpLocations.select('locid, countrycode, region, city, latitude, longitude') + .where(:locid => locid) + .limit(1) + .first + end + + def self.make_row(locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode) + c = ActiveRecord::Base.connection.raw_connection + c.prepare('blah', 'insert into geoiplocations (locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode, geog) values($1, $2, $3, $4, $5, $6, $7, $8, $9, ST_SetSRID(ST_MakePoint($7, $6), 4326)::geography)') + c.exec_prepared('blah', [locid, countrycode, region, city, postalcode, latitude, longitude, metrocode, areacode]) + c.exec("deallocate blah") + end + end +end diff --git a/ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb b/ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb new file mode 100644 index 000000000..c6be8a6df --- /dev/null +++ b/ruby/spec/jam_ruby/models/geo_ip_locations_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe GeoIpLocations do + + before do + GeoIpLocations.delete_all + GeoIpLocations.make_row(17192, 'US', 'TX', 'Austin', '78749', 30.2076, -97.8587, 635, '512') + GeoIpLocations.make_row(48086, 'MX', '28', 'Matamoros', '', 25.8833, -97.5000, nil, '') + end + + it "count" do GeoIpLocations.count.should == 2 end + + let(:first) { GeoIpLocations.lookup(17192) } + let(:second) { GeoIpLocations.lookup(48086) } + let(:third) { GeoIpLocations.lookup(999999) } # bogus + + it "first" do + first.locid.should == 17192 + first.countrycode.should eql('US') + first.region.should eql('TX') + first.city.should eql('Austin') + first.latitude.should == 30.2076 + first.longitude.should == -97.8587 + end + + it "second" do + second.locid.should == 48086 + second.countrycode.should eql('MX') + second.region.should eql('28') + second.city.should eql('Matamoros') + second.latitude.should == 25.8833 + second.longitude.should == -97.5000 + end + + it "third" do third.should be_nil end +end