cexio_reinvestor 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTIyMTJiZDQ0YTM2MTRhOWZlZmQ3YWRiNjVkNTg5M2ZkNzc3ZWJhMA==
4
+ NDViNzhmOGZlODFlNWMwY2ZjODQxZTM0YjE1NDcxNTRjY2MyMTEzNA==
5
5
  data.tar.gz: !binary |-
6
- NjQ0YjUzOGM5ZDUwNjU1MjhiMDE3MGUyZDZhODAzYWFiYTJmNzc2ZQ==
6
+ OTM2MTA2MTE5ZjExZTg4NzFiMDgwNzdhZTc3MWFmNTU4ODIxYjg4NQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZTAwODhmZjE2MWFhZmI5Mzk0ZTM5NDdkY2MwZDFiZmZiMTViYWVjMGRiNmRk
10
- YzE3Yzc4NmFkODAxNTc1MmE3M2JhY2RkODQ1NWRlNDdiZTg0OWJhM2RlYTVi
11
- YTkyZTJjMzMyNTk4NGFjN2RkZWFhZTY4OTBhZWVjOGYzMDg1MDM=
9
+ NDdiMzBjZDlhN2U4N2RjNGNmYzg3OTRjNjM0ZTQxYTlkMWJhYmEwYWQ2ZGY2
10
+ OWQ4NzgzZjAwY2RiMGIwNWVjYmFmZmI0ZDRlMGY1Nzc3MmY1Nzg4YTEzYTVl
11
+ Mjg5NjE2ZGVmNTA4YTlkNTIwYTM5ZjNiMTA2NTM3NDdkNjUzOWQ=
12
12
  data.tar.gz: !binary |-
13
- MzJhNmM1ZGY3MWYyYTExNWRmOTkxZmY3NTRlYTkzZWFmYWNjZDU1MGI1NGUy
14
- N2M5MWJjNzYzZTUxNzMxOWMzNzQwMDc0NDg2NzAwMzNiYjI1Y2FmYzNjNmUx
15
- NzI2N2IyN2JiOTk5NmEzYzliZWU2YzBkYzczNzZhYzRhZjdhODI=
13
+ MzIyYzk2ZmUxOWYzOGZhNDRhMWZiYzI2NGFjOGM2MDRjMDUzMjJiOTUyOGQy
14
+ NmI1Y2QzNTlkMmFjMDM5YmIzMjFkMjIzMzkyZjE3ZjM4Yjc2YjlkZjY3YzIw
15
+ NjY2NDllYTA0NzExZWI3OTg2NjgzMmM3MzY5MzdlNTAyODgwNWU=
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "micro-optparse"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
25
- #spec.add_development_dependency "rspec", "~> 2.14.1"
26
- #spec.add_development_dependency "debugger"
25
+ spec.add_development_dependency "rspec", "~> 2.14.1"
26
+ spec.add_development_dependency "webmock", "~> 1.17.2"
27
+ spec.add_development_dependency "debugger"
27
28
  end
@@ -20,9 +20,9 @@ module CexioReinvestor
20
20
  to, from = pair.split('/')
21
21
  balance = BigDecimal.new(api.balance[from]['available'])
22
22
  sleep 1
23
- price, amount = api.order_book(pair)['asks'].first.map { |x| BigDecimal.new(x, 8) }
23
+ price = BigDecimal.new(api.ticker(pair)['ask'], 8)
24
24
  sleep 1
25
- amount_to_buy = [ balance / price, amount ].min.round(8, :down)
25
+ amount_to_buy = (balance / price).round(8, :down)
26
26
  return if amount_to_buy < MIN
27
27
 
28
28
  result = api.place_order('buy', amount_to_buy, price, pair)
@@ -38,12 +38,22 @@ module CexioReinvestor
38
38
  print ", #{result['pending'].to_s('F')} pending" if result['pending'] > 0
39
39
  puts " - #{Time.now}"
40
40
  end
41
+ rescue Timeout::Error
42
+ :timeout
43
+ rescue JSON::ParserError
44
+ :invalid_response
41
45
  end
42
46
 
43
47
  def run
44
48
  while true do
45
49
  [ 'GHS/BTC', 'GHS/NMC' ].each do |pair|
46
- trade(pair)
50
+ case trade(pair)
51
+ when :timeout
52
+ puts "Timed out when trying to contact CEX.io."
53
+ when :invalid_response
54
+ puts "Hit CEX.io API request limit, will sleep for a bit."
55
+ sleep 50
56
+ end
47
57
  sleep 10
48
58
  end
49
59
  end
@@ -1,3 +1,3 @@
1
1
  module CexioReinvestor
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe CexioReinvestor::Trader do
4
+ PAIRS = [ 'GHS/BTC', 'GHS/NMC', 'NMC/BTC' ]
5
+
6
+ let(:trader) { CexioReinvestor::Trader.new('username', 'api_key', 'api_secret') }
7
+
8
+ let(:stub_balance) do
9
+ stub_request(:post, 'https://cex.io/api/balance/').to_return(body: lambda { |r| balance })
10
+ end
11
+ let(:balance) { '{"timestamp":"1391640624","username":"eromirou","IXC":{"available":"3.95433625"},"DVC":{"available":"148.66713273"},"BTC":{"available":"1.5","orders":"0.00000000"},"NMC":{"available":"5.6","orders":"0.00000000"},"GHS":{"available":"200.1","orders":"0.00000000"}}' }
12
+
13
+ let(:stub_ticker) do
14
+ stub_request(:post, %r{https://cex\.io/api/ticker/(#{PAIRS.join('|')})})
15
+ .to_return(body: lambda { |r| send(r.uri.path.split('/')[-2..-1].join('_').downcase) })
16
+ end
17
+ let(:ghs_btc) { '{"timestamp":"1391640624", "low":"0.02", "high":"0.03", "last":"0.025", "volume":"500", "bid":"0.026", "ask":"0.027"}' }
18
+ let(:ghs_nmc) { '{"timestamp":"1391640624", "low":"4", "high":"5", "last":"4.5", "volume":"200", "bid":"4.6", "ask":"4.7"}' }
19
+ let(:nmc_btc) { '{"timestamp":"1391640624", "low":"0.005", "high":"0.008", "last":"0.006", "volume":"3000", "bid":"0.006", "ask":"0.007"}' }
20
+
21
+ let(:stub_place_order) do
22
+ stub_request(:post, %r{https://cex\.io/api/place_order/(#{PAIRS.join('|')})})
23
+ .to_return(body: lambda { |r|
24
+ params = CGI.parse(r.body)
25
+ send("#{r.uri.path.split('/')[-2..-1].join('_').downcase}_#{params['type'].first}_order")
26
+ })
27
+ end
28
+ let(:ghs_btc_buy_order) do
29
+ '{"id":"123456", "time":"1391640624", "pending":"0.0", "amount":"1", "type":"buy", "price":"0.025"}'
30
+ end
31
+ let(:ghs_btc_sell_order) do
32
+ '{"id":"123456", "time":"1391640624", "pending":"0.0", "amount":"1", "type":"sell", "price":"0.026"}'
33
+ end
34
+
35
+ before :each do
36
+ stub_balance
37
+ stub_ticker
38
+ stub_place_order
39
+ end
40
+
41
+ describe '#trade(pair)' do
42
+ it 'does not blow up when a timeout occurs' do
43
+ stub_request(:post, 'https://cex.io/api/balance/').to_timeout
44
+ expect do
45
+ expect(trader.trade('GHS/BTC')).to eq :timeout
46
+ end.not_to raise_error
47
+ end
48
+
49
+ it 'does not blow up when a parser error occurs' do
50
+ stub_request(:post, 'https://cex.io/api/balance/').to_return body: '<html></html>'
51
+ expect do
52
+ expect(trader.trade('GHS/BTC')).to eq :invalid_response
53
+ end.not_to raise_error
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,13 @@
1
+ require 'webmock/rspec'
2
+
3
+ WebMock.disable_net_connect!
4
+
5
+ require 'cexio_reinvestor'
6
+
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+
12
+ config.order = 'random'
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cexio_reinvestor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Rojas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-05 00:00:00.000000000 Z
11
+ date: 2014-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cexio
@@ -52,6 +52,48 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.17.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.17.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: debugger
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
55
97
  description: CEX.io Reinvestor reinvests earned BTC and NMC.
56
98
  email:
57
99
  - tmsrjs@gmail.com
@@ -69,6 +111,8 @@ files:
69
111
  - cexio_reinvestor.gemspec
70
112
  - lib/cexio_reinvestor.rb
71
113
  - lib/cexio_reinvestor/version.rb
114
+ - spec/cexio_reinvestor_spec.rb
115
+ - spec/spec_helper.rb
72
116
  homepage: ''
73
117
  licenses:
74
118
  - MIT
@@ -93,4 +137,6 @@ rubygems_version: 2.1.9
93
137
  signing_key:
94
138
  specification_version: 4
95
139
  summary: CEX.io Reinvestor.
96
- test_files: []
140
+ test_files:
141
+ - spec/cexio_reinvestor_spec.rb
142
+ - spec/spec_helper.rb