sibit 0.19.1 → 0.20.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63efa03d475df765f994bd609f8f5deb31cea1450566f1e68a8c5a8ac497ec0c
4
- data.tar.gz: c36540c991e462bf6825d9a48109a3f0c1a4ca1934b4741a17074a3cf8503565
3
+ metadata.gz: 3b0578e225f53b1d3904fae19aa6f41ba7aea719bd137587821b3e2fba2a0373
4
+ data.tar.gz: 82178b4a20da5d18d99101649c31ad8fd3188c88a4ea74577cf2413d1bf6f9e2
5
5
  SHA512:
6
- metadata.gz: 98cc194b64718dc11ba35650a6c1bda4b52e4ca04cd935784f45387fb6dbdb552de00343d5e7dfd90ba796137500028bf5c31177006a087f2ec2a754751d3a5e
7
- data.tar.gz: 161f739388531e1b79ca07860d374023c0ff8109de32b35d35dc487892b4fa09e2db2471a524983aa8f2e35723806053dcbeb0e98ecc9ff2e8ecfe2e1da737ec
6
+ metadata.gz: 756a8a1273e053f4a880220f8d33db38544e36f4a0d38118ee72cc1421d21311900859b9fbac5732c24260a9c48d7599f0865970db27e7046543ad52b2ceb223
7
+ data.tar.gz: 64650acdb47d4671028075dd84e1d3f7f5c8eefb9c67e5b45212e5d6c5aaec1e1576419a126808b20a4e11fc6dc2bd16ea5f473accbb0d1dac936c2896308ecc
@@ -5,7 +5,7 @@ install: |
5
5
  sudo bundle install --no-color "--gemfile=$(pwd)/Gemfile"
6
6
  release:
7
7
  script: |-
8
- bundle exec rake
8
+ bundle exec rake clean test rubocop copyright
9
9
  sed -i "s/1\.0\.snapshot/${tag}/g" lib/sibit/version.rb
10
10
  git add lib/sibit/version.rb
11
11
  git commit -m "version set to ${tag}"
@@ -14,7 +14,7 @@ release:
14
14
  gem push *.gem --config-file ../rubygems.yml
15
15
  merge:
16
16
  script: |-
17
- bundle exec rake
17
+ bundle exec rake clean test rubocop copyright
18
18
  deploy:
19
19
  script: |-
20
20
  echo "There is nothing to deploy"
@@ -32,9 +32,10 @@ class Sibit
32
32
  # Best of API.
33
33
  class BestOf
34
34
  # Constructor.
35
- def initialize(list, log: Sibit::Log.new)
35
+ def initialize(list, log: Sibit::Log.new, verbose: false)
36
36
  @list = list
37
37
  @log = log
38
+ @verbose = verbose
38
39
  end
39
40
 
40
41
  # Current price of BTC in USD (float returned).
@@ -106,12 +107,12 @@ class Sibit
106
107
  begin
107
108
  results << yield(api)
108
109
  rescue Sibit::Error => e
109
- @log.info("The API #{api.class.name} failed at #{method}(): #{e.message}")
110
+ @log.info("The API #{api.class.name} failed at #{method}(): #{e.message}") if @verbose
110
111
  end
111
112
  end
112
113
  if results.empty?
113
- raise Sibit::Error, "No APIs out of #{@api.length} managed to succeed at #{method}(): \
114
- #{@api.map { |a| a.class.name }.join(', ')}"
114
+ raise Sibit::Error, "No APIs out of #{@list.length} managed to succeed at #{method}(): \
115
+ #{@list.map { |a| a.class.name }.join(', ')}"
115
116
  end
116
117
  results.group_by(&:to_s).values.max_by(&:size)[0]
117
118
  end
@@ -58,7 +58,16 @@ class Sibit
58
58
  @log.info("The balance of #{address} is zero (not found)")
59
59
  return 0
60
60
  end
61
- txns = json['data']['list']
61
+ data = json['data']
62
+ if data.nil?
63
+ @log.info("The balance of #{address} is probably zero (not found)")
64
+ return 0
65
+ end
66
+ txns = data['list']
67
+ if txns.nil?
68
+ @log.info("The balance of #{address} is probably zero (not found)")
69
+ return 0
70
+ end
62
71
  balance = txns.map { |tx| tx['value'] }.inject(&:+) || 0
63
72
  @log.info("The balance of #{address} is #{balance}, total txns: #{txns.count}")
64
73
  balance
@@ -164,9 +173,13 @@ class Sibit
164
173
  psize = 50
165
174
  all = []
166
175
  loop do
167
- txns = Sibit::Json.new(http: @http, log: @log).get(
176
+ data = Sibit::Json.new(http: @http, log: @log).get(
168
177
  URI("https://chain.api.btc.com/v3/block/#{hash}/tx?page=#{page}&pagesize=#{psize}")
169
- )['data']['list'].map do |t|
178
+ )['data']
179
+ raise Sibit::Error, "The block #{hash} has no data at page #{page}" if data.nil?
180
+ list = data['list']
181
+ raise Sibit::Error, "The list is empty for block #{hash} at page #{page}" if list.nil?
182
+ txns = list.map do |t|
170
183
  {
171
184
  hash: t['hash'],
172
185
  outputs: t['outputs'].reject { |o| o['spent_by_tx'] }.map do |o|
@@ -32,9 +32,10 @@ class Sibit
32
32
  # First of API.
33
33
  class FirstOf
34
34
  # Constructor.
35
- def initialize(list, log: Sibit::Log.new)
35
+ def initialize(list, log: Sibit::Log.new, verbose: false)
36
36
  @list = list
37
37
  @log = log
38
+ @verbose = verbose
38
39
  end
39
40
 
40
41
  # Current price of BTC in USD (float returned).
@@ -109,12 +110,12 @@ class Sibit
109
110
  done = true
110
111
  break
111
112
  rescue Sibit::Error => e
112
- @log.info("The API #{api.class.name} failed at #{method}(): #{e.message}")
113
+ @log.info("The API #{api.class.name} failed at #{method}(): #{e.message}") if @verbose
113
114
  end
114
115
  end
115
116
  unless done
116
- raise Sibit::Error, "No APIs out of #{@api.length} managed to succeed at #{method}(): \
117
- #{@api.map { |a| a.class.name }.join(', ')}"
117
+ raise Sibit::Error, "No APIs out of #{@list.length} managed to succeed at #{method}(): \
118
+ #{@list.map { |a| a.class.name }.join(', ')}"
118
119
  end
119
120
  result
120
121
  end
@@ -26,5 +26,5 @@
26
26
  # License:: MIT
27
27
  class Sibit
28
28
  # Current version of the library.
29
- VERSION = '0.19.1'
29
+ VERSION = '0.20.1'
30
30
  end
@@ -47,4 +47,16 @@ class TestBestOf < Minitest::Test
47
47
  assert_equal(64, sibit.latest.length)
48
48
  assert_equal(12, sibit.fees[:S])
49
49
  end
50
+
51
+ def test_all_fail
52
+ api = Class.new do
53
+ def latest
54
+ raise Sibit::Error, 'intentionally'
55
+ end
56
+ end.new
57
+ sibit = Sibit::BestOf.new([api, api])
58
+ assert_raises Sibit::Error do
59
+ sibit.latest
60
+ end
61
+ end
50
62
  end
@@ -42,6 +42,28 @@ class TestBtc < Minitest::Test
42
42
  assert_equal(0, balance)
43
43
  end
44
44
 
45
+ def test_get_zero_balance_no_txns
46
+ stub_request(
47
+ :get,
48
+ 'https://chain.api.btc.com/v3/address/1MZT1fa6y8H9UmbZV6HqKF4UY41o9MGT5f/unspent'
49
+ ).to_return(body: '{"data":{}}')
50
+ sibit = Sibit::Btc.new
51
+ balance = sibit.balance('1MZT1fa6y8H9UmbZV6HqKF4UY41o9MGT5f')
52
+ assert(balance.is_a?(Integer))
53
+ assert_equal(0, balance)
54
+ end
55
+
56
+ def test_get_broken_balance
57
+ stub_request(
58
+ :get,
59
+ 'https://chain.api.btc.com/v3/address/1MZT1fa6y8H9UmbZV6HqKF4UY41o9MGT5f/unspent'
60
+ ).to_return(body: '{}')
61
+ sibit = Sibit::Btc.new
62
+ balance = sibit.balance('1MZT1fa6y8H9UmbZV6HqKF4UY41o9MGT5f')
63
+ assert(balance.is_a?(Integer))
64
+ assert_equal(0, balance)
65
+ end
66
+
45
67
  def test_get_empty_balance
46
68
  stub_request(
47
69
  :get,
@@ -64,6 +86,18 @@ class TestBtc < Minitest::Test
64
86
  assert_equal(123, balance)
65
87
  end
66
88
 
89
+ def test_fetch_block_broken
90
+ hash = '000000000000000007341915521967247f1dec17b3a311b8a8f4495392f1439b'
91
+ stub_request(:get, "https://chain.api.btc.com/v3/block/#{hash}")
92
+ .to_return(body: '{"data": {"next_block_hash": "n", "hash": "h", "prev_block_hash": "p"}}')
93
+ stub_request(:get, "https://chain.api.btc.com/v3/block/#{hash}/tx?page=1&pagesize=50")
94
+ .to_return(body: '{}')
95
+ sibit = Sibit::Btc.new
96
+ assert_raises Sibit::Error do
97
+ sibit.block(hash)
98
+ end
99
+ end
100
+
67
101
  def test_fetch_block
68
102
  hash = '000000000000000007341915521967247f1dec17b3a311b8a8f4495392f1439b'
69
103
  stub_request(:get, "https://chain.api.btc.com/v3/block/#{hash}")
@@ -47,4 +47,16 @@ class TestFirstOf < Minitest::Test
47
47
  assert_equal(64, sibit.latest.length)
48
48
  assert_equal(12, sibit.fees[:S])
49
49
  end
50
+
51
+ def test_all_fail
52
+ api = Class.new do
53
+ def latest
54
+ raise Sibit::Error, 'intentionally'
55
+ end
56
+ end.new
57
+ sibit = Sibit::FirstOf.new([api, api])
58
+ assert_raises Sibit::Error do
59
+ sibit.latest
60
+ end
61
+ end
50
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sibit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.1
4
+ version: 0.20.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-17 00:00:00.000000000 Z
11
+ date: 2020-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace