class Flashboy::Exchange

Public Instance Methods

order_book(pair) click to toggle source
# File lib/flashboy/exchange.rb, line 30
def order_book(pair)
  id = formatted_pair(pair)
  data = request order_book_path(id)
  OrderBook.new parse_order_book(data, id).merge(
    pair: pair,
    exchange: name
  )
end
quote(pair) click to toggle source
# File lib/flashboy/exchange.rb, line 3
def quote(pair)
  id = formatted_pair(pair)
  data = request quote_path(id)
  Quote.new parse_quote(data, id).merge(
    pair: pair,
    exchange: name
  )
end
trades(pair, options = {}) click to toggle source
# File lib/flashboy/exchange.rb, line 12
def trades(pair, options = {})
  start, finish, cursor = nil
  if options[:start] || options[:finish]
    finish = options[:finish] || Time.now.to_i
    start  = options[:start] || finish - (60 * 60)
  elsif options[:since]
    cursor = options[:since] || 0
  end

  id = formatted_pair(pair)
  url = trades_path(id, {start: start, finish: finish, since: cursor})
  # puts url
  data = request url
  parse_trades(data, self, id).map do |trade|
    Trade.new(trade.merge(pair: pair, exchange: name))
  end
end

Private Instance Methods

connection() click to toggle source
# File lib/flashboy/exchange.rb, line 61
def connection
  @connection ||= Faraday.new(url: host) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    # faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end
formatted_pair(pair) click to toggle source
# File lib/flashboy/exchange.rb, line 41
def formatted_pair(pair)
  currency, base = pair.split('-')
  if [currency, base].compact.empty?
    raise ArgumentError, 'You must provide the currency pair as CURRENCY-BASE'
  end
  [currency, base]
end
request(path) click to toggle source
# File lib/flashboy/exchange.rb, line 49
def request(path)
  begin
    response = connection.get(path).body
    data = JSON.parse(response)
  rescue JSON::ParserError => e
    Flashboy.logger.warn "Failed to parse response from #{host}/#{path}"
    data = {}
  end

  data.is_a?(Hash) ? data.recursively_symbolize_keys! : data.map(&:recursively_symbolize_keys!)
end