Class: GoogleBusinessApiUrlSigner::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/google_business_api_url_signer/signer.rb

Overview

Public: Takes care of signing URLs

Google’s documentation for this can be found here: developers.google.com/maps/documentation/business/webservices#generating_valid_signatures

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Signer

Public: Initializes the signer

options - url must be given within the options,

private_key can be given, or you can set default value with:
GoogleBusinessApiUrlSigner::Signer.default_private_key = 'key'


22
23
24
25
26
# File 'lib/google_business_api_url_signer/signer.rb', line 22

def initialize(options = {})
  @url = options.fetch :url
  @private_key = options.fetch :private_key, default_private_key
  @private_key = default_private_key if @private_key.blank?
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/google_business_api_url_signer/signer.rb', line 14

def url
  @url
end

Instance Method Details

#private_keyObject



28
29
30
31
# File 'lib/google_business_api_url_signer/signer.rb', line 28

def private_key
  return @private_key if @private_key.present?
  fail MissingPrivateKeyError
end

#signatureObject

Public: Calculates the signature from the given URL and private key



34
35
36
# File 'lib/google_business_api_url_signer/signer.rb', line 34

def signature
  Base64.urlsafe_encode64 signature_digest
end

#signed_urlObject

Public: Calculates the signature and returns a signed version of the URL



39
40
41
42
43
44
45
46
47
48
# File 'lib/google_business_api_url_signer/signer.rb', line 39

def signed_url
  [
    parsed_url.scheme,
    '://',
    parsed_url.host,
    parsed_url.path,
    '?',
    query_params_as_string_with_signature
  ].join.html_safe
end