class Google::Maps::API
Constants
- STATUS_OK
- STATUS_ZERO_RESULTS
Public Class Methods
query(service, args = {})
click to toggle source
# File lib/google_maps/api.rb, line 24 def query(service, args = {}) args = args.merge(Google::Maps.default_params[service]) if Google::Maps.default_params[service] url = url(service, args) response(url) end
Private Class Methods
add_digital_signature(url)
click to toggle source
# File lib/google_maps/api.rb, line 40 def add_digital_signature(url) parsed_url = url.is_a?(URI) ? url : URI.parse(url) url_to_sign = parsed_url.path + '?' + parsed_url.query # Decode the private key raw_key = decode_url_safe_base_64(Google::Maps.client_secret) # create a signature using the private key and the URL sha1 = HMAC::SHA1.new(raw_key) sha1 << url_to_sign raw_sig = sha1.digest # encode the signature into base64 for url use form. signature = encode_url_safe_base_64(raw_sig) # prepend the server and append the signature. "#{parsed_url.scheme}://#{parsed_url.host}#{url_to_sign}&signature=#{signature}".strip end
base_url(service, args = {})
click to toggle source
# File lib/google_maps/api.rb, line 92 def base_url(service, args = {}) url = URI.parse("#{Google::Maps.end_point}#{Google::Maps.send(service)}/#{Google::Maps.format}#{query_string(args)}") Google::Maps.logger.debug("url before possible signing: #{url}") url.to_s end
decode_url_safe_base_64(value)
click to toggle source
# File lib/google_maps/api.rb, line 32 def decode_url_safe_base_64(value) Base64.decode64(value.tr('-_', '+/')) end
encode_url_safe_base_64(value)
click to toggle source
# File lib/google_maps/api.rb, line 36 def encode_url_safe_base_64(value) Base64.encode64(value).tr('+/', '-_') end
handle_result_status(status)
click to toggle source
# File lib/google_maps/api.rb, line 70 def handle_result_status(status) raise ZeroResultsException, "Google did not return any results: #{status}" if status == STATUS_ZERO_RESULTS raise InvalidResponseException, "Google returned an error status: #{status}" if status != STATUS_OK end
query_string(args = {})
click to toggle source
# File lib/google_maps/api.rb, line 98 def query_string(args = {}) '?' + URI.encode_www_form(args) unless args.empty? end
response(url)
click to toggle source
# File lib/google_maps/api.rb, line 59 def response(url) begin result = Google::Maps::Result.new JSON.parse(HTTPClient.new.get_content(url)) rescue StandardError => e Google::Maps.logger.error e.message.to_s raise InvalidResponseException, "unknown error: #{e.message}" end handle_result_status(result.status) result end
url(service, args = {})
click to toggle source
# File lib/google_maps/api.rb, line 84 def url(service, args = {}) if Google::Maps.authentication_mode == Google::Maps::Configuration::API_KEY url_with_api_key(service, args) elsif Google::Maps.authentication_mode == Google::Maps::Configuration::DIGITAL_SIGNATURE url_with_digital_signature(service, args) end end
url_with_api_key(service, args = {})
click to toggle source
# File lib/google_maps/api.rb, line 75 def url_with_api_key(service, args = {}) base_url(service, args.merge(key: Google::Maps.api_key)) end
url_with_digital_signature(service, args = {})
click to toggle source
# File lib/google_maps/api.rb, line 79 def url_with_digital_signature(service, args = {}) url = base_url(service, args.merge(client: Google::Maps.client_id)) add_digital_signature(url) end