bitex 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +89 -0
  6. data/Rakefile +1 -0
  7. data/bitex.gemspec +33 -0
  8. data/lib/bitex.rb +11 -0
  9. data/lib/bitex/api.rb +57 -0
  10. data/lib/bitex/buy.rb +25 -0
  11. data/lib/bitex/market.rb +67 -0
  12. data/lib/bitex/match.rb +24 -0
  13. data/lib/bitex/order.rb +185 -0
  14. data/lib/bitex/profile.rb +10 -0
  15. data/lib/bitex/sell.rb +25 -0
  16. data/lib/bitex/specie_deposit.rb +27 -0
  17. data/lib/bitex/specie_withdrawal.rb +56 -0
  18. data/lib/bitex/transaction.rb +14 -0
  19. data/lib/bitex/usd_deposit.rb +65 -0
  20. data/lib/bitex/usd_withdrawal.rb +56 -0
  21. data/lib/bitex/version.rb +3 -0
  22. data/spec/ask_spec.rb +24 -0
  23. data/spec/bid_spec.rb +24 -0
  24. data/spec/buy_spec.rb +11 -0
  25. data/spec/fixtures/aggregated_data.json +1 -0
  26. data/spec/fixtures/asks_cancel.json +1 -0
  27. data/spec/fixtures/asks_create.json +1 -0
  28. data/spec/fixtures/bids_cancel.json +1 -0
  29. data/spec/fixtures/bids_create.json +1 -0
  30. data/spec/fixtures/market_ticker.json +1 -0
  31. data/spec/fixtures/order_book.json +1 -0
  32. data/spec/fixtures/orders.json +1 -0
  33. data/spec/fixtures/profile.json +1 -0
  34. data/spec/fixtures/transactions.json +1 -0
  35. data/spec/fixtures/user_transactions.json +1 -0
  36. data/spec/market_spec.rb +46 -0
  37. data/spec/order_spec.rb +23 -0
  38. data/spec/profile_spec.rb +23 -0
  39. data/spec/sell_spec.rb +11 -0
  40. data/spec/spec_helper.rb +28 -0
  41. data/spec/specie_deposit_spec.rb +16 -0
  42. data/spec/specie_withdrawal_spec.rb +37 -0
  43. data/spec/support/from_json_shared_examples.rb +52 -0
  44. data/spec/support/order_shared_examples.rb +45 -0
  45. data/spec/support/request_stubs.rb +17 -0
  46. data/spec/transaction_spec.rb +19 -0
  47. data/spec/usd_deposit_spec.rb +45 -0
  48. data/spec/usd_withdrawal_spec.rb +37 -0
  49. metadata +269 -0
@@ -0,0 +1,10 @@
1
+ module Bitex
2
+ # Your balances, fee and deposit addresses
3
+ class Profile
4
+ # Your profile conveniently formatted as a ruby hash with symbolized keys.
5
+ # @see https://bitex.la/developers#profile
6
+ def self.get
7
+ Api.private(:GET, '/private/profile').symbolize_keys
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ module Bitex
2
+ # A transaction in which you sold some quantity of specie.
3
+ class Sell < Match
4
+ # @!attribute id
5
+ # @return [Integer] This sell's unique ID.
6
+
7
+ # @!attribute created_at
8
+ # @return [Time] Time when this Sell happened.
9
+
10
+ # @!attribute specie
11
+ # @return [Symbol] :btc or :ltc
12
+
13
+ # @!attribute quantity
14
+ # @return [BigDecimal] Quantity of specie sold
15
+
16
+ # @!attribute amount
17
+ # @return [BigDecimal] Amount of USD earned
18
+
19
+ # @!attribute fee
20
+ # @return [BigDecimal] USD amount paid as transaction fee.
21
+
22
+ # @!attribute price
23
+ # @return [BigDecimal] Price charged per unit
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ module Bitex
2
+ # A deposit of some specie into your bitex.la balance
3
+ class SpecieDeposit
4
+ # @!attribute id
5
+ # @return [Integer] This SpecieDeposit's unique ID.
6
+ attr_accessor :id
7
+
8
+ # @!attribute created_at
9
+ # @return [Time] Time when this deposit credited.
10
+ attr_accessor :created_at
11
+
12
+ # @!attribute specie
13
+ # @return [Symbol] :btc or :ltc
14
+ attr_accessor :specie
15
+
16
+ # @!attribute quantity
17
+ # @return [BigDecimal] Quantity deposited
18
+ attr_accessor :quantity
19
+
20
+ # @visibility private
21
+ def self.from_json(json)
22
+ Api.from_json(new, json, true) do |thing|
23
+ thing.quantity = BigDecimal.new(json[4].to_s)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ module Bitex
2
+ # A withdrawal of some specie from your bitex.la balance
3
+ class SpecieWithdrawal
4
+ # @!attribute id
5
+ # @return [Integer] This SpecieWithdrawal's unique ID.
6
+ attr_accessor :id
7
+
8
+ # @!attribute created_at
9
+ # @return [Time] Time when this withdrawal was requested by you.
10
+ attr_accessor :created_at
11
+
12
+ # @!attribute specie
13
+ # @return [Symbol] :btc or :ltc
14
+ attr_accessor :specie
15
+
16
+ # @!attribute amount
17
+ # @return [BigDecimal] Quantity deposited
18
+ attr_accessor :quantity
19
+
20
+ # @!attribute status
21
+ # Returns the status of this withdrawal.
22
+ # * :received Our engine is checking if you have enough funds.
23
+ # * :pending your withdrawal was accepted and is being processed.
24
+ # * :done your withdrawal was processed and published to the network.
25
+ # * :cancelled your withdrawal could not be processed.
26
+ attr_accessor :status
27
+
28
+ # @!attribute reason
29
+ # Returns the reason for cancellation of this withdrawal, if any.
30
+ # * :not_cancelled
31
+ # * :insufficient_funds The instruction was received, but you didn't have enough
32
+ # funds available
33
+ # * :destination_invalid The destination address was invalid.
34
+ attr_accessor :reason
35
+
36
+ # @visibility private
37
+ def self.from_json(json)
38
+ status_lookup = {
39
+ 1 => :received,
40
+ 2 => :pending,
41
+ 3 => :done,
42
+ 4 => :cancelled,
43
+ }
44
+ reason_lookup = {
45
+ 0 => :not_cancelled,
46
+ 1 => :insufficient_funds,
47
+ 2 => :destination_invalid,
48
+ }
49
+ Api.from_json(new, json, true) do |thing|
50
+ thing.quantity = BigDecimal.new(json[4].to_s)
51
+ thing.status = status_lookup[json[5]]
52
+ thing.reason = reason_lookup[json[6]]
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,14 @@
1
+ module Bitex
2
+ # Utility class for fetching an heterogeneous list of objects that
3
+ # compose your transaction history.
4
+ class Transaction
5
+ # @return [Array<Bitex::Buy, Bitex::Sell, Bitex::SpecieDeposit,
6
+ # Bitex::SpecieWithdrawal, Bitex::UsdDeposit, Bitex::UsdWithdrawal>]
7
+ # Returns an heterogeneous array with all your transactions for the past
8
+ # 30 days sorted by descending date.
9
+ # @see https://bitex.la/developers#user-transactions
10
+ def self.all
11
+ Api.private(:GET, '/private/transactions').collect{|o| Api.deserialize(o) }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,65 @@
1
+ module Bitex
2
+ # A deposit of USD to your bitex.la balance.
3
+ class UsdDeposit
4
+ # @!attribute id
5
+ # @return [Integer] This UsdDeposit's unique ID.
6
+ attr_accessor :id
7
+
8
+ # @!attribute created_at
9
+ # @return [Time] Time when this deposit was announced by you.
10
+ attr_accessor :created_at
11
+
12
+ # @!attribute amount
13
+ # @return [BigDecimal] Final amount credited to your bitex USD balance.
14
+ attr_accessor :amount
15
+
16
+ # @!attribute deposit_method
17
+ # The method used for this deposit
18
+ # * :astropay
19
+ # * :other
20
+ attr_accessor :deposit_method
21
+
22
+ # @!attribute status
23
+ # The status of this deposit.
24
+ # * :pending your deposit notice was received, we're waiting for the funds
25
+ # to credit.
26
+ # * :done your deposit credited correctly, the funds are available in your
27
+ # balance.
28
+ # * :cancelled your deposit did not credit, check the 'reason' field.
29
+ attr_accessor :status
30
+
31
+ # @!attribute reason
32
+ # The reason for cancellation of this deposit, if any.
33
+ # * :not_cancelled.
34
+ # * :did_not_credit funds never arrived to our end.
35
+ # * :sender_unknown we could not accept these funds because you're not the
36
+ # sender.
37
+ # * :other we'll contact you regarding this deposit.
38
+ attr_accessor :reason
39
+
40
+ # @visibility private
41
+ def self.from_json(json)
42
+ deposit_method_lookup = {
43
+ 1 => :astropay,
44
+ 2 => :other,
45
+ }
46
+ status_lookup = {
47
+ 1 => :pending,
48
+ 2 => :done,
49
+ 3 => :cancelled,
50
+ }
51
+ reason_lookup = {
52
+ 0 => :not_cancelled,
53
+ 1 => :did_not_credit,
54
+ 2 => :sender_unknown,
55
+ 3 => :other,
56
+ }
57
+ Api.from_json(new, json) do |thing|
58
+ thing.amount = BigDecimal.new(json[3].to_s)
59
+ thing.deposit_method = deposit_method_lookup[json[4]]
60
+ thing.status = status_lookup[json[5]]
61
+ thing.reason = reason_lookup[json[6]]
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,56 @@
1
+ module Bitex
2
+ # A withdrawal of USD from your bitex.la balance
3
+ class UsdWithdrawal
4
+ # @!attribute id
5
+ # @return [Integer] This UsdWithdrawal's unique ID.
6
+ attr_accessor :id
7
+
8
+ # @!attribute created_at
9
+ # @return [Time] Time when this withdrawal was requested by you.
10
+ attr_accessor :created_at
11
+
12
+ # @!attribute amount
13
+ # @return [BigDecimal] Amount withdrawn from your bitex USD balance.
14
+ attr_accessor :amount
15
+
16
+ # @!attribute status
17
+ # Returns the status of this withdrawal.
18
+ # * :received Our engine is checking if you have enough funds.
19
+ # * :pending your withdrawal was accepted and is being processed.
20
+ # * :done your withdrawal was processed and it's on its way through
21
+ # the withdrawal channel you chose.
22
+ # * :cancelled your withdrawal could not be processed.
23
+ attr_accessor :status
24
+
25
+ # @!attribute reason
26
+ # Returns the reason for cancellation of this withdrawal, if any.
27
+ # * :not_cancelled
28
+ # * :insufficient_funds The instruction was received, but you didn't have enough
29
+ # funds available
30
+ # * :no_instructions We could not understand the instructions you provided.
31
+ # * :recipient_unknown we could not issue this withdrawal because you're
32
+ # not the receiving party.
33
+ attr_accessor :reason
34
+
35
+ # @visibility private
36
+ def self.from_json(json)
37
+ status_lookup = {
38
+ 1 => :received,
39
+ 2 => :pending,
40
+ 3 => :done,
41
+ 4 => :cancelled,
42
+ }
43
+ reason_lookup = {
44
+ 0 => :not_cancelled,
45
+ 1 => :insufficient_funds,
46
+ 2 => :no_instructions,
47
+ 3 => :recipient_unknown,
48
+ }
49
+ Api.from_json(new, json) do |thing|
50
+ thing.amount = BigDecimal.new(json[3].to_s)
51
+ thing.status = status_lookup[json[4]]
52
+ thing.reason = reason_lookup[json[5]]
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Bitex
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitex::Ask do
4
+ let(:as_json) do
5
+ [2,12345678,946685400,1,100.00000000,100.00000000,1000.00000000,1]
6
+ end
7
+
8
+ it_behaves_like 'API class'
9
+ it_behaves_like 'API class with a specie'
10
+ it_behaves_like 'JSON deserializable order'
11
+
12
+ describe 'Api calls' do
13
+ before(:each){ Bitex.api_key = 'valid_api_key' }
14
+ it_behaves_like 'Order', 'asks'
15
+ end
16
+
17
+ { quantity: 100.0, remaining_quantity: 100.0}.each do |field, value|
18
+ it "sets #{field} as BigDecimal" do
19
+ thing = subject.class.from_json(as_json).send(field)
20
+ thing.should be_a BigDecimal
21
+ thing.should == value
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitex::Bid do
4
+ let(:as_json) do
5
+ [1,12345678,946685400,1,100.00000000,100.00000000,1000.00000000,1]
6
+ end
7
+
8
+ it_behaves_like 'API class'
9
+ it_behaves_like 'API class with a specie'
10
+ it_behaves_like 'JSON deserializable order'
11
+
12
+ describe 'Api calls' do
13
+ before(:each){ Bitex.api_key = 'valid_api_key' }
14
+ it_behaves_like 'Order', 'bids'
15
+ end
16
+
17
+ { amount: 100.0, remaining_amount: 100.0}.each do |field, value|
18
+ it "sets #{field} as BigDecimal" do
19
+ thing = subject.class.from_json(as_json).send(field)
20
+ thing.should be_a BigDecimal
21
+ thing.should == value
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitex::Buy do
4
+ let(:as_json) do
5
+ [4,12345678,946685400,1,100.50000000,201.0000000,0.05000000,2.00000000]
6
+ end
7
+
8
+ it_behaves_like 'API class'
9
+ it_behaves_like 'API class with a specie'
10
+ it_behaves_like 'JSON deserializable match'
11
+ end
@@ -0,0 +1 @@
1
+ [[1403668800, 570.0, 574.0, 570.0, 574.0, 1.06771929, 571.0, 570.917848641659],[1403683200, 560.0, 570.0, 560.0, 570.0, 1.14175147, 565.0, 565.753596095655],[1403697600, 560.0, 560.0, 560.0, 560.0, 0.0, 565.0, 0.0],[1403712000, 560.0, 560.0, 560.0, 560.0, 0.0, 565.0, 0.0]]
@@ -0,0 +1 @@
1
+ [1,12345678,946685400,1,100.00000000,100.00000000,1000.00000000,3]
@@ -0,0 +1 @@
1
+ [2,12345678,946685400,1,100.00000000,100.00000000,1000.00000000,1]
@@ -0,0 +1 @@
1
+ [1,12345678,946685400,1,100.00000000,100.00000000,1000.00000000,3]
@@ -0,0 +1 @@
1
+ [1,12345678,946685400,1,100.00000000,100.00000000,1000.00000000,1]
@@ -0,0 +1 @@
1
+ {"last":639.0,"high":659.0,"low":639.0,"vwap":647.195852839369,"volume":4.80579022,"bid":637.0,"ask":638.5}
@@ -0,0 +1 @@
1
+ {"bids":[[639.21,1.95],[637.0,0.47],[630.0,1.58]],"asks":[[642.4,0.4],[643.3,0.95],[644.3,0.25]]}
@@ -0,0 +1 @@
1
+ [[1,12345678,946685400,1,100.00000000,10.00000000,1000.00000000,1],[2,12345678,946685400,1,10.00000000,1.00000000,1100.00000000,1]]
@@ -0,0 +1 @@
1
+ {"usd_balance":10000.00,"usd_reserved":2000.00,"usd_available":8000.00,"btc_balance":20.00000000,"btc_reserved":5.00000000,"btc_available":15.00000000,"ltc_balance":250.00000000,"ltc_reserved":100.00000000,"ltc_available":150.00000000,"fee":0.5,"btc_deposit_address":"1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","ltc_deposit_address":"LXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
@@ -0,0 +1 @@
1
+ [[1404259180, 1272, 650.0, 0.5], [1404259179, 1271, 639.0, 0.46948356]]
@@ -0,0 +1 @@
1
+ [[3,12345678,946685400,1,2.00000000,600.00000000,0.05000000,300.00000000],[4,12345678,946685400,1,2.00000000,600.00000000,0.05000000,300.00000000],[5,12345678,946685400,1,100.00000000],[6,12345678,946685400,1,100.00000000,1,0],[7,12345678,946685400,100.00000000,1,1,0],[8,12345678,946685400,100.00000000,1,0]]
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitex::MarketData do
4
+
5
+ { btc: Bitex::BitcoinMarketData,
6
+ ltc: Bitex::LitecoinMarketData
7
+ }.each do |specie, market_data|
8
+ describe "when getting #{specie} market data" do
9
+ it "gets the ticker" do
10
+ stub_get("/#{specie}/market/ticker", 'market_ticker')
11
+ market_data.ticker.should == {
12
+ last: 639.0, high: 659.0, low: 639.0, vwap: 647.195852839369,
13
+ volume: 4.80579022, bid: 637.0, ask: 638.5
14
+ }
15
+ end
16
+
17
+ it "gets the order book" do
18
+ stub_get("/#{specie}/market/order_book", 'order_book')
19
+ market_data.order_book.should == {
20
+ bids: [[639.21,1.95],[637.0,0.47],[630.0,1.58]],
21
+ asks: [[642.4,0.4],[643.3,0.95],[644.3,0.25]]
22
+ }
23
+ end
24
+
25
+ it "gets the transactions" do
26
+ stub_get("/#{specie}/market/transactions", 'transactions')
27
+ market_data.transactions.should == [
28
+ [1404259180, 1272, 650.0, 0.5],
29
+ [1404259179, 1271, 639.0, 0.46948356]
30
+ ]
31
+ end
32
+
33
+ %w(last_24_hours last_7_days last_30_days).each do |method|
34
+ it "gets aggregated data for #{method}" do
35
+ stub_get("/#{specie}/market/#{method}", 'aggregated_data')
36
+ market_data.send(method).should == [
37
+ [1403668800, 570.0, 574.0, 570.0, 574.0, 1.06771929, 571.0, 570.917848641659],
38
+ [1403683200, 560.0, 570.0, 560.0, 570.0, 1.14175147, 565.0, 565.753596095655],
39
+ [1403697600, 560.0, 560.0, 560.0, 560.0, 0.0, 565.0, 0.0],
40
+ [1403712000, 560.0, 560.0, 560.0, 560.0, 0.0, 565.0, 0.0]
41
+ ]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitex::Order do
4
+ before(:each) do
5
+ Bitex.api_key = 'valid_api_key'
6
+ end
7
+
8
+ it 'gets all orders' do
9
+ stub_private(:get, "/private/orders", 'orders')
10
+ bid, ask, empty = Bitex::Order.all
11
+ bid.should be_a Bitex::Bid
12
+ ask.should be_a Bitex::Ask
13
+ empty.should be_nil
14
+ end
15
+
16
+ it 'gets active orders' do
17
+ stub_private(:get, "/private/orders/active", 'orders')
18
+ bid, ask, empty = Bitex::Order.active
19
+ bid.should be_a Bitex::Bid
20
+ ask.should be_a Bitex::Ask
21
+ empty.should be_nil
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitex::Profile do
4
+ before(:each){ Bitex.api_key = 'valid_api_key' }
5
+
6
+ it 'gets your profile' do
7
+ stub_private(:get, '/private/profile', 'profile')
8
+ Bitex::Profile.get.should == {
9
+ usd_balance: 10000.0,
10
+ usd_reserved: 2000.0,
11
+ usd_available: 8000.0,
12
+ btc_balance: 20.0,
13
+ btc_reserved: 5.0,
14
+ btc_available: 15.0,
15
+ ltc_balance: 250.0,
16
+ ltc_reserved: 100.0,
17
+ ltc_available: 150.0,
18
+ fee: 0.5,
19
+ btc_deposit_address: "1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
20
+ ltc_deposit_address: "LXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
21
+ }
22
+ end
23
+ end