Class: GeoMagic::GraticuleMultiAdapter

Inherits:
GraticuleAdapter show all
Defined in:
lib/geo_magic/geocode/graticule_adapter.rb

Overview

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

Use the :timeout option to specify the number of seconds to allow for each geocoder before raising a Timout::Error (defaults to 10 seconds).

Graticule.service(:multi).new(geocoders, :timeout => 3)

Instance Attribute Summary collapse

Attributes inherited from GeoAdapter

#environment, #service_name

Instance Method Summary collapse

Methods inherited from GraticuleAdapter

#create_graticule_service, #gs_service

Methods inherited from GeoAdapter

#get_key, #get_service, #setup

Constructor Details

#initialize(services, env = :default) ⇒ GraticuleMultiAdapter

Returns a new instance of GraticuleMultiAdapter.



47
48
49
50
# File 'lib/geo_magic/geocode/graticule_adapter.rb', line 47

def initialize services, env = :default
  super(:multi, env)
  @map_services = [services].compact.uniq.flatten
end

Instance Attribute Details

#geo_coderObject

Returns the value of attribute geo_coder.



45
46
47
# File 'lib/geo_magic/geocode/graticule_adapter.rb', line 45

def geo_coder
  @geo_coder
end

#map_servicesObject

Returns the value of attribute map_services.



45
46
47
# File 'lib/geo_magic/geocode/graticule_adapter.rb', line 45

def map_services
  @map_services
end

Instance Method Details

#geocode(location_str) ⇒ Object



67
68
69
# File 'lib/geo_magic/geocode/graticule_adapter.rb', line 67

def geocode location_str
  geo_coder.locate location_str
end

#instance(options = {}, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/geo_magic/geocode/graticule_adapter.rb', line 52

def instance options = {}, &block      
  @geo_coder ||= begin      
    if block
      gs_service.new multi_services, options do |result|
        yield
      end
    else
      gs_service.new multi_services, options do |result|
        true
      end          
    end          
  end
  self
end

#multi_servicesObject



71
72
73
74
75
76
77
# File 'lib/geo_magic/geocode/graticule_adapter.rb', line 71

def multi_services
  map_services.map do |service_name|
    api_key = send :"#{service_name}_key"
    raise ArgumentError, "API key for service #{service_name} has not been set. Please insert key in your api keys configuration file" if api_key.blank? 
    Graticule.service(service_name.to_sym).new(api_key)
  end      
end