Class: Etna::Hmac

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/hmac.rb

Direct Known Subclasses

TestHmac

Constant Summary collapse

SIGN_ITEMS =

These are the items that need to be signed

[ :method, :host, :path, :expiration, :nonce, :id, :headers ]

Instance Method Summary collapse

Constructor Details

#initialize(application, params) ⇒ Hmac

Returns a new instance of Hmac.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/etna/hmac.rb', line 7

def initialize application, params
  @application = application

  @test_signature = params.delete(:test_signature)

  SIGN_ITEMS.each do |item|
    raise ArgumentError, "Hmac requires param #{item}" unless params[item]
    instance_variable_set("@#{item}", params[item])
  end

  @id = @id.to_sym

  raise ArgumentError, "Headers must be a Hash" unless @headers.is_a?(Hash)
end

Instance Method Details

#signatureObject



48
49
50
# File 'lib/etna/hmac.rb', line 48

def signature
  @application.sign.hmac(text_to_sign, @application.config(:hmac_keys)[@id])
end

#url_params(with_headers = true) ⇒ Object

this returns arguments for URI::HTTP.build



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/etna/hmac.rb', line 23

def url_params(with_headers=true)
  params = {
    signature: signature,
    expiration: @expiration,
    nonce: @nonce,
    id: @id.to_s,
    headers: @headers.keys.join(','),
  }.merge(with_headers ? @headers : {}).map do |name, value|
    [
      "X-Etna-#{ name.to_s.split(/_/).map(&:capitalize).join('-') }",
      value
    ]
  end.to_h

  return {
    host: @host,
    path: @path,
    query: URI.encode_www_form(params)
  }
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/etna/hmac.rb', line 44

def valid?
  valid_id? && valid_signature? && valid_timestamp?
end