Class: Lookup::GoogleLookup

Inherits:
Base
  • Object
show all
Defined in:
lib/lookup/google_lookup.rb

Constant Summary collapse

GEO_HASH =
{
  'city'     => 'locality',
  'state'    => 'administrative_area_level_1',
  'county'   => 'administrative_area_level_2',
  'country'  => 'country'
}

Instance Attribute Summary collapse

Attributes inherited from Base

#geo_obj, #response, #response_obj, #zipcode

Instance Method Summary collapse

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, options = {})
  super
  @api_key = options[:api_key]
  @region  = options[:region]
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/lookup/google_lookup.rb', line 5

def api_key
  @api_key
end

#regionObject

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_detailsObject



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_responseObject



30
31
32
# File 'lib/lookup/google_lookup.rb', line 30

def fetch_response
  self.response = JSON.parse(response)['results'].first
end

#processObject



24
25
26
27
28
# File 'lib/lookup/google_lookup.rb', line 24

def process
  super
  fetch_response
  build_geo_details
end

#search_urlObject



20
21
22
# File 'lib/lookup/google_lookup.rb', line 20

def search_url
  "http://maps.googleapis.com/maps/api/geocode/json?address=#{zipcode}&region=#{region}&key=#{api_key}"
end