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

Constructor Details

#initialize(key) ⇒ YahooGeocoder

Returns a new instance of YahooGeocoder.



70
71
72
73
74
75
76
# File 'lib/kamelopard/geocode.rb', line 70

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kamelopard/geocode.rb', line 79

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



96
97
98
99
100
# File 'lib/kamelopard/geocode.rb', line 96

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