Class: Bibox::Rest::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Private::Contracts::Orders

#contract_orders

Methods included from Private::Contracts::Assets

#contract_assets

Methods included from Private::Contracts::General

#contract_values

Methods included from Private::Bills

#bills

Methods included from Private::Transfers

#deposit_address, #deposits, #transfers, #withdrawals

Methods included from Private::Assets

#assets, #coin_list

Methods included from Private::OrderBook

#extended_order_book

Methods included from Private::Orders

#buy, #cancel_order, #list_orders, #order_history, #pending_orders, #perform_order, #sell

Methods included from Public::Klines

#klines

Methods included from Public::OrderBook

#order_book

Methods included from Public::Trades

#trades

Methods included from Public::Pairs

#pair, #pairs

Methods included from Public::Ticker

#ticker

Methods included from Errors

#error?

Constructor Details

#initialize(configuration: ::Bibox.configuration) ⇒ Client

Returns a new instance of Client.



6
7
8
9
# File 'lib/bibox/rest/client.rb', line 6

def initialize(configuration: ::Bibox.configuration)
  self.url              =   "https://api.bibox.com/v1"
  self.configuration    =   configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



4
5
6
# File 'lib/bibox/rest/client.rb', line 4

def configuration
  @configuration
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/bibox/rest/client.rb', line 4

def url
  @url
end

Instance Method Details

#auth(payload = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bibox/rest/client.rb', line 56

def auth(payload = {})
  data              =   {}
  data[:cmds]       =   payload.to_json
  
  if configured?
    data[:apikey]   =   self.configuration.key
    data[:sign]     =   ::OpenSSL::HMAC.hexdigest('md5', self.configuration.secret, payload.to_json)
  end
  
  return data
end

#check_credentials!Object



33
34
35
36
37
# File 'lib/bibox/rest/client.rb', line 33

def check_credentials!
  unless configured?
    raise ::Bibox::Errors::MissingConfigError.new("Bibox gem hasn't been properly configured.")
  end
end

#configured?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/bibox/rest/client.rb', line 29

def configured?
  !self.configuration.key.to_s.empty? && !self.configuration.secret.to_s.empty?
end

#get(path, params: {}, options: {}) ⇒ Object



48
49
50
# File 'lib/bibox/rest/client.rb', line 48

def get(path, params: {}, options: {})
  request path, method: :get, params: params, options: options
end

#parse(response) ⇒ Object



43
44
45
46
# File 'lib/bibox/rest/client.rb', line 43

def parse(response)
  error?(response)
  response
end

#post(path, params: {}, data: {}, options: {}) ⇒ Object



52
53
54
# File 'lib/bibox/rest/client.rb', line 52

def post(path, params: {}, data: {}, options: {})
  request path, method: :post, params: params, data: auth(data), options: options
end

#request(path, method: :get, params: {}, data: {}, options: {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bibox/rest/client.rb', line 68

def request(path, method: :get, params: {}, data: {}, options: {})
  user_agent    =   options.fetch(:user_agent, self.configuration.faraday.fetch(:user_agent, nil))
  proxy         =   options.fetch(:proxy, nil)
    
  connection    =   Faraday.new(url: to_uri(path)) do |builder|
    builder.headers[:user_agent] = user_agent if !user_agent.to_s.empty?
    builder.request  :url_encoded if method.eql?(:post)
    builder.response :logger      if self.configuration.verbose_faraday?
    builder.response :json

    if proxy
      puts "[Bibox::Rest::Client] - Will connect to Bibox using proxy: #{proxy.inspect}" if self.configuration.verbose_faraday?
      builder.proxy = proxy
    end

    builder.adapter self.configuration.faraday.fetch(:adapter, :net_http)
  end
    
  case method
    when :get
      connection.get do |request|
        request.params  =   params if params && !params.empty?
      end&.body
    when :post
      connection.post do |request|
        request.body    =   data
        request.params  =   params if params && !params.empty?
      end&.body
  end
end

#to_uri(path) ⇒ Object



39
40
41
# File 'lib/bibox/rest/client.rb', line 39

def to_uri(path)
  "#{self.url}#{path}"
end