Class: YahooGeocoder
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
Instance Method Summary collapse
-
#initialize(key) ⇒ YahooGeocoder
constructor
A new instance of YahooGeocoder.
-
#lookup(address) ⇒ Object
Returns an object built from the JSON result of the lookup, or an exception.
- #parse_response(resp) ⇒ Object
Constructor Details
#initialize(key) ⇒ YahooGeocoder
Returns a new instance of YahooGeocoder.
71 72 73 74 75 76 77 |
# File 'lib/kamelopard/geocode.rb', line 71 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kamelopard/geocode.rb', line 80 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
97 98 99 100 101 |
# File 'lib/kamelopard/geocode.rb', line 97 def parse_response(resp) d = JSON.parse(resp) raise d['ErrorMessage'] if d['Error'].to_i != 0 d end |