Class: SimpleConnect::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_connect/headers.rb

Overview

Builds the signed request headers (Content-Type + User-Agent + key id + timestamp + HMAC signature) for a given body. One instance per Client; holds the key_id and secret so callers never see them.

Constant Summary collapse

KEY_ID_HEADER =
"X-SimpleConnect-Key-Id"
TIMESTAMP_HEADER =
"X-SimpleConnect-Timestamp"
SIGNATURE_HEADER =
"X-SimpleConnect-Signature"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_id:, secret:, user_agent: nil) ⇒ Headers

Returns a new instance of Headers.



16
17
18
19
20
# File 'lib/simple_connect/headers.rb', line 16

def initialize(key_id:, secret:, user_agent: nil)
  @key_id     = key_id.to_s
  @secret     = secret.to_s
  @user_agent = (user_agent || self.class.default_user_agent).to_s
end

Class Method Details

.default_user_agentObject



12
13
14
# File 'lib/simple_connect/headers.rb', line 12

def self.default_user_agent
  "simple_connect-client/#{SimpleConnect::VERSION} ruby/#{RUBY_VERSION}"
end

Instance Method Details

#build_for(body) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_connect/headers.rb', line 22

def build_for(body)
  timestamp = Time.now.to_i.to_s
  {
    "Content-Type" => "application/json",
    "User-Agent" => @user_agent,
    KEY_ID_HEADER => @key_id,
    TIMESTAMP_HEADER => timestamp,
    SIGNATURE_HEADER => "sha256=#{compute_signature(timestamp, body)}"
  }
end