jam-cloud/ruby/lib/jam_ruby/models/max_mind_geo.rb

38 lines
1.2 KiB
Ruby

module JamRuby
class MaxMindGeo < ActiveRecord::Base
self.table_name = 'max_mind_geo'
def self.import_from_max_mind(file)
# File Geo-124
# Format:
# startIpNum,endIpNum,country,region,city,postalCode,latitude,longitude,dmaCode,areaCode
MaxMindGeo.transaction do
MaxMindGeo.delete_all
File.open(file, 'r:ISO-8859-1') do |io|
MaxMindGeo.pg_copy_from io, :map => { 'startIpNum' => 'ip_bottom', 'endIpNum' => 'ip_top', 'country' => 'country', 'region' => 'region', 'city' => 'city'}, :columns => [:startIpNum, :endIpNum, :country, :region, :city] do |row|
row[0] = ip_address_to_int(row[0])
row[1] = ip_address_to_int(row[1])
row.delete_at(5)
row.delete_at(5)
row.delete_at(5)
row.delete_at(5)
row.delete_at(5)
end
end
end
end
# Make an IP address fit in a signed int. Just divide it by 2, as the least significant part
# just can't possibly matter. We can verify this if needed. My guess is the entire bottom octet is
# actually irrelevant
def self.ip_address_to_int(ip)
ip.split('.').inject(0) {|total,value| (total << 8 ) + value.to_i} / 2
end
end
end