class WorldFlags::Generators::GeoipGenerator

Public Instance Methods

main_flow() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 16
def main_flow
  local? ? copy_local : download_latest
end

Protected Instance Methods

agent_options() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 66
def agent_options
  {"User-Agent" => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100402 Ubuntu/9.10 (karmic) Firefox/3.5.9", "From" => "foo@bar.com", "Referer" => "http://www.foo.bar/"}
end
copy_local() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 26
def copy_local
  copy_file "GeoIP.dat", "db/GeoIP.dat"
end
download_latest() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 30
def download_latest 
  # http = ::Resourceful::HttpAccessor.new
  # resource = http.resource( zip_adr )
  # # resource.on_redirect { |req, resp| resp.header['Location'] =~ /example.com/ }
  # resp = resource.get  # Will only follow the redirect if the new location is example.com

  # puts "get: #{file_name} from #{zip_adr}"

  agent = Mechanize.new  { |agent| agent.user_agent_alias = 'Mac Safari'}
  agent.robots = false

  # puts "robots allowed? #{agent.robots_allowed?}"

  agent.pluggable_parser.default = Mechanize::Download
  agent.get(zip_adr).save(file_name)

  # File.open(file_name, 'w+') do |zip_file|
  #   zip_file.write(resp.body)
  # end
  #zf is an instance of class Tempfile
  # puts "zip path: #{zf.path}"

  Zip::ZipFile.open(file_name) do |zipfile|
    # puts "zipfile: #{zipfile}"
    #zipfile.class is Zip::ZipFile
    zipfile.each do |e|
      #e is an instance of Zip::ZipEntry
      fpath = File.join file_name, e.to_s
      FileUtils.mkdir_p File.dirname(target_location)
      #the block is for handling an existing file. returning true will overwrite the files.
      zipfile.extract(e, target_location) { true }
    end
  end
  FileUtils.rm(file_name) # if options[:cleanup]
end
file_name() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 78
def file_name
  'GeoIP.dat.gz'
end
local?() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 22
def local?
  !(options[:local] == false)
end
target_location() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 70
def target_location
  ::Rails.root.join 'db', 'GeoIP.dat'
end
zip_adr() click to toggle source
# File lib/generators/world_flags/geoip_generator.rb, line 74
def zip_adr
  "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/#{file_name}"
end