Class: OpenGraphPlus::Signature::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/opengraphplus/signature/generator.rb

Defined Under Namespace

Classes: InvalidAPIKeyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Generator

Returns a new instance of Generator.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/opengraphplus/signature/generator.rb', line 13

def initialize(api_key)
  @api_key = case api_key
  when APIKey
    api_key
  when String
    APIKey.parse(api_key)
  end

  raise InvalidAPIKeyError, "API key is missing or invalid" unless @api_key
  raise InvalidAPIKeyError, "API key is missing public_key" unless @api_key.public_key
  raise InvalidAPIKeyError, "API key is missing secret_key" unless @api_key.secret_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/opengraphplus/signature/generator.rb', line 11

def api_key
  @api_key
end

Instance Method Details

#generate(path_and_query) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/opengraphplus/signature/generator.rb', line 26

def generate(path_and_query)
  path_and_query
    .then { |data| OpenSSL::HMAC.digest(DIGEST_ALGORITHM, api_key.secret_key, data) }
    .then { |hmac| hmac.byteslice(0, HMAC_BYTES) }
    .then { |truncated| "#{api_key.public_key}:#{truncated}" }
    .then { |payload| Base64.urlsafe_encode64(payload, padding: false) }
end