Class: Graticule::Geocoder::Multimap

Inherits:
Rest
  • Object
show all
Defined in:
lib/graticule/geocoder/multimap.rb

Overview

Multimap geocoding API

Constant Summary collapse

PRECISION =

This precision information is not complete. More details should be implemented from: www.multimap.com/share/documentation/clientzone/gqcodes.htm

{
  "6"=> :country,
  "5" => :state,
  "4" => :postal_code,
  "3" => :city,
  "2" => :street,
  "1" => :address
}

Constants inherited from Base

Base::USER_AGENT

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Multimap

Web services initializer.

The api_key is the Open API key that uniquely identifies your application.

See www.multimap.com/openapi/



28
29
30
31
# File 'lib/graticule/geocoder/multimap.rb', line 28

def initialize(api_key)
  @api_key = api_key
  @url = URI.parse "http://clients.multimap.com/API/geocode/1.2/#{@api_key}"
end

Instance Method Details

#check_error(xml) ⇒ Object

Raises:



66
67
68
69
# File 'lib/graticule/geocoder/multimap.rb', line 66

def check_error(xml)
  error = xml.elements['Results'].attributes['errorCode']
  raise Error, error unless error.nil?
end

#locate(address) ⇒ Object

Returns a location for an address in the form of a String, Hash or Location.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/graticule/geocoder/multimap.rb', line 35

def locate(address)
  location = address.is_a?(String) ? address : location_from_params(address)
  case location
  when String
    get :qs => location
  when Location
    get "street" => location.street, 
        "region" => location.region, 
        "city" => location.city, 
        "postalCode" => location.postal_code, 
        "countryCode" => location.country
  end
end

#parse_response(xml) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/graticule/geocoder/multimap.rb', line 49

def parse_response(xml)
  r = xml.elements['Results/Location[1]']
    
  location = Location.new
  location.precision = PRECISION[r.attributes['geocodeQuality']] || :unknown
  
  location.street = r.elements['Address/Street'].text.titleize unless r.elements['Address/Street'].nil?
  location.locality = r.elements['Address/Areas/Area'].text.titleize unless r.elements['Address/Areas/Area'].nil?
  location.region = r.elements['Address/State'].text.titleize unless r.elements['Address/State'].nil?
  location.postal_code = r.elements['Address/PostalCode'].text unless r.elements['Address/PostalCode'].nil?
  location.country = r.elements['Address/CountryCode'].text
  
  location.latitude = r.elements['Point/Lat'].text.to_f
  location.longitude = r.elements['Point/Lon'].text.to_f
  [location] 
end