class MapsApi::Google::Requester
Class for making requests to Google
Maps API
Public Instance Methods
array_result()
click to toggle source
Return a compacted, flattened array of different address responses. @return (see AddressGeocoder::Requester#array_result
)
# File lib/maps_api/google/requester.rb, line 51 def array_result [@result['results']].flatten end
certain?()
click to toggle source
Check if the certainty level of the response @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.
# 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_call()
click to toggle source
Make a call to Google
Maps’ Geocoding API @return (see AddressGeocoder::Requester#make_call
)
# 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?()
click to toggle source
Determines whether the request to Google
Maps’ Geocoding API was a success @return (see AddressGeocoder::Requester#success?
)
# 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
Private Instance Methods
call()
click to toggle source
# File lib/maps_api/google/requester.rb, line 57 def call attempts = 0 begin @result = HTTParty.get(@url_generator.generate_url) rescue sleep(0.5) attempts += 1 retry if attempts <= 5 connection_error('Could not connect to GoogleAPI') end end