Class: Lookup::GoogleLookup
Constant Summary collapse
- GEO_HASH =
{ 'city' => 'locality', 'state' => 'administrative_area_level_1', 'county' => 'administrative_area_level_2', 'country' => 'country' }
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#region ⇒ Object
Returns the value of attribute region.
Attributes inherited from Base
#geo_obj, #response, #response_obj, #zipcode
Instance Method Summary collapse
- #build_geo_details ⇒ Object
- #fetch_address_component(component_name) ⇒ Object
- #fetch_response ⇒ Object
-
#initialize(zipcode, options = {}) ⇒ GoogleLookup
constructor
A new instance of GoogleLookup.
- #process ⇒ Object
- #search_url ⇒ Object
Methods inherited from Base
#api_request, #parsed_url, process
Constructor Details
#initialize(zipcode, options = {}) ⇒ GoogleLookup
Returns a new instance of GoogleLookup.
14 15 16 17 18 |
# File 'lib/lookup/google_lookup.rb', line 14 def initialize(zipcode, = {}) super @api_key = [:api_key] @region = [:region] end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
5 6 7 |
# File 'lib/lookup/google_lookup.rb', line 5 def api_key @api_key end |
#region ⇒ Object
Returns the value of attribute region.
5 6 7 |
# File 'lib/lookup/google_lookup.rb', line 5 def region @region end |
Instance Method Details
#build_geo_details ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/lookup/google_lookup.rb', line 34 def build_geo_details self.response_obj = OpenStruct.new(response) self.geo_obj = OpenStruct.new GEO_HASH.keys.each do |key| address_component_value = fetch_address_component(GEO_HASH[key]) geo_obj.send("#{key}=", address_component_value) end geo_obj end |
#fetch_address_component(component_name) ⇒ Object
44 45 46 47 |
# File 'lib/lookup/google_lookup.rb', line 44 def fetch_address_component(component_name) component_name = response_obj.address_components.find { |x| x['types'].include?(component_name) } component_name and component_name['long_name'] end |
#fetch_response ⇒ Object
30 31 32 |
# File 'lib/lookup/google_lookup.rb', line 30 def fetch_response self.response = JSON.parse(response)['results'].first end |
#process ⇒ Object
24 25 26 27 28 |
# File 'lib/lookup/google_lookup.rb', line 24 def process super fetch_response build_geo_details end |
#search_url ⇒ Object
20 21 22 |
# File 'lib/lookup/google_lookup.rb', line 20 def search_url "http://maps.googleapis.com/maps/api/geocode/json?address=#{zipcode}®ion=#{region}&key=#{api_key}" end |