class Justcoin

Constants

DEFAULT_URL
VERSION

Attributes

client[R]
key[R]
options[R]

Public Class Methods

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

Creates a new Justcoin client.

@param key the API key, get it from

https://justcoin.com/client/#settings/apikeys

@param options the client can receive the following options

* `:log` [true/false] – whether to log requests and responses
* `:logger` [Logger] – a custom logger instance, implies `log: true`
* `:raw` [true/false] – whether to return the raw Faraday::Response object instead of a parsed value
* any other options are passed on to the Faraday client
# File lib/justcoin.rb, line 23
def initialize(key, options={})
  @key = key

  options[:url] ||= DEFAULT_URL
  options[:params] ||= {}
  options[:params][:key] ||= key
  options[:headers] ||= {}
  options[:headers][:user_agent] ||= "Justcoin ruby client v#{Justcoin::VERSION}"
  @options = options

  @client = build_client
end

Public Instance Methods

balances() click to toggle source

List balances

@return [Array] an array of current balances

# File lib/justcoin.rb, line 39
def balances
  client.get "balances"
end
cancel_order(id) click to toggle source

Cancel the remainder of the specified order

@param id the id of the order @return true if cancelling the order was successful

# File lib/justcoin.rb, line 103
def cancel_order(id)
  client.delete("orders/#{id}") == ""
end
create_order(market, type, price, amount) click to toggle source

Create an order

@param [String/Symbol] market the id of the market (e.g. `:btcstr`) @param type the order type; can be `:bid` (buy base currency) or `:ask` (sell base currency) @param [Numeric/String/nil] price the order price; if set to `nil`,

the order is placed as a market order

@param [Numeric/String] amount the amount of base currency

(identified by the first three letters of the market id) to buy/sell

@return [Hashie::Mash] includes the order id if the order is successfully created

# File lib/justcoin.rb, line 84
def create_order(market, type, price, amount)
  type = type.downcase.to_sym
  raise ArgumentError, "type must be either :bid or :ask" unless [:bid, :ask].include?(type)

  params = {
    market: market.upcase,
    type: type,
    price: price,
    amount: amount
  }

  client.post "orders", params
end
Also aliased as: place_order
market_depth(id) click to toggle source

Retrieve market depth

@param [String/Symbol] id the id of the market (e.g. `:btcstr`) @return [Hashie::Mash] `bids` and `asks` as arrays of `[price, volume]` pairs

# File lib/justcoin.rb, line 54
def market_depth(id)
  client.get "markets/#{id.upcase}/depth"
end
markets() click to toggle source

List markets

@return [Array] an array of markets with current statistics

# File lib/justcoin.rb, line 46
def markets
  client.get "markets"
end
order(id) click to toggle source

Retrieve a specific user order

Works for all kinds of orders: active, cancelled, or historical.

@param id the id of the order @return [Hashie::Mash] information about the order including matches

# File lib/justcoin.rb, line 71
def order(id)
  client.get "orders/#{id}"
end
orders() click to toggle source

Retrieve active user orders

@return [Array] an array of the user's active orders

# File lib/justcoin.rb, line 61
def orders
  client.get "orders"
end
place_order(market, type, price, amount)
Alias for: create_order

Private Instance Methods

build_client() click to toggle source
# File lib/justcoin.rb, line 113
def build_client
  Faraday.new(client_options) do |f|
    unless options[:raw]
      # This extracts the body and discards all other data from the
      # Faraday::Response object. It should be placed here in the middle
      # of the stack so that it runs as the last one.
      f.use Justcoin::BodyExtractor
      f.use Justcoin::DecimalConverter
    end

    f.request :json
    f.response :mashify
    f.response :dates
    f.response :json, content_type: /\bjson$/
    f.response :logger, options[:logger] if options[:logger] || options[:log]
    f.response :raise_error

    f.adapter Faraday.default_adapter
  end
end
client_options() click to toggle source
# File lib/justcoin.rb, line 109
def client_options
  options.select { |k,v| Faraday::ConnectionOptions.members.include?(k) }
end