data model for jamisp table

This commit is contained in:
Scott Comer 2014-02-22 22:00:32 -06:00
parent 5c9a75b466
commit 74fc2f1b48
6 changed files with 89 additions and 4 deletions

View File

@ -122,7 +122,7 @@ require "jam_ruby/models/icecast_mount_template"
require "jam_ruby/models/facebook_signup"
require "jam_ruby/models/recording_play"
require "jam_ruby/models/feed"
require "jam_ruby/models/jam_isp"
include Jampb

View File

@ -168,8 +168,15 @@ SQL
ConnectionManager.active_record_transaction do |connection_manager|
conn = connection_manager.pg_conn
addr = JamIsp.ip_to_num(ip_address)
puts("============= JamIsp 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} =============")
# todo turn ip_address string into a number, then fetch the locid and ispid and the other stuff...
addr = 0
locidispid = 0
latitude = 0.0
longitude = 0.0

View File

@ -0,0 +1,28 @@
require 'ipaddr'
module JamRuby
class JamIsp < ActiveRecord::Base
self.table_name = 'jamisp'
def self.ip_to_num(ip_addr)
i = IPAddr.new(ip_addr)
return i.to_i if i.ipv4?
nil
end
def self.lookup(ipnum)
JamIsp.select(:coid)
.where('geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip', ipnum, ipnum)
.limit(1)
.first
end
def self.make_row(beginip, endip, coid)
c = ActiveRecord::Base.connection.raw_connection
c.prepare('blah', 'insert into jamisp (beginip, endip, coid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))')
c.exec_prepared('blah', [beginip, endip, coid])
c.exec("deallocate blah")
end
end
end

View File

@ -0,0 +1,51 @@
require 'spec_helper'
describe JamIsp do
before do
JamIsp.delete_all
JamIsp.make_row(0x01020300, 0x010203ff, 1)
JamIsp.make_row(0x02030400, 0x020304ff, 2)
JamIsp.make_row(0x03040500, 0x030405ff, 3)
JamIsp.make_row(0x04050600, 0x040506ff, 4)
JamIsp.make_row(0xc0A80100, 0xc0A801ff, 5)
JamIsp.make_row(0xfffefd00, 0xfffefdff, 6)
end
after do
JamIsp.delete_all
JamIsp.make_row(0x00000000, 0xffffffff, 1)
end
it "count" do JamIsp.count.should == 6 end
let(:first_addr) { JamIsp.ip_to_num('1.2.3.4') }
let(:second_addr) { JamIsp.ip_to_num('2.3.4.5') }
let(:third_addr) { JamIsp.ip_to_num('3.4.5.6') }
let(:fourth_addr) { JamIsp.ip_to_num('4.5.6.7') }
let(:fifth_addr) { JamIsp.ip_to_num('192.168.1.107') }
let(:sixth_addr) { JamIsp.ip_to_num('255.254.253.252') }
it "first_addr" do first_addr.should == 0x01020304 end
it "second_addr" do second_addr.should == 0x02030405 end
it "third_addr" do third_addr.should == 0x03040506 end
it "fourth_addr" do fourth_addr.should == 0x04050607 end
it "fifth_addr" do fifth_addr.should == 0xc0A8016b end
it "sixth_addr" do sixth_addr.should == 0xfffefdfc end
let(:first) { JamIsp.lookup(0x01020304) }
let(:second) { JamIsp.lookup(0x02030405) }
let(:third) { JamIsp.lookup(0x03040506) }
let(:fourth) { JamIsp.lookup(0x04050607) }
let(:fifth) { JamIsp.lookup(0xc0A8016b) }
let(:sixth) { JamIsp.lookup(0xfffefdfc) }
let(:seventh) { JamIsp.lookup(0) } # bogus
it "first.coid" do first.coid.should == 1 end
it "second.coid" do second.coid.should == 2 end
it "third.coid" do third.coid.should == 3 end
it "fourth.coid" do fourth.coid.should == 4 end
it "fifth.coid" do fifth.coid.should == 5 end
it "sixth.coid" do sixth.coid.should == 6 end
it "seventh" do seventh.should be_nil end
end

View File

@ -35,4 +35,3 @@ describe MaxMindGeo do
it { third.ip_start.should == '1.0.1.0' }
it { third.ip_end.should == '1.0.1.255' }
end

View File

@ -39,7 +39,7 @@ FactoryGirl.define do
countrycode 'US'
region 'TX'
city 'Austin'
ip_address "1.1.1.1"
ip_address '1.1.1.1'
as_musician true
end