Sha256: c8f58c8affbf8fad3f035f8c80a5c816759cbddd5152db0d769b09f346de5073

Contents?: true

Size: 965 Bytes

Versions: 1

Compression:

Stored size: 965 Bytes

Contents

require 'rake'

namespace :geolocation do
  desc 'Update the Geolocation database. Specify target= to set the target.'
  task :update do
    require 'open-uri'
    require 'zlib'
    require 'stringio'
    
    raise 'Please specify the target' unless ENV['target']
    file     = ENV['target']
    tmp_file = file + '.tmp'
    
    url = ENV['url'] || "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"
    content = nil
    open(url) do |io|
      compr = StringIO.new(raw = io.read)
      if compr.read(2) == "\x1f\x8b"
        compr.rewind
        gz = Zlib::GzipReader.new(compr)
        content = gz.read
        gz.close
      else
        content = raw
      end
    end
    
    begin
      File.open(tmp_file, 'w') do |f|
        f.write(content)
      end
      File.unlink(file) if FileTest.exists?(file)
      File.rename(tmp_file, file)
    rescue
      raise "Error: Problem updating file! #{$!} (#{$!.class})"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ivanvc-geolocation_city-0.0.1 lib/geolocation_city/rake_task.rb