Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
Add Order class to abstract formatting into [price, amount, timestamp]
Browse files Browse the repository at this point in the history
  • Loading branch information
tmlee committed Oct 18, 2017
1 parent 8d26e3d commit 0d54134
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
14 changes: 12 additions & 2 deletions lib/cryptoexchange/exchanges/allcoin/services/order_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@ def adapt(output, market_pair)
order_book.base = market_pair.base
order_book.target = market_pair.target
order_book.market = Allcoin::Market::NAME
order_book.asks = output['asks']
order_book.bids = output['bids']
order_book.asks = adapt_orders output['asks']
order_book.bids = adapt_orders output['bids']
order_book.timestamp = timestamp
order_book.payload = output
order_book
end

def adapt_orders(orders)
orders.collect do |order_entry|
price, amount = order_entry
order = Cryptoexchange::Models::Order.new(price: price,
amount: amount,
timestamp: nil)
order.format_as_list
end
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/cryptoexchange/exchanges/coinmate/services/order_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ def adapt(output, market_pair)
end

def adapt_orders(orders, timestamp)
# Format as: [price, amount, timestamp]
orders.collect { |order_entry| order_entry.values << timestamp }
orders.collect do |order_entry|
order = Cryptoexchange::Models::Order.new(price: order_entry['price'],
amount: order_entry['amount'],
timestamp: timestamp)
order.format_as_list
end
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/cryptoexchange/exchanges/gemini/services/order_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ def adapt(output, market_pair)
end

def adapt_orders(orders)
# Format as: [price, amount, timestamp]
orders.collect { |order_entry| order_entry.values }
orders.collect do |order_entry|
price, amount, timestamp = order_entry.values
order = Cryptoexchange::Models::Order.new(price: price,
amount: amount,
timestamp: timestamp)
order.format_as_list
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/cryptoexchange/models/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Cryptoexchange
module Models
class Order
attr_accessor :price, :amount, :timestamp

def initialize(args={})
@price = args[:price]
@amount = args[:amount]
@timestamp = args[:timestamp] || nil
end

def format_as_list
[@price, @amount, @timestamp]
end
end
end
end
4 changes: 2 additions & 2 deletions spec/exchanges/allcoin/integration/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
expect(order_book.market).to eq 'allcoin'
expect(order_book.asks).to_not be_empty
expect(order_book.bids).to_not be_empty
expect(order_book.asks.first.size).to eq 2
expect(order_book.bids.first.size).to eq 2
expect(order_book.asks.first.size).to eq 3
expect(order_book.bids.first.size).to eq 3
expect(order_book.asks.count).to be > 10
expect(order_book.bids.count).to be > 10
expect(order_book.timestamp).to be_a Numeric
Expand Down

0 comments on commit 0d54134

Please sign in to comment.