Sha256: 1fafda808ee20d86cbe6bfed92d5a8f7fd4525dfd3e0d79815e4756ea2cde6c7

Contents?: true

Size: 1.6 KB

Versions: 15

Compression:

Stored size: 1.6 KB

Contents

module Graticule #:nodoc:
  module Geocoder #:nodoc:
    class Multi
      
      # The Multi geocoder allows you to use multiple geocoders in succession.
      #
      #   geocoder = Graticule.service(:multi).new(
      #     Graticule.service(:google).new("api_key"),
      #     Graticule.service(:yahoo).new("api_key"),
      #   )
      #   geocoder.locate '49423' # <= tries geocoders in succession
      #
      # The Multi geocoder will try the geocoders in order if a Graticule::AddressError
      # is raised.  You can customize this behavior by passing in a block to the Multi
      # geocoder.  For example, to try the geocoders until one returns a result with a
      # high enough precision:
      #
      #   geocoder = Graticule.service(:multi).new(geocoders) do |result|
      #     [:address, :street].include?(result.precision)
      #   end
      #
      # Geocoders will be tried in order until the block returned true for one of the results
      #
      def initialize(*geocoders, &acceptable)
        @acceptable = acceptable || lambda { true }
        @geocoders = geocoders.flatten
      end
      
      def locate(address)
        last_error = nil
        @geocoders.each do |geocoder|
          begin
            result = geocoder.locate address
            return result if @acceptable.call(result)
          rescue Error => e
            last_error = e
          rescue Errno::ECONNREFUSED
            logger.error("Connection refused to #{service}")
          end
        end
        raise last_error || AddressError.new("Couldn't find '#{address}' with any of the services")
      end
      
    end
  end
end

Version data entries

15 entries across 15 versions & 6 rubygems

Version Path
CodeMonkeySteve-graticule-0.2.11 lib/graticule/geocoder/multi.rb
CodeMonkeySteve-graticule-0.2.12 lib/graticule/geocoder/multi.rb
aub-graticule-0.2.11 lib/graticule/geocoder/multi.rb
collectiveidea-graticule-0.2.12 lib/graticule/geocoder/multi.rb
norman-graticule-0.2.7 lib/graticule/geocoder/multi.rb
pepe-graticule-0.2.11 lib/graticule/geocoder/multi.rb
aub-graticule-0.3.2 lib/graticule/geocoder/multi.rb
aub-graticule-0.3.1 lib/graticule/geocoder/multi.rb
aub-graticule-0.3.0 lib/graticule/geocoder/multi.rb
graticule-0.2.12 lib/graticule/geocoder/multi.rb
graticule-0.2.10 lib/graticule/geocoder/multi.rb
graticule-0.2.6 lib/graticule/geocoder/multi.rb
graticule-0.2.9 lib/graticule/geocoder/multi.rb
graticule-0.2.8 lib/graticule/geocoder/multi.rb
graticule-0.2.7 lib/graticule/geocoder/multi.rb