class Liquid::Client

Attributes

http[R]
params[R]
path[R]
token_id[R]
url[RW]
user_secret[R]

Public Class Methods

new(token, secret) click to toggle source
# File lib/liquid/client.rb, line 9
def initialize(token, secret)
  @url = "https://api.liquid.com"
  @http = http
  @token_id = token || ENV["LIQUID_KEY"]
  @user_secret = secret || ENV["LIQUID_SECRET"]
end

Public Instance Methods

active_orders() click to toggle source
# File lib/liquid/client.rb, line 27
def active_orders
  @path = '/orders?status=live'
  get_request
end
balance() click to toggle source
# File lib/liquid/client.rb, line 32
def balance
  @path = '/accounts/balance'
  get_request
end
cancel_order(id) click to toggle source

PUT

# File lib/liquid/client.rb, line 74
def cancel_order(id)
  @path = "/orders/#{id}/cancel"
  put_request
end
create_order(order_type: "limit", pair: nil, side: nil, quantity: nil, price: nil) click to toggle source

POST

# File lib/liquid/client.rb, line 56
def create_order(order_type: "limit", pair: nil, side: nil, quantity: nil, price: nil)
  pair_id = product_id(pair)
  raise InvalidArgument if side.nil? || quantity.nil? || price.nil?
  @path = '/orders'
  @params = 
    {
      "order": {
        "order_type": order_type,
        "product_id": pair_id,
        "side": side,
        "quantity": quantity,
        "price": price
      }
    }.to_json
  post_request
end
limit_buy_price(pair) click to toggle source
# File lib/liquid/client.rb, line 47
def limit_buy_price(pair)
  product_detail(pair)["market_bid"].to_f
end
limit_sell_price(pair) click to toggle source
# File lib/liquid/client.rb, line 51
def limit_sell_price(pair)
  product_detail(pair)["market_ask"].to_f
end
orders() click to toggle source
# File lib/liquid/client.rb, line 22
def orders
  @path = '/orders'
  get_request
end
product() click to toggle source

GET

# File lib/liquid/client.rb, line 17
def product
  @path = '/products'
  get_request
end
product_detail(pair) click to toggle source
# File lib/liquid/client.rb, line 37
def product_detail(pair)
  product.select{|p|
    p["currency_pair_code"] == pair
  }.first
end
product_id(pair) click to toggle source
# File lib/liquid/client.rb, line 43
def product_id(pair)
  product_detail(pair)["id"].to_i
end

Private Instance Methods

auth_payload() click to toggle source
# File lib/liquid/client.rb, line 115
def auth_payload
  {
    path: path,
    nonce: DateTime.now.strftime('%Q'),
    token_id: token_id
  }
end
get_request() click to toggle source
# File lib/liquid/client.rb, line 87
def get_request
  request = Net::HTTP::Get.new(path)
  request(request)
end
post_request() click to toggle source
# File lib/liquid/client.rb, line 92
def post_request
  request = Net::HTTP::Post.new(path)
  request.body = @params
  request(request)
end
put_request() click to toggle source
# File lib/liquid/client.rb, line 98
def put_request
  request = Net::HTTP::Put.new(path)
  request(request)
end
request(request) click to toggle source
# File lib/liquid/client.rb, line 103
def request(request)
  request.add_field('X-Quoine-API-Version', '2')
  request.add_field('X-Quoine-Auth', signature)
  request.add_field('Content-Type', 'application/json')
  response = http.request(request)
  begin
    JSON.parse(response.body)
  rescue JSON::ParserError 
    raise ResponseParseError
  end
end
signature() click to toggle source
# File lib/liquid/client.rb, line 123
def signature
  JWT.encode(auth_payload, user_secret, 'HS256')
end