Class: MapsApi::Google::Requester

Inherits:
AddressGeocoder::Requester show all
Defined in:
lib/maps_api/google/requester.rb

Overview

Class for making requests to Google Maps API

Since:

  • 0.0.1

Instance Attribute Summary

Attributes inherited from AddressGeocoder::Requester

#address, #api_key, #language, #parser, #result

Instance Method Summary collapse

Methods inherited from AddressGeocoder::Requester

#connection_error, #initialize

Constructor Details

This class inherits a constructor from AddressGeocoder::Requester

Instance Method Details

#array_resultArray<Hash>

Return a compacted, flattened array of different address responses.

Returns:

  • (Array<Hash>)

    a collection of possible addresses

Since:

  • 0.0.1



51
52
53
# File 'lib/maps_api/google/requester.rb', line 51

def array_result
  [@result['results']].flatten
end

#certain?Boolean

Note:

certainty is determined in two ways: first, by ensuring that the country was not the only field returned and that it was the correct country; second, that the city, state, and postal code were all present in the response if they were included in the level of call.

Check if the certainty level of the response

Returns:

  • (Boolean)

Since:

  • 0.0.1



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/maps_api/google/requester.rb', line 36

def certain?
  level = @url_generator.level
  if @parser.just_country?(@result) ||
     @parser.not_correct_country?(@result)
    false
  elsif @parser.city_present?(level) || @parser.state_present?(level) ||
        @parser.pc_present?(level)
    false
  else
    true
  end
end

#make_callvoid

This method returns an undefined value.

Make a call to Google Maps’ Geocoding API

Since:

  • 0.0.1



11
12
13
14
15
16
17
18
19
20
# File 'lib/maps_api/google/requester.rb', line 11

def make_call
  @url_generator = UrlGenerator.new(address: @address.dup,
                                    api_key: @api_key,
                                    language: @language)
  @url_generator.levels.each do |level_of_search|
    @url_generator.level = level_of_search
    call
    break if success?
  end
end

#success?Boolean

Determines whether the request to Google Maps’ Geocoding API was a success

Returns:

  • (Boolean)
  • (Boolean)

    true, or false if the request failed.

Since:

  • 0.0.1



25
26
27
28
29
# File 'lib/maps_api/google/requester.rb', line 25

def success?
  return false unless @result['status'] == 'OK'
  return false unless @result['results'][0]['address_components'].length > 1
  true
end