sibit 0.17.0 → 0.18.0

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: a146f6b5c827dd0cabc3736c0cf4218c600e80adf6a78ae6ef72665acd5a9d86
4
- data.tar.gz: 5dff578a3f2889896f6c8869227efe7451ba97df317ded00284d63ee0dab4a75
3
+ metadata.gz: 6bd701b64312a5b018c9c7545cd0a104953bafab231543932d536a42e04e97bd
4
+ data.tar.gz: 83c7f78688df06cd8ea6ddc40f6b328ef40bb81b1feddb11b6be4b9b1c295753
5
5
  SHA512:
6
- metadata.gz: 1fe6289d3eedbdc45cc2543e498307f4aa7cfe7b1b1951549d827f5ebd9ce96f659818d6672ad21f57aa0b2856f189ea042e604d65ad40422bb4ae909976d9f2
7
- data.tar.gz: b76f16791ef4140beac79b9650c6677d22127604592747b43fb9ca1c8b3e1d94faeea868d959aa74fe8debfe8637fe01340f7d0604015646b9a4865a34cff301
6
+ metadata.gz: adbeaee159b161ad9451954260dc82efddf53871e63943991c2c1de33de4849e117323aad6b95d7dab52cc5a78074bbe89428f172df673713cc84c4551a7a2fb
7
+ data.tar.gz: 8b5d2e3c516046838a6a520bf979bfe46d3d71b8fee4063ede6385894d8fc17e934993aa0db5a3ecefd7b23c9e2eeb12a2b998be8f81fd87b6a9f97f69d3d3fb
data/README.md CHANGED
@@ -119,12 +119,13 @@ The library works through one (or a few) public APIs for fetching
119
119
  Bitcoin data and pushing transactions to the network. At the moment we
120
120
  work with the following APIs:
121
121
 
122
- * [Blockchain.com](https://www.blockchain.com/api/blockchain_api)
123
- * [BTC.com](https://btc.com/api-doc)
124
- * [Cryptoapis.io](https://docs.cryptoapis.io/rest-apis/blockchain-as-a-service-apis/btc/index)
125
- * [Bitcoinchain.com](https://bitcoinchain.com/api)
126
- * [Blockchair.com](https://blockchair.com/api/docs)
127
- * [Earn.com](https://bitcoinfees.earn.com/api)
122
+ * [Blockchain.com](https://www.blockchain.com/api/blockchain_api): `Sibit::Blockchain`
123
+ * [BTC.com](https://btc.com/api-doc): `Sibit::Btc`
124
+ * [Cryptoapis.io](https://docs.cryptoapis.io/rest-apis/blockchain-as-a-service-apis/btc/index): `Sibit::Cryptoapis`
125
+ * [Bitcoinchain.com](https://bitcoinchain.com/api): `Sibit::Bitcoinchain`
126
+ * [Blockchair.com](https://blockchair.com/api/docs): `Sibit::Blockchair`
127
+ * [Cex.io](https://cex.io/rest-api): `Sibit::Cex`
128
+ * [Earn.com](https://bitcoinfees.earn.com/api): `Sibit::Earn`
128
129
 
129
130
  The first one in this list is used by default. If you want to use a different
130
131
  one, you just specify it in the constructor of `Sibit` object:
@@ -44,7 +44,7 @@ class Sibit
44
44
  end
45
45
 
46
46
  # Current price of BTC in USD (float returned).
47
- def price(_currency)
47
+ def price(_currency = 'USD')
48
48
  raise Sibit::Error, 'Bitcoinchain API doesn\'t provide BTC price'
49
49
  end
50
50
 
@@ -53,6 +53,15 @@ class Sibit
53
53
  raise Sibit::Error, 'Bitcoinchain API doesn\'t provide height()'
54
54
  end
55
55
 
56
+ # Get hash of the block after this one.
57
+ def next_of(hash)
58
+ nxt = Sibit::Json.new(http: @http, log: @log).get(
59
+ URI("https://api-r.bitcoinchain.com/v1/block/#{hash}")
60
+ )[0]['next_block']
61
+ nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
62
+ nxt
63
+ end
64
+
56
65
  # Gets the balance of the address, in satoshi.
57
66
  def balance(address)
58
67
  json = Sibit::Json.new(http: @http, log: @log).get(
@@ -47,7 +47,7 @@ class Sibit
47
47
  end
48
48
 
49
49
  # Current price of BTC in USD (float returned).
50
- def price(currency)
50
+ def price(currency = 'USD')
51
51
  h = Sibit::Json.new(http: @http, log: @log).get(
52
52
  URI('https://blockchain.info/ticker')
53
53
  )[currency]
@@ -57,6 +57,11 @@ class Sibit
57
57
  price
58
58
  end
59
59
 
60
+ # Get hash of the block after this one.
61
+ def next_of(_hash)
62
+ raise Sibit::Error, 'Blockchain API doesn\'t provide next_of()'
63
+ end
64
+
60
65
  # The height of the block.
61
66
  def height(hash)
62
67
  json = Sibit::Json.new(http: @http, log: @log).get(
@@ -46,7 +46,7 @@ class Sibit
46
46
  end
47
47
 
48
48
  # Current price of BTC in USD (float returned).
49
- def price(_currency)
49
+ def price(_currency = 'USD')
50
50
  raise Sibit::Error, 'Blockchair doesn\'t provide BTC price'
51
51
  end
52
52
 
@@ -55,6 +55,12 @@ class Sibit
55
55
  raise Sibit::Error, 'Blockchair API doesn\'t provide height()'
56
56
  end
57
57
 
58
+ # Get hash of the block after this one.
59
+ def next_of(_hash)
60
+ # They don't provide next block hash
61
+ raise Sibit::Error, 'Blockchair API doesn\'t provide next_of()'
62
+ end
63
+
58
64
  # Gets the balance of the address, in satoshi.
59
65
  def balance(address)
60
66
  json = Sibit::Json.new(http: @http, log: @log).get(
data/lib/sibit/btc.rb CHANGED
@@ -30,6 +30,8 @@ require_relative 'json'
30
30
 
31
31
  # Btc.com API.
32
32
  #
33
+ # Here: https://btc.com/api-doc
34
+ #
33
35
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
34
36
  # Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
35
37
  # License:: MIT
@@ -44,7 +46,7 @@ class Sibit
44
46
  end
45
47
 
46
48
  # Current price of BTC in USD (float returned).
47
- def price(_currency)
49
+ def price(_currency = 'USD')
48
50
  raise Sibit::Error, 'Btc.com API doesn\'t provide prices'
49
51
  end
50
52
 
@@ -63,6 +65,16 @@ class Sibit
63
65
  balance
64
66
  end
65
67
 
68
+ # Get hash of the block after this one.
69
+ def next_of(hash)
70
+ head = Sibit::Json.new(http: @http, log: @log).get(
71
+ URI("https://chain.api.btc.com/v3/block/#{hash}")
72
+ )
73
+ nxt = head['data']['next_block_hash']
74
+ nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
75
+ nxt
76
+ end
77
+
66
78
  # The height of the block.
67
79
  def height(hash)
68
80
  json = Sibit::Json.new(http: @http, log: @log).get(
data/lib/sibit/cex.rb ADDED
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019-2020 Yegor Bugayenko
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the 'Software'), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'uri'
24
+ require 'json'
25
+ require_relative 'error'
26
+ require_relative 'log'
27
+ require_relative 'http'
28
+ require_relative 'json'
29
+
30
+ # Cex.io API.
31
+ #
32
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
33
+ # Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
34
+ # License:: MIT
35
+ class Sibit
36
+ # Btc.com API.
37
+ class Cex
38
+ # Constructor.
39
+ def initialize(log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
40
+ @http = http
41
+ @log = log
42
+ @dry = dry
43
+ end
44
+
45
+ # Current price of BTC in USD (float returned).
46
+ def price(currency = 'USD')
47
+ json = Sibit::Json.new(http: @http, log: @log).get(
48
+ URI("https://cex.io/api/last_price/BTC/#{currency}")
49
+ )
50
+ p = json['lprice'].to_f
51
+ @log.info("The price of BTC is #{p} #{currency}")
52
+ p
53
+ end
54
+
55
+ # Get hash of the block after this one.
56
+ def next_of(_hash)
57
+ raise Sibit::Error, 'Cex.io API doesn\'t provide next_of()'
58
+ end
59
+
60
+ # Gets the balance of the address, in satoshi.
61
+ def balance(_address)
62
+ raise Sibit::Error, 'Cex.io doesn\'t implement balance()'
63
+ end
64
+
65
+ # The height of the block.
66
+ def height(_hash)
67
+ raise Sibit::Error, 'Cex.io doesn\'t implement height()'
68
+ end
69
+
70
+ # Get recommended fees, in satoshi per byte.
71
+ def fees
72
+ raise Sibit::Error, 'Cex.io doesn\'t implement fees()'
73
+ end
74
+
75
+ # Gets the hash of the latest block.
76
+ def latest
77
+ raise Sibit::Error, 'Cex.io doesn\'t implement latest()'
78
+ end
79
+
80
+ # Fetch all unspent outputs per address.
81
+ def utxos(_sources)
82
+ raise Sibit::Error, 'Cex.io doesn\'t implement utxos()'
83
+ end
84
+
85
+ # Push this transaction (in hex format) to the network.
86
+ def push(_hex)
87
+ raise Sibit::Error, 'Cex.io doesn\'t implement push()'
88
+ end
89
+
90
+ def block(_hash)
91
+ raise Sibit::Error, 'Cex.io doesn\'t implement block()'
92
+ end
93
+ end
94
+ end
@@ -45,10 +45,18 @@ class Sibit
45
45
  end
46
46
 
47
47
  # Current price of BTC in USD (float returned).
48
- def price(_currency)
48
+ def price(_currency = 'USD')
49
49
  raise Sibit::Error, 'Cryptoapis doesn\'t provide BTC price'
50
50
  end
51
51
 
52
+ # Get hash of the block after this one.
53
+ def next_of(hash)
54
+ Sibit::Json.new(http: @http, log: @log).get(
55
+ URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks/#{hash}"),
56
+ headers: headers
57
+ )['payload']['hash']
58
+ end
59
+
52
60
  # The height of the block.
53
61
  def height(hash)
54
62
  json = Sibit::Json.new(http: @http, log: @log).get(
data/lib/sibit/earn.rb CHANGED
@@ -52,6 +52,11 @@ class Sibit
52
52
  raise Sibit::Error, 'balance() doesn\'t work here'
53
53
  end
54
54
 
55
+ # Get hash of the block after this one.
56
+ def next_of(_hash)
57
+ raise Sibit::Error, 'Earn.com API doesn\'t provide next_of()'
58
+ end
59
+
55
60
  # The height of the block.
56
61
  def height(_hash)
57
62
  raise Sibit::Error, 'Earn API doesn\'t provide height()'
data/lib/sibit/json.rb CHANGED
@@ -51,7 +51,7 @@ class Sibit
51
51
  def get(uri, headers: {})
52
52
  start = Time.now
53
53
  res = @http.client(uri).get(
54
- "#{uri.path}?#{uri.query}",
54
+ "#{uri.path.empty? ? '/' : uri.path}#{uri.query ? "?#{uri.query}" : ''}",
55
55
  {
56
56
  'Accept' => 'application/json',
57
57
  'User-Agent' => user_agent,
data/lib/sibit/version.rb CHANGED
@@ -26,5 +26,5 @@
26
26
  # License:: MIT
27
27
  class Sibit
28
28
  # Current version of the library.
29
- VERSION = '0.17.0'
29
+ VERSION = '0.18.0'
30
30
  end
@@ -36,7 +36,6 @@ class TestBitcoinchain < Minitest::Test
36
36
  :get,
37
37
  'https://api-r.bitcoinchain.com/v1/status'
38
38
  ).to_return(body: '{"hash": "test"}')
39
- WebMock.allow_net_connect!
40
39
  sibit = Sibit::Bitcoinchain.new
41
40
  hash = sibit.latest
42
41
  assert_equal('test', hash)
data/test/test_cex.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019-2020 Yegor Bugayenko
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the 'Software'), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'minitest/autorun'
24
+ require 'webmock/minitest'
25
+ require 'json'
26
+ require_relative '../lib/sibit'
27
+ require_relative '../lib/sibit/cex'
28
+
29
+ # Sibit::Cex test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class TestBtc < Minitest::Test
34
+ def test_get_price
35
+ stub_request(
36
+ :get,
37
+ 'https://cex.io/api/last_price/BTC/USD'
38
+ ).to_return(body: '{"lprice":123}')
39
+ sibit = Sibit::Cex.new
40
+ price = sibit.price
41
+ assert_equal(123.0, price)
42
+ end
43
+ end
data/test/test_json.rb CHANGED
@@ -21,6 +21,7 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'minitest/autorun'
24
+ require 'webmock/minitest'
24
25
  require 'uri'
25
26
  require_relative '../lib/sibit/json'
26
27
 
@@ -30,8 +31,10 @@ require_relative '../lib/sibit/json'
30
31
  # License:: MIT
31
32
  class TestJson < Minitest::Test
32
33
  def test_loads_hash
33
- WebMock.allow_net_connect!
34
- json = Sibit::Json.new.get(URI('https://api-r.bitcoinchain.com/v1/status'))
35
- assert(!json['hash'].nil?)
34
+ stub_request(:get, 'https://hello.com').to_return(body: '{"test":123}')
35
+ json = Sibit::Json.new.get(URI('https://hello.com'))
36
+ assert_equal(123, json['test'])
37
+ json = Sibit::Json.new.get(URI('https://hello.com/'))
38
+ assert_equal(123, json['test'])
36
39
  end
37
40
  end
data/test/test_live.rb CHANGED
@@ -67,6 +67,24 @@ class TestLive < Minitest::Test
67
67
  end
68
68
  end
69
69
 
70
+ def test_price
71
+ for_each do |api|
72
+ price = api.price
73
+ assert(price.is_a?(Float))
74
+ end
75
+ end
76
+
77
+ def test_next_of
78
+ for_each do |api|
79
+ hash = '000000003031a0e73735690c5a1ff2a4be82553b2a12b776fbd3a215dc8f778d'
80
+ nxt = api.next_of(hash)
81
+ assert_equal(
82
+ '0000000071966c2b1d065fd446b1e485b2c9d9594acd2007ccbd5441cfc89444',
83
+ nxt
84
+ )
85
+ end
86
+ end
87
+
70
88
  def test_height
71
89
  for_each do |api|
72
90
  hash = '000000003031a0e73735690c5a1ff2a4be82553b2a12b776fbd3a215dc8f778d'
@@ -98,6 +116,8 @@ class TestLive < Minitest::Test
98
116
  apis << Sibit::Cryptoapis.new('')
99
117
  require_relative '../lib/sibit/btc'
100
118
  apis << Sibit::Btc.new
119
+ require_relative '../lib/sibit/cex'
120
+ apis << Sibit::Cex.new
101
121
  require_relative '../lib/sibit/bitcoinchain'
102
122
  apis << Sibit::Bitcoinchain.new
103
123
  apis.each do |api|
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.17.0
4
+ version: 0.18.0
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-03-26 00:00:00.000000000 Z
11
+ date: 2020-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace
@@ -285,6 +285,7 @@ files:
285
285
  - lib/sibit/blockchain.rb
286
286
  - lib/sibit/blockchair.rb
287
287
  - lib/sibit/btc.rb
288
+ - lib/sibit/cex.rb
288
289
  - lib/sibit/cryptoapis.rb
289
290
  - lib/sibit/earn.rb
290
291
  - lib/sibit/error.rb
@@ -300,6 +301,7 @@ files:
300
301
  - test/test_blockchain.rb
301
302
  - test/test_blockchair.rb
302
303
  - test/test_btc.rb
304
+ - test/test_cex.rb
303
305
  - test/test_cryptoapis.rb
304
306
  - test/test_fake.rb
305
307
  - test/test_json.rb
@@ -339,6 +341,7 @@ test_files:
339
341
  - test/test_blockchain.rb
340
342
  - test/test_blockchair.rb
341
343
  - test/test_btc.rb
344
+ - test/test_cex.rb
342
345
  - test/test_cryptoapis.rb
343
346
  - test/test_fake.rb
344
347
  - test/test_json.rb