Class: CEX::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, api_key, api_secret) ⇒ API

Returns a new instance of API.



15
16
17
18
19
# File 'lib/cexio.rb', line 15

def initialize(username, api_key, api_secret)
  self.username = username
  self.api_key = api_key
  self.api_secret = api_secret
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



13
14
15
# File 'lib/cexio.rb', line 13

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



13
14
15
# File 'lib/cexio.rb', line 13

def api_secret
  @api_secret
end

#nonce_vObject

Returns the value of attribute nonce_v.



13
14
15
# File 'lib/cexio.rb', line 13

def nonce_v
  @nonce_v
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/cexio.rb', line 13

def username
  @username
end

Instance Method Details

#api_call(method, param = {}, priv = false, couple = '') ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/cexio.rb', line 21

def api_call(method, param = {}, priv = false, couple = '')
  url = 'https://cex.io/api/'+method+'/'
  url = url + couple + '/' if couple != ''
  if priv
    self.nonce
    param.merge!(:key => self.api_key, :signature => self.signature.to_s, :nonce => self.nonce_v)
  end
  answer = self.post(url, param)
  JSON.parse(answer)
end

#balanceObject



44
45
46
# File 'lib/cexio.rb', line 44

def balance
  self.api_call('balance', {}, true)
end

#cancel_order(order_id) ⇒ Object



52
53
54
# File 'lib/cexio.rb', line 52

def cancel_order(order_id)
  self.api_call('cancel_order', {:id => order_id.to_s}, true)
end

#nonceObject



62
63
64
# File 'lib/cexio.rb', line 62

def nonce
  self.nonce_v = Time.now.to_i.to_s
end

#open_orders(couple = 'GHS/BTC') ⇒ Object



48
49
50
# File 'lib/cexio.rb', line 48

def open_orders(couple = 'GHS/BTC')
  self.api_call('open_orders', {}, true, couple)
end

#order_book(couple = 'GHS/BTC') ⇒ Object



36
37
38
# File 'lib/cexio.rb', line 36

def order_book(couple = 'GHS/BTC')
  self.api_call('order_book', {}, false, couple)
end

#place_order(ptype = 'buy', amount = 1, price = 1, couple = 'GHS/BTC') ⇒ Object



56
57
58
# File 'lib/cexio.rb', line 56

def place_order(ptype = 'buy', amount = 1, price =1, couple = 'GHS/BTC')
  self.api_call('place_order', {:type => ptype, :amount => amount.to_s, :price => price.to_s}, true, couple)
end

#post(url, param) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/cexio.rb', line 71

def post(url, param)
  uri = URI.parse(url)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  params = Addressable::URI.new
  params.query_values = param
  https.post(uri.path, params.query).body
end

#signatureObject



66
67
68
69
# File 'lib/cexio.rb', line 66

def signature
  str = self.nonce_v + self.username + self.api_key
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), self.api_secret, str)
end

#ticker(couple = 'GHS/BTC') ⇒ Object



32
33
34
# File 'lib/cexio.rb', line 32

def ticker(couple = 'GHS/BTC')
  self.api_call('ticker', {}, false, couple)
end

#trade_history(since = 1, couple = 'GHS/BTC') ⇒ Object



40
41
42
# File 'lib/cexio.rb', line 40

def trade_history(since = 1, couple = 'GHS/BTC')
  self.api_call('trade_history', {:since => since.to_s}, false, couple)
end