Class: Graticule::Geocoder::Yahoo

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

Overview

Constant Summary collapse

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

Constants inherited from Base

Base::USER_AGENT

Instance Attribute Summary

Attributes inherited from Base

#preference

Instance Method Summary collapse

Methods inherited from Base

#<=>

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:



85
86
87
88
# File 'lib/graticule/geocoder/yahoo.rb', line 85

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

#extract_location(address) ⇒ Object



48
49
50
51
52
# File 'lib/graticule/geocoder/yahoo.rb', line 48

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

#locate(*args) ⇒ 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
45
46
# File 'lib/graticule/geocoder/yahoo.rb', line 40

def locate(*args)
  case args.first
    when :first then get :first, :location => extract_location(args[1]) 
    when :all then get :all, :location => extract_location(args[1])
    else get :first, :location => extract_location(args.first)
  end
end

#make_url(params) ⇒ Object

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



92
93
94
95
96
97
# File 'lib/graticule/geocoder/yahoo.rb', line 92

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

  super params
end

#parse_location_xml(element) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/graticule/geocoder/yahoo.rb', line 66

def parse_location_xml(element)
  returning Location.new do |location|
    location.precision = PRECISION[element.attributes['precision']] || Precision.unknown

    if element.attributes.include? 'warning' then
      location.warning = element.attributes['warning']
    end
    location.latitude = element.elements['Latitude'].text.to_f
    location.longitude = element.elements['Longitude'].text.to_f

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

#parse_response(parse_type, xml) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/graticule/geocoder/yahoo.rb', line 54

def parse_response(parse_type, xml)
  if parse_type == :all
    locations = []
    xml.elements['ResultSet'].each do |element|
      locations << parse_location_xml(element)
    end
    return locations
  else
    return parse_location_xml(xml.elements['ResultSet/Result[1]'])
  end
end