class Msewage::Importer::Importer

Attributes

api[R]
config[R]

Public Class Methods

new(options) click to toggle source
# File lib/msewage-importer/importer.rb, line 17
def initialize(options)
  @config = Config.new(options)
  puts options.inspect if config.verbose
end

Public Instance Methods

import() click to toggle source
# File lib/msewage-importer/importer.rb, line 22
def import
  assert
  importer = source_importer
  sources = importer.import
  import_sources(sources)
rescue NoTypeSpecified, NoSourceSpecified, InvalidTypeSpecified => e
  STDERR.puts e
  STDERR.puts "\nType #{$0} --help"
  exit
end

Private Instance Methods

add_source_type(source) click to toggle source
# File lib/msewage-importer/importer.rb, line 53
def add_source_type(source)
  source["source_type"] = source_type
  source
end
assert() click to toggle source
# File lib/msewage-importer/importer.rb, line 77
def assert
  raise NoTypeSpecified unless config_option(:type)
  raise InvalidTypeSpecified unless valid_type?
  raise NoSourceSpecified unless config_option(:source)
end
config_option(option) click to toggle source
# File lib/msewage-importer/importer.rb, line 83
def config_option(option)
  config.send(:config).send(:[], option)
end
ensure_coordinates(source) click to toggle source
# File lib/msewage-importer/importer.rb, line 45
def ensure_coordinates(source)
  unless source["latitude"] && source["longitude"]
    coordinates = geolocator.geolocate(source["location"])
    source["latitude"], source["longitude"] = coordinates
  end
  source
end
geolocator() click to toggle source
# File lib/msewage-importer/importer.rb, line 37
def geolocator
  @geolocator ||= Geolocator.new(config)
end
import_source(source) click to toggle source
# File lib/msewage-importer/importer.rb, line 69
def import_source(source)
  ensure_coordinates(source)
  add_source_type(source)
  unless api.insert(source)
    STDERR.puts "Error inserting #{source.inspect}"
  end
end
import_sources(sources) click to toggle source
# File lib/msewage-importer/importer.rb, line 62
def import_sources(sources)
  sources.each do |source|
    import_source(source)
  end
  puts
end
source_importer() click to toggle source
# File lib/msewage-importer/importer.rb, line 58
def source_importer
  Importers::Base.factory(config.source)
end
source_type() click to toggle source
# File lib/msewage-importer/importer.rb, line 87
def source_type
  SourceTypes.given_type_to_apis(config.type)
end
valid_type?() click to toggle source
# File lib/msewage-importer/importer.rb, line 91
def valid_type?
  !!source_type
end