Module: Geocoder::MaxmindDatabase

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

Instance Method Summary collapse

Instance Method Details

#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_filename(package)))
  open(filepath, 'wb') do |file|
    uri = URI.parse(archive_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).each do |filepath,table|
    print "Resetting table #{table}..."
    ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
    puts "done"
    insert_into_table(table, filepath)
  end
end