Class: HmacSignature::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/hmac_signature/credentials.rb

Constant Summary collapse

HEADER_CREDENTIAL_REGEXES =
[/^x-auth-(.+)$/i, /^http_x_auth_(.+)$/i]
PARAM_CREDENTIAL_REGEX =
/^auth_(.+)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, expiry, version, signature) ⇒ Credentials

Returns a new instance of Credentials.



34
35
36
37
38
39
# File 'lib/hmac_signature/credentials.rb', line 34

def initialize key, expiry, version, signature
	@key = key 
	@expiry = expiry
	@version = version
	@signature = signature
end

Instance Attribute Details

#expiryObject (readonly)

Returns the value of attribute expiry.



32
33
34
# File 'lib/hmac_signature/credentials.rb', line 32

def expiry
  @expiry
end

#keyObject (readonly)

Returns the value of attribute key.



32
33
34
# File 'lib/hmac_signature/credentials.rb', line 32

def key
  @key
end

#signatureObject (readonly)

Returns the value of attribute signature.



32
33
34
# File 'lib/hmac_signature/credentials.rb', line 32

def signature
  @signature
end

#versionObject (readonly)

Returns the value of attribute version.



32
33
34
# File 'lib/hmac_signature/credentials.rb', line 32

def version
  @version
end

Class Method Details

.from_headers(headers = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hmac_signature/credentials.rb', line 17

def from_headers headers={}
	hash = headers.inject({}) do |memo, (k,v)|
		HEADER_CREDENTIAL_REGEXES.each do |regex|
			if match = k.match(regex)
				new_key = match[1].downcase.gsub('-', '_')
				memo[new_key] = v
				break
			end
		end
		memo
	end
	new hash['key'], hash['expiry'], hash['version'], hash['signature']
end

.from_params(params = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/hmac_signature/credentials.rb', line 7

def from_params params={}
	hash = params.inject({}) do |memo, (k,v)|
		if match = k.to_s.match(PARAM_CREDENTIAL_REGEX)
			memo[match[1]] = v
		end
		memo
	end
	new hash['key'], hash['expiry'], hash['version'], hash['signature']
end

Instance Method Details

#to_hashObject



41
42
43
# File 'lib/hmac_signature/credentials.rb', line 41

def to_hash
	{'auth_key' => key, 'auth_expiry' => expiry, 'auth_version' => version, 'auth_signature' => signature}
end

#to_headersObject



45
46
47
# File 'lib/hmac_signature/credentials.rb', line 45

def to_headers
	{'X-Auth-Key' => key, 'X-Auth-Expiry' => expiry, 'X-Auth-Version' => version, 'X-Auth-Signature' => signature}
end