Class: Geokit::Geocoders::YahooGeocoder

Inherits:
Geocoder show all
Defined in:
lib/geokit/geocoders/yahoo.rb

Overview

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

Class Method Summary collapse

Methods inherited from Geocoder

call_geocoder_service, config, do_get, do_reverse_geocode, geocode, inherited, logger, net_adapter, new_loc, parse, process, protocol, provider_name, reverse_geocode, set_mappings, use_https?

Class Method Details

.do_geocode(address, _ = nil) ⇒ Object

Template method which does the geocode lookup.



24
25
26
# File 'lib/geokit/geocoders/yahoo.rb', line 24

def self.do_geocode(address, _=nil)
  process :json, submit_url(address)
end

.extract_geoloc(result_json) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# 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