Module: Cryptum::API::Signature

Defined in:
lib/cryptum/api/signature.rb

Overview

Module specifically related to orders history retrieval.

Class Method Summary collapse

Class Method Details

.generate(opts = {}) ⇒ Object

Obtain latest order history



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cryptum/api/signature.rb', line 9

public_class_method def self.generate(opts = {})
  api_secret = opts[:api_secret]

  http_method = if opts[:http_method].nil?
                  :GET
                else
                  opts[:http_method].to_s.upcase.scrub.strip.chomp.to_sym
                end

  api_call = opts[:api_call].to_s.scrub.strip.chomp
  api_call = '/users/self/verify' if opts[:api_call].nil?

  if opts[:params].nil?
    path = api_call
  else
    uri = Addressable::URI.new
    uri.query_values = opts[:params]
    params = uri.query
    path = "#{api_call}?#{params}"
  end

  http_body = opts[:http_body].to_s.scrub.strip.chomp

  api_timestamp = Time.now.utc.to_i.to_s

  api_signature = Base64.strict_encode64(
    OpenSSL::HMAC.digest(
      'sha256',
      Base64.strict_decode64(api_secret),
      "#{api_timestamp}#{http_method}#{path}#{http_body}"
    )
  )

  if http_body == ''
    api_signature = Base64.strict_encode64(
      OpenSSL::HMAC.digest(
        'sha256',
        Base64.strict_decode64(api_secret),
        "#{api_timestamp}#{http_method}#{path}"
      )
    )
  end

  api_signature_response = {}
  api_signature_response[:api_timestamp] = api_timestamp
  api_signature_response[:api_signature] = api_signature

  api_signature_response
rescue Interrupt, StandardError => e
  Cryptum::Log.append(level: :error, msg: e, which_self: self)
end

.helpObject

Display Usage for this Module



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cryptum/api/signature.rb', line 62

public_class_method def self.help
  puts "USAGE:
    signature = #{self}.generate_signature(
      api_secret: 'required - Coinbase Pro API Secret',
      http_method: 'optional - Defaults to :GET',
      api_call: 'optional - Defaults to /users/self/verify',
      params: 'optional - HTTP GET Parameters',
      http_body: 'optional HTTP POST Body'
    )
  "
end