data model for geoipblocks
This commit is contained in:
parent
bab83c91c1
commit
b0dfa5501f
|
|
@ -123,6 +123,7 @@ require "jam_ruby/models/facebook_signup"
|
|||
require "jam_ruby/models/recording_play"
|
||||
require "jam_ruby/models/feed"
|
||||
require "jam_ruby/models/jam_isp"
|
||||
require "jam_ruby/models/geo_ip_blocks"
|
||||
|
||||
include Jampb
|
||||
|
||||
|
|
|
|||
|
|
@ -57,6 +57,26 @@ module JamRuby
|
|||
|
||||
if ip_address
|
||||
# todo turn ip_address string into a number, then fetch the locid and ispid and the other stuff...
|
||||
|
||||
addr = JamIsp.ip_to_num(ip_address)
|
||||
puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
|
||||
|
||||
isp = JamIsp.lookup(addr)
|
||||
if isp.nil? then ispid = 0 else ispid = isp.coid end
|
||||
puts("============= JamIsp.lookup returns #{ispid} for #{addr} =============")
|
||||
|
||||
block = GeoIpBlocks.lookup(addr)
|
||||
if block.nil? then locid = 0 else locid = block.locid end
|
||||
puts("============= GeoIpBlocks.lookup returns #{locid} for #{addr} =============")
|
||||
|
||||
locidispid = 0
|
||||
latitude = 0.0
|
||||
longitude = 0.0
|
||||
countrycode = 'US'
|
||||
region = 'TX'
|
||||
city = 'Austin'
|
||||
|
||||
# todo stuff this stuff into the connection records
|
||||
end
|
||||
|
||||
sql =<<SQL
|
||||
|
|
@ -168,14 +188,18 @@ 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...
|
||||
|
||||
addr = JamIsp.ip_to_num(ip_address)
|
||||
puts("============= JamIsp returns #{addr} for #{ip_address} =============")
|
||||
puts("============= JamIsp.ip_to_num returns #{addr} for #{ip_address} =============")
|
||||
|
||||
isp = JamIsp.lookup(addr)
|
||||
if isp.nil? then ispid = 0 else ispid = isp.coid end
|
||||
puts("============= JamIsp returns #{ispid} for #{addr} =============")
|
||||
puts("============= JamIsp.lookup returns #{ispid} for #{addr} =============")
|
||||
|
||||
# todo turn ip_address string into a number, then fetch the locid and ispid and the other stuff...
|
||||
block = GeoIpBlocks.lookup(addr)
|
||||
if block.nil? then locid = 0 else locid = block.locid end
|
||||
puts("============= GeoIpBlocks.lookup returns #{locid} for #{addr} =============")
|
||||
|
||||
locidispid = 0
|
||||
latitude = 0.0
|
||||
|
|
@ -184,7 +208,6 @@ SQL
|
|||
region = 'TX'
|
||||
city = 'Austin'
|
||||
|
||||
|
||||
lock_connections(conn)
|
||||
|
||||
conn.exec("INSERT INTO connections (user_id, client_id, addr, locidispid, latitude, longitude, countrycode, region, city, aasm_state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
module JamRuby
|
||||
class GeoIpBlocks < ActiveRecord::Base
|
||||
|
||||
self.table_name = 'geoipblocks'
|
||||
|
||||
def self.lookup(ipnum)
|
||||
GeoIpBlocks.select(:locid)
|
||||
.where('geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip', ipnum, ipnum)
|
||||
.limit(1)
|
||||
.first
|
||||
end
|
||||
|
||||
def self.make_row(beginip, endip, locid)
|
||||
c = ActiveRecord::Base.connection.raw_connection
|
||||
c.prepare('blah', 'insert into geoipblocks (beginip, endip, locid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))')
|
||||
c.exec_prepared('blah', [beginip, endip, locid])
|
||||
c.exec("deallocate blah")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe GeoIpBlocks do
|
||||
|
||||
before do
|
||||
GeoIpBlocks.delete_all
|
||||
GeoIpBlocks.make_row(0x00000000, 0xffffffff, 17192)
|
||||
end
|
||||
|
||||
it "count" do GeoIpBlocks.count.should == 1 end
|
||||
|
||||
let(:first) { GeoIpBlocks.lookup(0x01020304) }
|
||||
let(:second) { GeoIpBlocks.lookup(0x02030405) }
|
||||
let(:seventh) { GeoIpBlocks.lookup(9999999999) } # bogus
|
||||
|
||||
it "first.locid" do first.locid.should == 17192 end
|
||||
it "second.locid" do second.locid.should == 17192 end
|
||||
it "seventh" do seventh.should be_nil end
|
||||
end
|
||||
Loading…
Reference in New Issue