Module: Geocoder::MaxmindDatabase

Extended by:
MaxmindDatabase
Included in:
MaxmindDatabase
Defined in:
lib/maxmind_database.rb

Instance Method Summary collapse

Instance Method Details

#archive_edition(package) ⇒ Object



38
39
40
41
42
43
44
# 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) ⇒ Object



32
33
34
35
36
# 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") ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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") ⇒ Object



23
24
25
26
27
28
29
30
# 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