Class: FreeZipcodeData::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/free_zipcode_data/data_source.rb

Constant Summary collapse

BASE_URL =
'http://download.geonames.org/export/zip'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country = nil) ⇒ DataSource

Returns a new instance of DataSource.



13
14
15
16
17
# File 'lib/free_zipcode_data/data_source.rb', line 13

def initialize(country = nil)
  @country = country
  @options = Options.instance.hash
  @logger  = Logger.instance
end

Instance Attribute Details

#countryObject (readonly)

Returns the value of attribute country.



11
12
13
# File 'lib/free_zipcode_data/data_source.rb', line 11

def country
  @country
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/free_zipcode_data/data_source.rb', line 11

def options
  @options
end

Instance Method Details

#datafileObject



28
29
30
31
32
# File 'lib/free_zipcode_data/data_source.rb', line 28

def datafile
  @datafile ||= begin
    datafile_with_headers
  end
end

#downloadObject



19
20
21
22
23
24
25
26
# File 'lib/free_zipcode_data/data_source.rb', line 19

def download
  return nil if !options.clobber && File.exist?(zipfile_path)
  FileUtils.mkdir_p(options.work_dir)
  @logger.info("Downloading: #{zipfile} from GeoNames...")
  open(zipfile_path, 'wb') do |file|
    file << open("#{BASE_URL}/#{zipfile}").read
  end
end