Module Ym4r::YahooMaps::BuildingBlock::Geocoding
In: lib/ym4r/yahoo_maps/building_block/geocoding.rb

Methods

get  

Classes and Modules

Class Ym4r::YahooMaps::BuildingBlock::Geocoding::Result

Public Class methods

Sends a request to the Yahoo! Maps geocoding service and returns the result in an easy to use Ruby object, hiding the creation of the query string and the XML parsing of the answer.

[Source]

    # File lib/ym4r/yahoo_maps/building_block/geocoding.rb, line 11
11:         def self.get(param)
12:           unless param.has_key?(:street) or
13:               param.has_key?(:city) or
14:               param.has_key?(:state) or
15:               param.has_key?(:zip) or
16:               param.has_key?(:location)
17:             raise MissingParameterException.new("Missing location data for the Yahoo! Maps Geocoding service")
18:           end
19:           
20:           url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=#{Ym4r::YahooMaps::APP_ID}&"
21:           url << "street=#{param[:street]}&" if param.has_key?(:street)
22:           url << "city=#{param[:city]}&" if param.has_key?(:city)
23:           url << "state=#{param[:state]}&" if param.has_key?(:state)
24:           url << "zip=#{param[:zip]}&" if param.has_key?(:zip)
25:           url << "location=#{param[:location]}&" if param.has_key?(:location)
26:           url << "output=xml"
27:           
28:           begin
29:             xml = open(URI.encode(url)).read
30:           rescue OpenURI::HTTPError => error
31:             raise BadRequestException.new(error.to_s)
32:           rescue
33:             raise ConnectionException.new("Unable to connect to Yahoo! Maps Geocoding service")
34:           end
35:           
36:           doc = REXML::Document.new(xml) 
37:           
38:           if doc.root.name == "Error"
39:             raise RateLimitExceededException.new("Rate limit exceeded for Yahoo! Maps Geocoding service")
40:           else
41:             results = []
42:             doc.elements.each("//Result") do |result|
43:               data = result.elements
44:               results << Geocoding::Result.new(result.attributes['precision'],
45:                                                result.attributes['warning'],
46:                                                data['Latitude'].text.to_f,
47:                                                data['Longitude'].text.to_f,
48:                                                data['Address'].text,
49:                                                data['City'].text,
50:                                                data['State'].text,
51:                                                data['Zip'].text,
52:                                                data['Country'].text)
53:             end
54:             results
55:           end
56:         end

[Validate]