class Geokit::Geocoders::YahooGeocoder

Yahoo geocoder implementation. Requires the Geokit::Geocoders::YAHOO variable to contain a Yahoo API key. Conforms to the interface set by the Geocoder class.

Private Class Methods

do_geocode(address) click to toggle source

Template method which does the geocode lookup.

# File lib/geokit/geocoders/yahoo.rb, line 24
def self.do_geocode(address)
  process :json, submit_url(address)
end
extract_geoloc(result_json) click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 43
def self.extract_geoloc(result_json)
  loc = new_loc
  loc.lat      = result_json["latitude"]
  loc.lng      = result_json["longitude"]
  set_address_components(result_json, loc)
  set_precision(result_json, loc)
  loc.success  = true
  loc
end
parse_json(results) click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 28
def self.parse_json(results)
  boss_results = results && results["bossresponse"] && results["bossresponse"]["placefinder"] && results["bossresponse"]["placefinder"]["results"]
  return GeoLoc.new unless boss_results && boss_results.first
  loc = nil
  boss_results.each do |result|
    extracted_geoloc = extract_geoloc(result)
    if loc.nil?
      loc = extracted_geoloc
    else
      loc.all.push(extracted_geoloc)
    end
  end
  loc
end
set_address_components(result_json, loc) click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 53
def self.set_address_components(result_json, loc)
  loc.country_code   = result_json["countrycode"]
  loc.street_address = result_json["line1"].to_s.empty? ? nil : result_json["line1"]
  loc.city           = result_json["city"]
  loc.state          = loc.is_us? ? result_json["statecode"] : result_json["state"]
  loc.zip            = result_json["postal"]
end
set_precision(result_json, loc) click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 61
def self.set_precision(result_json, loc)
  loc.precision = case result_json["quality"].to_i
                  when 9, 10         then "country"
                  when 19..30       then "state"
                  when 39, 40        then "city"
                  when 49, 50        then "neighborhood"
                  when 59, 60, 64     then "zip"
                  when 74, 75        then "zip+4"
                  when 70..72       then "street"
                  when 80..87       then "address"
                  when 62, 63, 90, 99  then "building"
                  else "unknown"
                  end

  loc.accuracy = %w(unknown country state state city zip zip+4 street address building).index(loc.precision)
end
submit_url(address) click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 11
def self.submit_url(address)
  address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
  query_string = "?q=#{Geokit::Inflector.url_escape(address_str)}&flags=J"

  o = OauthUtil.new
  o.consumer_key = key
  o.consumer_secret = secret
  base = "#{protocol}://yboss.yahooapis.com/geo/placefinder"
  parsed_url = URI.parse("#{base}#{query_string}")
  "#{base}?#{o.sign(parsed_url).query_string}"
end