Class: YahooGeocoder

Inherits:
Geocoder show all
Defined in:
lib/kamelopard/geocode.rb

Overview

Uses Yahoo’s PlaceFinder geocoding service: developer.yahoo.com/geo/placefinder/guide/requests.html The argument to the constructor is a PlaceFinder API key, but testing suggests it’s actually unnecessary NB! This is deprecated, as Yahoo’s API is no longer free, and I’m not about to pay them to keep this tested. developer.yahoo.com/blogs/ydn/introducing-boss-geo-next-chapter-boss-53654.html

Instance Attribute Summary

Attributes inherited from Geocoder

#host, #params, #path

Instance Method Summary collapse

Methods inherited from Geocoder

#processParam

Constructor Details

#initialize(key) ⇒ YahooGeocoder

Returns a new instance of YahooGeocoder.



111
112
113
114
115
116
117
# File 'lib/kamelopard/geocode.rb', line 111

def initialize(key)
    @api_key = key
    @proto = 'http'
    @host = 'where.yahooapis.com'
    @path = '/geocode'
    @params = { 'appid' => @api_key, 'flags' => 'J' }
end

Instance Method Details

#lookup(address) ⇒ Object

Returns an object built from the JSON result of the lookup, or an exception



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kamelopard/geocode.rb', line 120

def lookup(address)
    # The argument can be a string, in which case PlaceFinder does the parsing
    # The argument can also be a hash, with several possible keys. See the PlaceFinder documentation for details
    # http://developer.yahoo.com/geo/placefinder/guide/requests.html
    http = Net::HTTP.new(@host)
    if address.kind_of? Hash then
        p = @params.merge address
    else
        p = @params.merge( { 'q' => address } )
    end
    q = p.map { |k,v| "#{ CGI.escape(k) }=#{ CGI.escape(v) }" }.join('&')
    u = URI::HTTP.build([nil, @host, nil, @path, q, nil])

    resp = Net::HTTP.get u
    parse_response resp
end

#parse_response(resp) ⇒ Object



137
138
139
140
141
# File 'lib/kamelopard/geocode.rb', line 137

def parse_response(resp)
    d = JSON.parse(resp)
    raise d['ErrorMessage'] if d['Error'].to_i != 0
    d
end