class MapsApi::Google::Parser

Class for parsing Google Maps API responses

Constants

CITY_TYPES

List of Google’s attribute title for cities

POSTAL_TYPES

List of Google’s attribute titles for postal codes

STATE_TYPES

List of Google’s attribute titles for states

STREET_TYPES

List of Google’s attribute title for streets

Public Instance Methods

city_present?(level) click to toggle source

Check to see if the city should have been used in the call, and if so, was it? @return [Boolean] true, or false if the city should have been used but wasn’t or vice versa

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Takes a specific field and converts it into our format @param field [Hash] one particular field from Google Maps’ response @return [void]

# 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() click to toggle source

Convert Google Maps’ response into our format with the goal of finding several matching addresses @return (see AddressGeocoder::Parser#parse_response)

# 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) click to toggle source

Check to see if the postal code should have been used in the call, and if so, was it? @return [Boolean] true, or false if the postal code should have been used but wasn’t or vice versa

# 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) click to toggle source

Check to see if the state should have been used in the call, and if so, was it? @return [Boolean] true, or false if the state should have been used but wasn’t or vice versa

# File lib/maps_api/google/parser.rb, line 55
def state_present?(level)
  4 == level && @address[:state]
end

Private Instance Methods

add_city(field) click to toggle source
# File lib/maps_api/google/parser.rb, line 94
def add_city(field)
  if @city
    @state  = field['long_name']
    @switch = true
  else
    @city = field['long_name']
  end
end
add_postal_code(field) click to toggle source
# File lib/maps_api/google/parser.rb, line 116
def add_postal_code(field)
  @postal_code = field['long_name']
end
add_state(field) click to toggle source
# File lib/maps_api/google/parser.rb, line 103
def add_state(field)
  str = if field['types'].include? 'administrative_area_level_1'
          'short_name'
        else
          'long_name'
        end
  if @switch
    @city   = @state
    @switch = false
  end
  @state = field[str]
end
add_street(field) click to toggle source
# File lib/maps_api/google/parser.rb, line 90
def add_street(field)
  @street = field['long_name']
end
define_address() click to toggle source
# File lib/maps_api/google/parser.rb, line 81
def define_address
  { country: @address[:country], city: @city, state: @state,
    postal_code: @postal_code, street: @street }
end
similar?(array1, array2) click to toggle source
# File lib/maps_api/google/parser.rb, line 86
def similar?(array1, array2)
  (array1 & array2).any?
end