Class: MapsApi::Google::Parser

Inherits:
AddressGeocoder::Parser show all
Defined in:
lib/maps_api/google/parser.rb

Overview

Class for parsing Google Maps API responses

Since:

  • 0.0.1

Constant Summary collapse

STREET_TYPES =

List of Google’s attribute title for streets

Since:

  • 0.0.1

{ values: %w(route), name: 'street' }.freeze
CITY_TYPES =

List of Google’s attribute title for cities

Since:

  • 0.0.1

{ values: %w(neighborhood locality sublocality),
name: 'city' }.freeze
STATE_TYPES =

List of Google’s attribute titles for states

Since:

  • 0.0.1

{ 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

Since:

  • 0.0.1

{ values: %w(postal_code postal_code_prefix),
name: 'postal_code' }.freeze

Instance Attribute Summary

Attributes inherited from AddressGeocoder::Parser

#address, #fields

Instance Method Summary collapse

Methods inherited from AddressGeocoder::Parser

#initialize

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

Returns:

  • (Boolean)

    true, or false if the city should have been used but

Since:

  • 0.0.1



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

Returns:

  • (Boolean)

Since:

  • 0.0.1



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

Returns:

  • (Boolean)

Since:

  • 0.0.1



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

Parameters:

  • field (Hash)

    one particular field from Google Maps’ response

Since:

  • 0.0.1



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_responseArray<Hash>

Convert Google Maps’ response into our format with the goal of finding several matching addresses

Returns:

  • (Array<Hash>)

    a list of matching addresses

Since:

  • 0.0.1



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

Returns:

  • (Boolean)

    true, or false if the postal code should have been

Since:

  • 0.0.1



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

Returns:

  • (Boolean)

    true, or false if the state should have been used but

Since:

  • 0.0.1



55
56
57
# File 'lib/maps_api/google/parser.rb', line 55

def state_present?(level)
  4 == level && @address[:state]
end