Module: Mexbt::Client

Includes:
RestClient
Included in:
Account, Public
Defined in:
lib/mexbt/client.rb

Constant Summary collapse

SSL_VERSION =
:TLSv1_2

Instance Method Summary collapse

Instance Method Details

#auth_paramsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mexbt/client.rb', line 24

def auth_params
  if public_key.nil? || private_key.nil?
    raise "You must configure your API keys!"
  end
  if user_id.nil?
    raise "You must configure your user_id!"
  end
  nonce = (Time.now.to_f*10000).to_i
  {
    apiKey: public_key,
    apiNonce: nonce,
    apiSig: OpenSSL::HMAC.hexdigest('sha256', private_key, "#{nonce}#{user_id}#{public_key}").upcase
  }
end

#call(path, params = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mexbt/client.rb', line 39

def call(path, params={})
  payload = params
  params.merge!(auth_params) if self.respond_to?(:private?)
  res = Request.execute(method: :post, url: url(path), payload: payload.to_json, ssl_version: SSL_VERSION)
  if res.length === 0
    raise "Empty response from API"
  end
  json_response = ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(res))
  if json_response[:isAccepted]
    json_response
  else
    raise json_response[:rejectReason]
  end
end

#call_data(path) ⇒ Object



54
55
56
57
# File 'lib/mexbt/client.rb', line 54

def call_data(path)
  res = Request.execute(method: :get, url: "https://data.mexbt.com/#{path}", ssl_version: SSL_VERSION)
  ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(res))
end

#private_keyObject



16
17
18
# File 'lib/mexbt/client.rb', line 16

def private_key
  @private_key || Mexbt.private_key
end

#public_keyObject



12
13
14
# File 'lib/mexbt/client.rb', line 12

def public_key
  @public_key || Mexbt.public_key
end

#url(path) ⇒ Object



8
9
10
# File 'lib/mexbt/client.rb', line 8

def url(path)
  "#{endpoint()}/v1/#{path}"
end

#user_idObject



20
21
22
# File 'lib/mexbt/client.rb', line 20

def user_id
  @user_id || Mexbt.user_id
end