diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 973ed5efe..be78578a6 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -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 diff --git a/ruby/lib/jam_ruby/connection_manager.rb b/ruby/lib/jam_ruby/connection_manager.rb index 0e9ee5baf..ebb1b9345 100644 --- a/ruby/lib/jam_ruby/connection_manager.rb +++ b/ruby/lib/jam_ruby/connection_manager.rb @@ -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 diff --git a/ruby/lib/jam_ruby/models/jam_isp.rb b/ruby/lib/jam_ruby/models/jam_isp.rb new file mode 100644 index 000000000..4157b1ce3 --- /dev/null +++ b/ruby/lib/jam_ruby/models/jam_isp.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/jam_isp_spec.rb b/ruby/spec/jam_ruby/models/jam_isp_spec.rb new file mode 100644 index 000000000..b68b309f7 --- /dev/null +++ b/ruby/spec/jam_ruby/models/jam_isp_spec.rb @@ -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 diff --git a/ruby/spec/jam_ruby/models/max_mind_geo_spec.rb b/ruby/spec/jam_ruby/models/max_mind_geo_spec.rb index 1df42817d..08cfb0766 100644 --- a/ruby/spec/jam_ruby/models/max_mind_geo_spec.rb +++ b/ruby/spec/jam_ruby/models/max_mind_geo_spec.rb @@ -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 - diff --git a/websocket-gateway/spec/factories.rb b/websocket-gateway/spec/factories.rb index 4bc7c87a6..35f0c4b84 100644 --- a/websocket-gateway/spec/factories.rb +++ b/websocket-gateway/spec/factories.rb @@ -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