Class: BitcointradeSDK::Base

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

Constant Summary collapse

BASE_URL =
'https://api.bitcointrade.com.br/v1'

Instance Method Summary collapse

Instance Method Details

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bitcointrade_sdk/base.rb', line 9

def send_request(verb, path, options = {}, public_response = false)
  timestamp = (Time.now.to_f * 1000).round.to_s
  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", "")}"

  headers = { content_type:  'application/json' }
  headers[:authorization] = "ApiToken #{BitcointradeSDK.api_token}" unless public_response

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

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