Class: Graticule::Geocoder::Yahoo

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

Overview

Constant Summary collapse

PRECISION =
{
  "country"=> :country,
  "state" => :state,
  "city" => :city,
  "zip+4" => :zip,
  "zip+2" => :zip,
  "zip" => :zip,
  "street" => :street,
  "address" => :address
}

Constants inherited from Base

Base::USER_AGENT

Instance Method Summary collapse

Constructor Details

#initialize(appid) ⇒ Yahoo

Web services initializer.

The appid is the Application ID that uniquely identifies your application. See: developer.yahoo.com/faq/index.html#appid

See developer.yahoo.com/search/rest.html



26
27
28
29
# File 'lib/graticule/geocoder/yahoo.rb', line 26

def initialize(appid)
  @appid = appid
  @url = URI.parse "http://api.local.yahoo.com/MapsService/V1/geocode"
end

Instance Method Details

#check_error(xml) ⇒ Object

Extracts and raises an error from xml, if any.

Raises:



67
68
69
70
# File 'lib/graticule/geocoder/yahoo.rb', line 67

def check_error(xml) #:nodoc:
  err = xml.elements['Error']
  raise Error, err.elements['Message'].text if err
end

#locate(address) ⇒ Object

Returns a Location for address.

The address can be any of:

  • city, state

  • city, state, zip

  • zip

  • street, city, state

  • street, city, state, zip

  • street, zip



40
41
42
43
44
# File 'lib/graticule/geocoder/yahoo.rb', line 40

def locate(address)
  location = (address.is_a?(String) ? address : location_from_params(address).to_s(:country => false))
  # yahoo pukes on line breaks
  get :location => location.gsub("\n", ', ')
end

#make_url(params) ⇒ Object

Creates a URL from the Hash params. Automatically adds the appid and sets the output type to ‘xml’.



74
75
76
77
78
79
# File 'lib/graticule/geocoder/yahoo.rb', line 74

def make_url(params) #:nodoc:
  params[:appid] = @appid
  params[:output] = 'xml'

  super params
end

#parse_response(xml) ⇒ Object

:nodoc:



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

def parse_response(xml) # :nodoc:
  r = xml.elements['ResultSet/Result[1]']
  returning Location.new do |location|
    location.precision = PRECISION[r.attributes['precision']] || :unknown

    if r.attributes.include? 'warning' then
      location.warning = r.attributes['warning']
    end

    location.latitude = r.elements['Latitude'].text.to_f
    location.longitude = r.elements['Longitude'].text.to_f

    location.street = r.elements['Address'].text.titleize unless r.elements['Address'].text.blank?
    location.locality = r.elements['City'].text.titleize unless r.elements['City'].text.blank?
    location.region = r.elements['State'].text
    location.postal_code = r.elements['Zip'].text
    location.country = r.elements['Country'].text
  end
end