Class: ItBitSDK::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/it_bit_sdk/base.rb

Constant Summary collapse

BASE_URL =
"https://api.itbit.com/v1"

Instance Method Summary collapse

Instance Method Details

#next_nonceObject



14
15
16
# File 'lib/it_bit_sdk/base.rb', line 14

def next_nonce
  timestamp
end

#send_request(verb, path, options = {}) ⇒ Object



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
# File 'lib/it_bit_sdk/base.rb', line 18

def send_request(verb, path, options = {})
  timestamp = (Time.now.to_f * 1000).round.to_s
  nonce     = next_nonce
  payload   = options.empty? || verb == :get ? nil : JSON.dump(options)
  query     = options.any? && verb == :get ? "?#{URI.encode_www_form(options)}" : ''
  url       = "#{BASE_URL}#{path}#{query.gsub("%E2%80%8B", "")}"

  signature = ::ItBitSDK::MessageSigner.new.sign_message(verb, url, payload, nonce, timestamp)
  headers = {
    authorization: "#{ItBitSDK.client_key}:#{signature}",
    x_auth_timestamp: timestamp,
    x_auth_nonce: nonce,
    content_type: 'application/json'
  }

  begin
    response = RestClient::Request.execute(
      :method => verb,
      :url => url,
      :payload => payload,
      :headers => headers,
      :ssl_version => 'SSLv23')
  rescue => e
    response = e.response.body
  end

  JSON.parse(response.to_str) if response.to_s
end

#timestampObject



10
11
12
# File 'lib/it_bit_sdk/base.rb', line 10

def timestamp
  Time.now.to_i
end