diff --git a/ruby/lib/jam_ruby/models/max_mind_isp.rb b/ruby/lib/jam_ruby/models/max_mind_isp.rb index cab07f862..2c7d6ed9a 100644 --- a/ruby/lib/jam_ruby/models/max_mind_isp.rb +++ b/ruby/lib/jam_ruby/models/max_mind_isp.rb @@ -13,9 +13,9 @@ module JamRuby MaxMindIsp.delete_all File.open(file, 'r:ISO-8859-1') do |io| io.gets # eat the copyright line. gah, why do they have that in their file?? - MaxMindIsp.pg_copy_from io, :map => { 'beginIp' => 'ip_start', 'endIp' => 'ip_end', 'countryCode' => 'country', 'ISP' => 'isp'}, :columns => [:beginIp, :endIp, :countryCode, :ISP] do |row| - row[0] = strip_quotes(row[0]) - row[1] = strip_quotes(row[1]) + MaxMindIsp.pg_copy_from io, :map => { 'beginIp' => 'ip_bottom', 'endIp' => 'ip_top', 'countryCode' => 'country', 'ISP' => 'isp'}, :columns => [:beginIp, :endIp, :countryCode, :ISP] do |row| + row[0] = ip_address_to_int(strip_quotes(row[0])) + row[1] = ip_address_to_int(strip_quotes(row[1])) row[2] = row[2] row[3] = row[3..-1].join(',') # this is because the parser just cuts on any ',' and ignores double quotes. essentially postgres-copy isn't a great csv parser -- or I need to configure it better while row.length > 4 @@ -27,6 +27,15 @@ module JamRuby 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 + + private + def self.strip_quotes str return nil if str.nil? @@ -45,4 +54,4 @@ module JamRuby str.gsub(/\"/, '""') end end -end +end \ No newline at end of file