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



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

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.
  signature =  Base64.urlsafe_encode64(raw_signature)
  return 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)


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

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

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

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

  parts.join
end

.urlencode_params(params) ⇒ String

URL encodes the parameters.

Parameters:

  • params (Hash, Array<Array>)

    The parameters

Returns:

  • (String)


35
36
37
# File 'lib/google_maps_service/url.rb', line 35

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