Skip to content
Snippets Groups Projects
Commit 02bdc9e7 authored by Alex Reisner's avatar Alex Reisner
Browse files

Add support for Yahoo geocoding.

parent 8f5df18b
No related branches found
No related tags found
No related merge requests found
require 'geocoder/lookups/base'
require "geocoder/results/yahoo"
module Geocoder::Lookup
class Yahoo < Base
private # ---------------------------------------------------------------
##
# Returns a parsed Yahoo geocoder search result (hash).
# Returns nil if non-200 HTTP response, timeout, or other error.
#
def results(query, reverse = false)
doc = fetch_data(query, reverse)
if doc = doc['ResultSet'] and doc['Error'] == 0
doc['Results']
else
warn "Yahoo Geocoding API error: #{doc['Error']} (#{doc['ErrorMessage']})."
end
end
def query_url(query, reverse = false)
params = {
:location => query,
:flags => "JXTSR",
:gflags => "AC#{'R' if reverse}",
:appid => Geocoder::Configuration.yahoo_appid
}
"http://where.yahooapis.com/geocode?" + params.to_query
end
end
end
require 'geocoder/results/base'
module Geocoder::Result
class Yahoo < Base
def coordinates
[latitude.to_f, longitude.to_f]
end
def address(format = :full)
(1..3).to_a.map{ |i| @data["line#{i}"] }.reject{ |i| i.nil? or i == "" }.join(", ")
end
def self.yahoo_attributes
%w[quality latitude longitude offsetlat offsetlon radius boundingbox name
line1 line2 line3 line4 cross house street xstreet unittype unit postal
neighborhood city county state country countrycode statecode countycode
level0 level1 level2 level3 level4 level0code level1code level2code
timezone areacode uzip hash woeid woetype]
end
yahoo_attributes.each do |a|
define_method a do
@data[a]
end
end
end
end
{
"ResultSet":{
"version":"1.0",
"Error":0,
"ErrorMessage":"No error",
"Locale":"us_US",
"Quality":90,
"Found":1,
"Results":[{
"quality":90,
"latitude":"40.750381",
"longitude":"-73.993988",
"offsetlat":"40.750381",
"offsetlon":"-73.993988",
"radius":100,
"name":"Madison Square Garden",
"line1":"Madison Square Garden",
"line2":"New York, NY 10001",
"line3":"",
"line4":"United States",
"house":"",
"street":"",
"xstreet":"",
"unittype":"",
"unit":"",
"postal":"10001",
"neighborhood":"",
"city":"New York",
"county":"New York County",
"state":"New York",
"country":"United States",
"countrycode":"US",
"statecode":"NY",
"countycode":"",
"uzip":"10001",
"hash":"",
"woeid":23617041,
"woetype":20
}]
}
}
......@@ -2,6 +2,11 @@ require 'test_helper'
class GeocoderTest < Test::Unit::TestCase
def setup
Geocoder::Configuration.lookup = :google
Geocoder.set_lookup :google
end
def test_fetch_coordinates
v = Venue.new(*venue_params(:msg))
assert_equal [40.750354, -73.993371], v.fetch_coordinates
......@@ -40,4 +45,20 @@ class GeocoderTest < Test::Unit::TestCase
v.fetch_coordinates
end
end
# --- Yahoo ---
def test_yahoo_result_components
Geocoder::Configuration.lookup = :yahoo
Geocoder.set_lookup :yahoo
results = Geocoder.search("Madison Square Garden, New York, NY")
assert_equal "10001", results.first.postal
end
def test_yahoo_address_formatting
Geocoder::Configuration.lookup = :yahoo
Geocoder.set_lookup :yahoo
results = Geocoder.search("Madison Square Garden, New York, NY")
assert_equal "Madison Square Garden, New York, NY 10001",
results.first.address
end
end
......@@ -40,13 +40,13 @@ require "geocoder/lookups/base"
#
module Geocoder
module Lookup
class Base
private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false)
File.read(File.join("test", "fixtures", "madison_square_garden.json"))
class Base
private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false)
File.read(File.join("test", "fixtures", "#{Geocoder::Configuration.lookup}_madison_square_garden.json"))
end
end
end
end
end
##
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment