class Cryptsy::Client

Constants

DEFAULT_OPTIONS
ORDER_TYPE_BUY
ORDER_TYPE_SELL

Public Class Methods

new(public_key, private_key, options = {}) click to toggle source

@param [String] public_key @param [String] private_key @param [Hash] options

# 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

Public Instance Methods

all_orders() click to toggle source
# File lib/cryptsy/client.rb, line 51
def all_orders
  call(:allmyorders)
end
all_trades() click to toggle source
# File lib/cryptsy/client.rb, line 59
def all_trades
  call(:allmytrades)
end
balance(currency_code) click to toggle source
# File lib/cryptsy/client.rb, line 32
def balance(currency_code)
  info.balances_available.fetch(currency_code.upcase).to_f
end
calculate_buy_fees(quantity, price) click to toggle source
# File lib/cryptsy/client.rb, line 112
def calculate_buy_fees(quantity, price)
  calculate_fees(ORDER_TYPE_BUY, quantity, price)
end
calculate_fees(order_type, quantity, price) click to toggle source
# File lib/cryptsy/client.rb, line 120
def calculate_fees(order_type, quantity, price)
  call(:calculatefees,
    ordertype: order_type,
    quantity: format_number(quantity),
    price: format_number(price)
  )
end
calculate_sell_fees(quantity, price) click to toggle source
# File lib/cryptsy/client.rb, line 116
def calculate_sell_fees(quantity, price)
  calculate_fees(ORDER_TYPE_SELL, quantity, price)
end
call(method_name, params = {}) click to toggle source

@raise [ClientError] @param [Symbol] method_name @param [Hash] params @return [Object]

# File lib/cryptsy/client.rb, line 144
def call(method_name, params = {})
  request = {
    method: method_name,
    nonce: (Time.now.to_f * 1000).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_orders() click to toggle source
# File lib/cryptsy/client.rb, line 108
def cancel_all_orders
  call(:cancelallorders)
end
cancel_order(order_id) click to toggle source
# File lib/cryptsy/client.rb, line 100
def cancel_order(order_id)
  call(:cancelorder, orderid: order_id)
end
cancel_orders(market_id) click to toggle source
# File lib/cryptsy/client.rb, line 104
def cancel_orders(market_id)
  call(:cancelmarketorders, marketid: market_id)
end
create_buy_order(market_id, quantity, price) click to toggle source
# File lib/cryptsy/client.rb, line 83
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) click to toggle source
# File lib/cryptsy/client.rb, line 91
def create_order(market_id, order_type, quantity, price)
  call(:createorder,
    marketid: market_id,
    ordertype: order_type,
    quantity: format_number(quantity),
    price: format_number(price)
  )
end
create_sell_order(market_id, quantity, price) click to toggle source
# File lib/cryptsy/client.rb, line 87
def create_sell_order(market_id, quantity, price)
  create_order(market_id, ORDER_TYPE_SELL, quantity, price)
end
format_number(number) click to toggle source

@param [Numeric] number @return [String]

# File lib/cryptsy/client.rb, line 184
def format_number(number)
  format('%.8f', number)
end
generate_new_address(currency) click to toggle source
# File lib/cryptsy/client.rb, line 128
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
info() click to toggle source
# File lib/cryptsy/client.rb, line 28
def info
  call(:getinfo)
end
make_withdrawal(address, amount) click to toggle source
# File lib/cryptsy/client.rb, line 136
def make_withdrawal(address, amount)
  call(:makewithdrawal, address: address, amount: amount)
end
market_by_pair(primary_code, secondary_code) click to toggle source
# File lib/cryptsy/client.rb, line 40
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) click to toggle source
# File lib/cryptsy/client.rb, line 71
def market_depth(market_id)
  call(:depth, marketid: market_id)
end
market_orders(market_id) click to toggle source
# File lib/cryptsy/client.rb, line 75
def market_orders(market_id)
  call(:marketorders, marketid: market_id)
end
market_trades(market_id) click to toggle source
# File lib/cryptsy/client.rb, line 79
def market_trades(market_id)
  call(:markettrades, marketid: market_id)
end
markets() click to toggle source
# File lib/cryptsy/client.rb, line 36
def markets
  call(:getmarkets)
end
normalize_currency_code(code) click to toggle source

@param [Object] code @return [String]

# File lib/cryptsy/client.rb, line 190
def normalize_currency_code(code)
  code.to_s.upcase
end
orders(market_id) click to toggle source
# File lib/cryptsy/client.rb, line 47
def orders(market_id)
  call(:myorders, marketid: market_id)
end
process_response(response) click to toggle source

@raise [ClientError] @param [Faraday::Response] response @return [Object]

# File lib/cryptsy/client.rb, line 168
def process_response(response)
  body = Hashie::Mash.new(JSON.parse(response.body))

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

  if body.return
    body.return
  else
    body
  end
end
trades(market_id, limit = 200) click to toggle source
# File lib/cryptsy/client.rb, line 55
def trades(market_id, limit = 200)
  call(:mytrades, marketid: market_id, limit: limit)
end
transactions() click to toggle source
# File lib/cryptsy/client.rb, line 63
def transactions
  call(:mytransactions)
end
transfers() click to toggle source
# File lib/cryptsy/client.rb, line 67
def transfers
  call(:mytransfers)
end