File: maxmind.rake

package info (click to toggle)
ruby-geocoder 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 732 kB
  • sloc: ruby: 6,173; makefile: 3
file content (73 lines) | stat: -rw-r--r-- 1,954 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'maxmind_database'

namespace :geocoder do
  namespace :maxmind do
    namespace :geolite do

      desc "Download and load/refresh MaxMind GeoLite City data"
      task load: [:download, :extract, :insert]

      desc "Download MaxMind GeoLite City data"
      task :download do
        p = MaxmindTask.check_for_package!
        MaxmindTask.download!(p, dir: ENV['DIR'] || "tmp/")
      end

      desc "Extract (unzip) MaxMind GeoLite City data"
      task :extract do
        p = MaxmindTask.check_for_package!
        MaxmindTask.extract!(p, dir: ENV['DIR'] || "tmp/")
      end

      desc "Load/refresh MaxMind GeoLite City data"
      task insert: [:environment] do
        p = MaxmindTask.check_for_package!
        MaxmindTask.insert!(p, dir: ENV['DIR'] || "tmp/")
      end
    end
  end
end

module MaxmindTask
  extend self

  def check_for_package!
    if %w[city country].include?(p = ENV['PACKAGE'])
      return p
    else
      puts "Please specify PACKAGE=city or PACKAGE=country"
      exit
    end
  end

  def download!(package, options = {})
    p = "geolite_#{package}_csv".intern
    Geocoder::MaxmindDatabase.download(p, options[:dir])
  end

  def extract!(package, options = {})
    begin
      require 'zip'
    rescue LoadError
      puts "Please install gem: rubyzip (>= 1.0.0)"
      exit
    end
    require 'fileutils'
    p = "geolite_#{package}_csv".intern
    archive_filename = Geocoder::MaxmindDatabase.archive_filename(p)
    Zip::File.open(File.join(options[:dir], archive_filename)).each do |entry|
      filepath = File.join(options[:dir], entry.name)
      if File.exist? filepath
        warn "File already exists (#{entry.name}), skipping"
      else
        FileUtils.mkdir_p(File.dirname(filepath))
        entry.extract(filepath)
      end
    end
  end

  def insert!(package, options = {})
    p = "geolite_#{package}_csv".intern
    Geocoder::MaxmindDatabase.insert(p, options[:dir])
  end
end