Class: Cryptsy::Client

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  url: 'https://api.cryptsy.com',
  ssl: {
    verify: false
  }
}
ORDER_TYPE_BUY =
'Buy'
ORDER_TYPE_SELL =
'Sell'

Instance Method Summary collapse

Constructor Details

#initialize(public_key, private_key, options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • public_key (String)
  • private_key (String)
  • options (Hash) (defaults to: {})


21
22
23
24
25
26
# File 'lib/cryptsy/client.rb', line 21

def initialize(public_key, private_key, options = {})
  @public_key = public_key
  @private_key = private_key
  @digest = OpenSSL::Digest::SHA512.new
  @connection = Faraday.new(DEFAULT_OPTIONS.merge(options))
end

Instance Method Details

#all_ordersObject



47
48
49
# File 'lib/cryptsy/client.rb', line 47

def all_orders
  call(:allmyorders)
end

#all_tradesObject



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

def all_trades
  call(:allmytrades)
end

#calculate_buy_fees(quantity, price) ⇒ Object



103
104
105
# File 'lib/cryptsy/client.rb', line 103

def calculate_buy_fees(quantity, price)
  calculate_fees(ORDER_TYPE_BUY, quantity, price)
end

#calculate_fees(order_type, quantity, price) ⇒ Object



111
112
113
# File 'lib/cryptsy/client.rb', line 111

def calculate_fees(order_type, quantity, price)
  call(:calculatefees, ordertype: order_type, quantity: quantity, price: price)
end

#calculate_sell_fees(quantity, price) ⇒ Object



107
108
109
# File 'lib/cryptsy/client.rb', line 107

def calculate_sell_fees(quantity, price)
  calculate_fees(ORDER_TYPE_SELL, quantity, price)
end

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

Parameters:

  • method_name (Symbol)
  • params (Hash) (defaults to: {})

Returns:

  • (Object)

Raises:

  • (CryptsyError)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cryptsy/client.rb', line 131

def call(method_name, params = {})
  request = {
    method: method_name,
    nonce: Time.now.to_i
  }.merge(params)

  body = URI.encode_www_form(request)
  signature = OpenSSL::HMAC.hexdigest(@digest, @private_key, body)

  response = @connection.post do |req|
    req.url 'api'
    req.body = body

    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.headers['Key'] = @public_key
    req.headers['Sign'] = signature
  end

  process_response(response)
end

#cancel_all_ordersObject



99
100
101
# File 'lib/cryptsy/client.rb', line 99

def cancel_all_orders
  call(:cancelallorders)
end

#cancel_order(order_id) ⇒ Object



91
92
93
# File 'lib/cryptsy/client.rb', line 91

def cancel_order(order_id)
  call(:cancelorder, orderid: order_id)
end

#cancel_orders(market_id) ⇒ Object



95
96
97
# File 'lib/cryptsy/client.rb', line 95

def cancel_orders(market_id)
  call(:cancelmarketorders, marketid: market_id)
end

#create_buy_order(market_id, quantity, price) ⇒ Object



79
80
81
# File 'lib/cryptsy/client.rb', line 79

def create_buy_order(market_id, quantity, price)
  create_order(market_id, ORDER_TYPE_BUY, quantity, price)
end

#create_order(market_id, order_type, quantity, price) ⇒ Object



87
88
89
# File 'lib/cryptsy/client.rb', line 87

def create_order(market_id, order_type, quantity, price)
  call(:createorder, marketid: market_id, ordertype: order_type, quantity: quantity, price: price)
end

#create_sell_order(market_id, quantity, price) ⇒ Object



83
84
85
# File 'lib/cryptsy/client.rb', line 83

def create_sell_order(market_id, quantity, price)
  create_order(market_id, ORDER_TYPE_SELL, quantity, price)
end

#generate_new_address(currency) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/cryptsy/client.rb', line 115

def generate_new_address(currency)
  if currency.is_a?(Integer)
    call(:generatenewaddress, currencyid: currency).address
  else
    call(:generatenewaddress, currencycode: normalize_currency_code(currency)).address
  end
end

#infoObject



28
29
30
# File 'lib/cryptsy/client.rb', line 28

def info
  call(:getinfo)
end

#make_withdrawal(address, amount) ⇒ Object



123
124
125
# File 'lib/cryptsy/client.rb', line 123

def make_withdrawal(address, amount)
  call(:makewithdrawal, address: address, amount: amount)
end

#market_by_pair(primary_code, secondary_code) ⇒ Object



36
37
38
39
40
41
# File 'lib/cryptsy/client.rb', line 36

def market_by_pair(primary_code, secondary_code)
  markets.find do |market|
    market.primary_currency_code == normalize_currency_code(primary_code) &&
      market.secondary_currency_code == normalize_currency_code(secondary_code)
  end
end

#market_depth(market_id) ⇒ Object



67
68
69
# File 'lib/cryptsy/client.rb', line 67

def market_depth(market_id)
  call(:depth, marketid: market_id)
end

#market_orders(market_id) ⇒ Object



71
72
73
# File 'lib/cryptsy/client.rb', line 71

def market_orders(market_id)
  call(:marketorders, marketid: market_id)
end

#market_trades(market_id) ⇒ Object



75
76
77
# File 'lib/cryptsy/client.rb', line 75

def market_trades(market_id)
  call(:markettrades, marketid: market_id)
end

#marketsObject



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

def markets
  call(:getmarkets)
end

#normalize_currency_code(code) ⇒ String

Parameters:

  • code (Object)

Returns:

  • (String)


167
168
169
# File 'lib/cryptsy/client.rb', line 167

def normalize_currency_code(code)
  code.to_s.upcase
end

#orders(market_id) ⇒ Object



43
44
45
# File 'lib/cryptsy/client.rb', line 43

def orders(market_id)
  call(:myorders, marketid: market_id)
end

#process_response(response) ⇒ Object

Parameters:

  • response (Faraday::Response)

Returns:

  • (Object)

Raises:

  • (CryptsyError)


155
156
157
158
159
160
161
162
163
# File 'lib/cryptsy/client.rb', line 155

def process_response(response)
  body = Hashie::Mash.new(JSON.parse(response.body))

  unless body.success.to_i == 1
    raise ClientError, body.error
  end

  body.return
end

#trades(market_id, limit = 200) ⇒ Object



51
52
53
# File 'lib/cryptsy/client.rb', line 51

def trades(market_id, limit = 200)
  call(:mytrades, marketid: market_id, limit: limit)
end

#transactionsObject



59
60
61
# File 'lib/cryptsy/client.rb', line 59

def transactions
  call(:mytransactions)
end

#transfersObject



63
64
65
# File 'lib/cryptsy/client.rb', line 63

def transfers
  call(:mytransfers)
end