Skip to content
Snippets Groups Projects
Commit 05483d32 authored by Alex Reisner's avatar Alex Reisner
Browse files

Put methods in module to prevent naming collisions.

parent e9a6658b
No related branches found
No related tags found
No related merge requests found
......@@ -9,61 +9,65 @@ namespace :geocoder do
desc "Download MaxMind GeoLite City data"
task :download do
p = check_for_package!
download(p, dir: ENV['DIR'] || "tmp/")
p = MaxmindTask.check_for_package!
MaxmindTask.download!(p, dir: ENV['DIR'] || "tmp/")
end
desc "Extract (unzip) MaxMind GeoLite City data"
task :extract do
p = check_for_package!
extract(p, dir: ENV['DIR'] || "tmp/")
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 = check_for_package!
insert(p, dir: ENV['DIR'] || "tmp/")
p = MaxmindTask.check_for_package!
MaxmindTask.insert!(p, dir: ENV['DIR'] || "tmp/")
end
end
end
end
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
module MaxmindTask
extend self
def download(package, options = {})
p = "geolite_#{package}_csv".intern
Geocoder::MaxmindDatabase.download(p, options[:dir])
end
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 extract(package, options = {})
begin
require 'zip'
rescue LoadError
puts "Please install gem: rubyzip (>= 1.0.0)"
exit
def download!(package, options = {})
p = "geolite_#{package}_csv".intern
Geocoder::MaxmindDatabase.download(p, options[:dir])
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)
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
end
def insert(package, options = {})
p = "geolite_#{package}_csv".intern
Geocoder::MaxmindDatabase.insert(p, options[:dir])
def insert!(package, options = {})
p = "geolite_#{package}_csv".intern
Geocoder::MaxmindDatabase.insert(p, options[:dir])
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment