Class: Cryptomarket::CredentialsFactory
- Inherits:
-
Object
- Object
- Cryptomarket::CredentialsFactory
- Defined in:
- lib/cryptomarket/credentials_factory.rb
Overview
Builds a credential used by the cryptomarket server
Instance Method Summary collapse
- #build_credential_message(http_method, method, timestamp, params) ⇒ Object
- #change_credentials(api_key:, api_secret:) ⇒ Object
- #change_window(window:) ⇒ Object
- #get_credential(http_method, method, params) ⇒ Object
-
#initialize(api_version:, api_key:, api_secret:, window: nil) ⇒ CredentialsFactory
constructor
A new instance of CredentialsFactory.
- #not_post_params(http_method, params) ⇒ Object
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
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cryptomarket/credentials_factory.rb', line 41 def (http_method, method, , params) msg = http_method + @api_version + method msg += if http_method.upcase == 'POST' params else not_post_params(http_method, params) end msg += msg += @window.to_s unless @window.nil? msg end |
#change_credentials(api_key:, api_secret:) ⇒ Object
21 22 23 24 |
# File 'lib/cryptomarket/credentials_factory.rb', line 21 def change_credentials(api_key:, api_secret:) @api_key = api_key @api_secret = api_secret end |
#change_window(window:) ⇒ Object
26 27 28 |
# File 'lib/cryptomarket/credentials_factory.rb', line 26 def change_window(window:) @window = window end |
#get_credential(http_method, method, params) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cryptomarket/credentials_factory.rb', line 30 def get_credential(http_method, method, params) = DateTime.now.strftime('%Q') msg = (http_method, method, , params) digest = OpenSSL::Digest.new 'sha256' signature = OpenSSL::HMAC.hexdigest digest, @api_secret, msg signed = "#{@api_key}:#{signature}:#{}" signed += ":#{@window}" unless @window.nil? encoded = Base64.encode64(signed).delete "\n" "HS256 #{encoded}" end |
#not_post_params(http_method, params) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/cryptomarket/credentials_factory.rb', line 53 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 |