Class: Cryptomarket::CredentialsFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptomarket/credentials_factory.rb

Overview

Builds a credential used by the cryptomarket server

Instance Method Summary collapse

Constructor Details

#initialize(api_version:, api_key:, api_secret:, window: nil) ⇒ CredentialsFactory

Returns a new instance of CredentialsFactory.



14
15
16
17
18
19
# File 'lib/cryptomarket/credentials_factory.rb', line 14

def initialize(api_version:, api_key:, api_secret:, window: nil)
  @api_version = api_version
  @api_key = api_key
  @api_secret = api_secret
  @window = window
end

Instance Method Details

#build_credential_message(http_method, method, timestamp, params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cryptomarket/credentials_factory.rb', line 32

def build_credential_message(http_method, method, timestamp, params)
  msg = http_method + @api_version + method
  msg += if http_method.upcase == 'POST'
           params
         else
           not_post_params(http_method, params)
         end
  msg += timestamp
  msg += @window unless @window.nil?
  msg
end

#get_credential(http_method, method, params) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/cryptomarket/credentials_factory.rb', line 21

def get_credential(http_method, method, params)
  timestamp = DateTime.now.strftime('%Q')
  msg = build_credential_message(http_method, method, timestamp, params)
  digest = OpenSSL::Digest.new 'sha256'
  signature = OpenSSL::HMAC.hexdigest digest, @api_secret, msg
  signed = "#{@api_key}:#{signature}:#{timestamp}"
  signed += ":#{@window}" unless @window.nil?
  encoded = Base64.encode64(signed).delete "\n"
  "HS256 #{encoded}"
end

#not_post_params(http_method, params) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/cryptomarket/credentials_factory.rb', line 44

def not_post_params(http_method, params)
  msg = ''
  if !params.nil? && params.keys.any?
    msg += '?' if http_method.upcase == 'GET'
    msg += URI.encode_www_form(params)
  end
  msg
end