Class: Pumi::DataSource::Geocoder::AbstractGeocoder

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

Defined Under Namespace

Classes: AbstractProvider, Google, Misspelling, Nominatim, Result

Constant Summary collapse

MISSPELLINGS =
[]
PROVIDERS =
{
  nominatim: Nominatim,
  google: Google
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(geocoder: ::Geocoder, providers: PROVIDERS.keys, **options) ⇒ AbstractGeocoder

Returns a new instance of AbstractGeocoder.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pumi/data_source/geocoder.rb', line 95

def initialize(geocoder: ::Geocoder, providers: PROVIDERS.keys, **options)
  @options = options

  geocoder.configure(
    google: {
      api_key: ENV["GOOGLE_API_KEY"]
    }
  )

  @providers = Array(providers).map do |name|
    PROVIDERS.fetch(name).new(geocoder:, name:)
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



93
94
95
# File 'lib/pumi/data_source/geocoder.rb', line 93

def options
  @options
end

#providersObject (readonly)

Returns the value of attribute providers.



93
94
95
# File 'lib/pumi/data_source/geocoder.rb', line 93

def providers
  @providers
end

Instance Method Details

#geocode_allObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pumi/data_source/geocoder.rb', line 109

def geocode_all
  locations.each_with_object([]).with_index do |(location, results), _index|
    next if !options[:regeocode] && !location.geodata.nil?

    geocoder_result = geocode(location)

    if geocoder_result.nil?
      ungeocoded_locations << location
      next
    end

    results << build_result(code: location.id, geocoder_result:)
  end
end