Module: GoogleMapsService::Url

Defined in:
lib/google_maps_service/url.rb

Overview

Helper for handling URL.

Constant Summary collapse

UNRESERVED_SET =

The unreserved URI characters (RFC 3986)

"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"

Class Method Summary collapse

Class Method Details

.sign_hmac(secret, payload) ⇒ String

Returns a base64-encoded HMAC-SHA1 signature of a given string.

Parameters:

  • secret (String)

    The key used for the signature, base64 encoded.

  • payload (String)

    The payload to sign.

Returns:

  • (String)

    Base64-encoded HMAC-SHA1 signature



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/google_maps_service/url.rb', line 15

def sign_hmac(secret, payload)
  secret = secret.encode("ASCII")
  payload = payload.encode("ASCII")

  # Decode the private key
  raw_key = Base64.urlsafe_decode64(secret)

  # Create a signature using the private key and the URL
  digest = OpenSSL::Digest.new("sha1")
  raw_signature = OpenSSL::HMAC.digest(digest, raw_key, payload)

  # Encode the signature into base64 for url use form.
  Base64.urlsafe_encode64(raw_signature)
end

.unquote_unreserved(uri) ⇒ String

Un-escape any percent-escape sequences in a URI that are unreserved characters. This leaves all reserved, illegal and non-ASCII bytes encoded.

Parameters:

  • uri (String)

Returns:

  • (String)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/google_maps_service/url.rb', line 43

def unquote_unreserved(uri)
  parts = uri.split("%")

  (1..parts.length - 1).each do |i|
    h = parts[i][0..1]

    parts[i] = if h =~ (/^(\h{2})(.*)/) && (c = $1.to_i(16).chr) && UNRESERVED_SET.include?(c)
      c + $2
    else
      "%" + parts[i]
    end
  end

  parts.join
end

.urlencode_params(params) ⇒ String

URL encodes the parameters.

Parameters:

  • params (Hash, Array<Array>)

    The parameters

Returns:

  • (String)


33
34
35
# File 'lib/google_maps_service/url.rb', line 33

def urlencode_params(params)
  unquote_unreserved(URI.encode_www_form(params))
end