Class: MapsApi::Google::Parser
- Inherits:
-
AddressGeocoder::Parser
- Object
- AddressGeocoder::Parser
- MapsApi::Google::Parser
- Defined in:
- lib/maps_api/google/parser.rb
Overview
Class for parsing Google Maps API responses
Constant Summary collapse
- STREET_TYPES =
List of Google’s attribute title for streets
{ values: %w(route), name: 'street' }.freeze
- CITY_TYPES =
List of Google’s attribute title for cities
{ values: %w(neighborhood locality sublocality), name: 'city' }.freeze
- STATE_TYPES =
List of Google’s attribute titles for states
{ values: %w(administrative_area_level_4 administrative_area_level_3 administrative_area_level_2 administrative_area_level_1), name: 'state' }.freeze
- POSTAL_TYPES =
List of Google’s attribute titles for postal codes
{ values: %w(postal_code postal_code_prefix), name: 'postal_code' }.freeze
Instance Attribute Summary
Attributes inherited from AddressGeocoder::Parser
Instance Method Summary collapse
-
#city_present?(level) ⇒ Boolean
Check to see if the city should have been used in the call, and if so, was it? wasn’t or vice versa.
- #just_country?(google_response) ⇒ Boolean
- #not_correct_country?(google_response) ⇒ Boolean
-
#parse_field(field) ⇒ void
Takes a specific field and converts it into our format.
-
#parse_response ⇒ Array<Hash>
Convert Google Maps’ response into our format with the goal of finding several matching addresses.
-
#pc_present?(level) ⇒ Boolean
Check to see if the postal code should have been used in the call, and if so, was it? used but wasn’t or vice versa.
-
#state_present?(level) ⇒ Boolean
Check to see if the state should have been used in the call, and if so, was it? wasn’t or vice versa.
Methods inherited from AddressGeocoder::Parser
Constructor Details
This class inherits a constructor from AddressGeocoder::Parser
Instance Method Details
#city_present?(level) ⇒ Boolean
Check to see if the city should have been used in the call, and if so, was it? wasn’t or vice versa
47 48 49 |
# File 'lib/maps_api/google/parser.rb', line 47 def city_present?(level) [3, 4, 7].include?(level) && @address[:city] end |
#just_country?(google_response) ⇒ Boolean
67 68 69 |
# File 'lib/maps_api/google/parser.rb', line 67 def just_country?(google_response) google_response['results'][0]['address_components'].count == 1 end |
#not_correct_country?(google_response) ⇒ Boolean
71 72 73 74 75 76 77 |
# File 'lib/maps_api/google/parser.rb', line 71 def not_correct_country?(google_response) components = google_response['results'] components = components[0]['address_components'] !(components.select do |x| x['short_name'] == @address[:country][:alpha2] end).any? end |
#parse_field(field) ⇒ void
This method returns an undefined value.
Takes a specific field and converts it into our format
35 36 37 38 39 40 41 |
# File 'lib/maps_api/google/parser.rb', line 35 def parse_field(field) [STREET_TYPES, CITY_TYPES, STATE_TYPES, POSTAL_TYPES].each do |type| if similar?(field['types'], type[:values]) send("add_#{type[:name]}", field) end end end |
#parse_response ⇒ Array<Hash>
Convert Google Maps’ response into our format with the goal of finding several matching addresses
25 26 27 28 29 30 |
# File 'lib/maps_api/google/parser.rb', line 25 def parse_response @fields['address_components'].each do |field| parse_field(field) end define_address end |
#pc_present?(level) ⇒ Boolean
Check to see if the postal code should have been used in the call, and if so, was it? used but wasn’t or vice versa
63 64 65 |
# File 'lib/maps_api/google/parser.rb', line 63 def pc_present?(level) [5, 6, 7].include?(level) && @address[:postal_code] end |
#state_present?(level) ⇒ Boolean
Check to see if the state should have been used in the call, and if so, was it? wasn’t or vice versa
55 56 57 |
# File 'lib/maps_api/google/parser.rb', line 55 def state_present?(level) 4 == level && @address[:state] end |