model for geoiplocations

This commit is contained in:
Scott Comer 2014-02-23 17:24:25 -06:00
parent a8144d4b28
commit 04dab677f9
4 changed files with 69 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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