module Geocoder::MaxmindDatabase

Public Instance Methods

archive_edition(package) click to toggle source
# File lib/maxmind_database.rb, line 38
def archive_edition(package)
  {
    geolite_country_csv: "GeoLite2-Country-CSV",
    geolite_city_csv: "GeoLite2-City-CSV",
    geolite_asn_csv: "GeoLite2-ASN-CSV"
  }[package]
end
archive_filename(package) click to toggle source
# File lib/maxmind_database.rb, line 32
def archive_filename(package)
  p = archive_url_path(package)
  s = !(pos = p.rindex('/')).nil? && pos + 1 || 0
  p[s..-1]
end
download(package, dir = "tmp") click to toggle source
# File lib/maxmind_database.rb, line 8
def download(package, dir = "tmp")
  filepath = File.expand_path(File.join(dir, "#{archive_edition(package)}.zip"))
  open(filepath, 'wb') do |file|
    uri = URI.parse(base_url(package))
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.request_get(uri.path) do |resp|
        # TODO: show progress
        resp.read_body do |segment|
          file.write(segment)
        end
      end
    end
  end
end
insert(package, dir = "tmp") click to toggle source
# File lib/maxmind_database.rb, line 23
def insert(package, dir = "tmp")
  data_files(package, dir).each do |filepath,table|
    print "Resetting table #{table}..."
    ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
    puts "done"
    insert_into_table(table, filepath)
  end
end