sibit 0.15.1 → 0.16.0

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,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef4603f30e6d52d2efec48d698778505150df7b5e98400938db804a1a412a053
4
- data.tar.gz: 1c30fe4e9d7e1c077204b26282ed78a169b6e86cc630baf376903bb2f8103601
3
+ metadata.gz: 1e3a925010c38e43c7657cf822c56a377b491b04cd0d048d444e25a66eba824f
4
+ data.tar.gz: 88a8c7fbd56ebcf3d37b298bdd5df931eaabfc72406da18cacc41fa19e98538f
5
5
  SHA512:
6
- metadata.gz: 8e1c22106253d2ee0ef9ab0d38e27526b4553c26bd1c01c8cb66910f692c19cd465b85b8104d5ea09c9dd2b22446e917bb333ff8cb7e1db3c730e0e6f3b472aa
7
- data.tar.gz: 035dfbccaef19b8ff39c7c404a0b11f058d813a0df492c741a6849ddac8eb0b990892523c186929e4b2822939d6011f246417c31ff3c24a5123732b3fe30b0db
6
+ metadata.gz: c2d94c940bc0d564090a9079d772fc1006c0bb64fdcc014cf8b77c4bfdfcfb3e09f8be2252ba8c0b5efe42bcad0130220b61c4bf9fbd0178401d8b720a625699
7
+ data.tar.gz: 6e22d3aad01fdb1eb058fa23d62ce137750f873f11f7afabc5677df240138ce4d0027ff0a77272068a2e4f2bba3359e9be7860d09bc68d3469c63a4a1e6262a6
data/README.md CHANGED
@@ -123,9 +123,10 @@ work with the following APIs:
123
123
  * [BTC.com](https://btc.com/api-doc)
124
124
  * [Cryptoapis.io](https://docs.cryptoapis.io/rest-apis/blockchain-as-a-service-apis/btc/index)
125
125
  * [Bitcoinchain.com](https://bitcoinchain.com/api)
126
+ * [Blockchair.com](https://blockchair.com/api/docs)
126
127
  * [Earn.com](https://bitcoinfees.earn.com/api)
127
128
 
128
- The first one in this list is used by default. If you want to use a diffent
129
+ The first one in this list is used by default. If you want to use a different
129
130
  one, you just specify it in the constructor of `Sibit` object:
130
131
 
131
132
  ```ruby
@@ -135,7 +136,8 @@ sibit = Sibit.new(api: Sibit::Btc.new)
135
136
  ```
136
137
 
137
138
  You may also use a combination of APIs. This may be very useful since
138
- some APIs are not reliable. You can provide an array of objects and they
139
+ some APIs are not reliable and others don't have all the features required.
140
+ You can provide an array of objects and they
139
141
  will be used one by one, until a successful response is obtained:
140
142
 
141
143
  ```ruby
@@ -50,9 +50,13 @@ class Sibit
50
50
 
51
51
  # Gets the balance of the address, in satoshi.
52
52
  def balance(address)
53
- (Sibit::Json.new(http: @http, log: @log).get(
53
+ json = Sibit::Json.new(http: @http, log: @log).get(
54
54
  URI("https://api-r.bitcoinchain.com/v1/address/#{address}")
55
- )[0]['balance'] * 100_000_000).to_i
55
+ )[0]
56
+ raise Sibit::Error, "Address #{address} not found" if json.nil?
57
+ b = (json['balance'] * 100_000_000).to_i
58
+ @log.info("The balance of #{address} is #{b} satoshi")
59
+ b
56
60
  end
57
61
 
58
62
  # Get recommended fees, in satoshi per byte.
@@ -62,9 +66,11 @@ class Sibit
62
66
 
63
67
  # Gets the hash of the latest block.
64
68
  def latest
65
- Sibit::Json.new(http: @http, log: @log).get(
69
+ hash = Sibit::Json.new(http: @http, log: @log).get(
66
70
  URI('https://api-r.bitcoinchain.com/v1/status')
67
71
  )['hash']
72
+ @log.info("The latest block hash is #{hash}")
73
+ hash
68
74
  end
69
75
 
70
76
  # Fetch all unspent outputs per address.
@@ -98,9 +98,11 @@ class Sibit
98
98
 
99
99
  # Gets the hash of the latest block.
100
100
  def latest
101
- Sibit::Json.new(http: @http, log: @log).get(
101
+ hash = Sibit::Json.new(http: @http, log: @log).get(
102
102
  URI('https://blockchain.info/latestblock')
103
103
  )['hash']
104
+ @log.info("The latest block hash is #{hash}")
105
+ hash
104
106
  end
105
107
 
106
108
  # This method should fetch a Blockchain block and return as a hash.
@@ -0,0 +1,98 @@
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 'cgi'
26
+ require_relative 'version'
27
+ require_relative 'error'
28
+ require_relative 'log'
29
+ require_relative 'http'
30
+ require_relative 'json'
31
+
32
+ # Blockchair.com API.
33
+ #
34
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
35
+ # Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
36
+ # License:: MIT
37
+ class Sibit
38
+ # Btc.com API.
39
+ class Blockchair
40
+ # Constructor.
41
+ def initialize(key: nil, log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
42
+ @key = key
43
+ @http = http
44
+ @log = log
45
+ @dry = dry
46
+ end
47
+
48
+ # Current price of BTC in USD (float returned).
49
+ def price(_currency)
50
+ raise Sibit::Error, 'Blockchair doesn\'t provide BTC price'
51
+ end
52
+
53
+ # Gets the balance of the address, in satoshi.
54
+ def balance(address)
55
+ json = Sibit::Json.new(http: @http, log: @log).get(
56
+ URI("https://api.blockchair.com/bitcoin/dashboards/address/#{address}?#{the_key}")
57
+ )['data'][address]
58
+ raise Sibit::Error, "Address #{address} not found" if json.nil?
59
+ b = json['address']['balance']
60
+ @log.info("The balance of #{address} is #{b} satoshi")
61
+ b
62
+ end
63
+
64
+ # Get recommended fees, in satoshi per byte.
65
+ def fees
66
+ raise Sibit::Error, 'Not implemented yet'
67
+ end
68
+
69
+ # Gets the hash of the latest block.
70
+ def latest
71
+ raise Sibit::Error, 'Not implemented yet'
72
+ end
73
+
74
+ # Fetch all unspent outputs per address.
75
+ def utxos(_sources)
76
+ raise Sibit::Error, 'Not implemented yet'
77
+ end
78
+
79
+ # Push this transaction (in hex format) to the network.
80
+ def push(hex)
81
+ Sibit::Json.new(http: @http, log: @log).post(
82
+ URI("https://api.blockchair.com/bitcoin/push/transaction?#{the_key}"),
83
+ "data=#{hex}"
84
+ )
85
+ end
86
+
87
+ # This method should fetch a Blockchain block and return as a hash.
88
+ def block(_hash)
89
+ raise Sibit::Error, 'Not implemented yet'
90
+ end
91
+
92
+ private
93
+
94
+ def the_key
95
+ @key.nil? ? '' : "key=#{CGI.escape(@key)}"
96
+ end
97
+ end
98
+ end
@@ -51,10 +51,13 @@ class Sibit
51
51
 
52
52
  # Gets the balance of the address, in satoshi.
53
53
  def balance(address)
54
- (Sibit::Json.new(http: @http, log: @log).get(
54
+ json = Sibit::Json.new(http: @http, log: @log).get(
55
55
  URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/address/#{address}"),
56
56
  headers: headers
57
- )['payload']['balance'].to_f * 100_000_000).to_i
57
+ )['payload']
58
+ b = (json['balance'].to_f * 100_000_000).to_i
59
+ @log.info("The balance of #{address} is #{b} satoshi")
60
+ b
58
61
  end
59
62
 
60
63
  # Get recommended fees, in satoshi per byte.
@@ -64,10 +67,12 @@ class Sibit
64
67
 
65
68
  # Gets the hash of the latest block.
66
69
  def latest
67
- Sibit::Json.new(http: @http, log: @log).get(
70
+ hash = Sibit::Json.new(http: @http, log: @log).get(
68
71
  URI('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks/latest'),
69
72
  headers: headers
70
73
  )['payload']['hash']
74
+ @log.info("The latest block hash is #{hash}")
75
+ hash
71
76
  end
72
77
 
73
78
  # Fetch all unspent outputs per address.
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.15.1'
29
+ VERSION = '0.16.0'
30
30
  end
@@ -0,0 +1,42 @@
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/blockchair'
28
+
29
+ # Sibit::Blockchair test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2019-2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class TestBlockchair < Minitest::Test
34
+ def test_fetch_balance
35
+ hash = '1GkQmKAmHtNfnD3LHhTkewJxKHVSta4m2a'
36
+ stub_request(:get, "https://api.blockchair.com/bitcoin/dashboards/address/#{hash}")
37
+ .to_return(body: "{\"data\": {\"#{hash}\": {\"address\": {\"balance\": 1}}}}")
38
+ sibit = Sibit::Blockchair.new
39
+ satoshi = sibit.balance(hash)
40
+ assert_equal(1, satoshi)
41
+ end
42
+ end
data/test/test_live.rb CHANGED
@@ -73,6 +73,8 @@ class TestLive < Minitest::Test
73
73
  skip
74
74
  WebMock.allow_net_connect!
75
75
  apis = []
76
+ require_relative '../lib/sibit/blockchair'
77
+ apis << Sibit::Blockchair.new
76
78
  require_relative '../lib/sibit/cryptoapis'
77
79
  apis << Sibit::Cryptoapis.new('-key-')
78
80
  require_relative '../lib/sibit/btc'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sibit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.1
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -283,6 +283,7 @@ files:
283
283
  - lib/sibit.rb
284
284
  - lib/sibit/bitcoinchain.rb
285
285
  - lib/sibit/blockchain.rb
286
+ - lib/sibit/blockchair.rb
286
287
  - lib/sibit/btc.rb
287
288
  - lib/sibit/cryptoapis.rb
288
289
  - lib/sibit/earn.rb
@@ -297,6 +298,7 @@ files:
297
298
  - test/test__helper.rb
298
299
  - test/test_bitcoinchain.rb
299
300
  - test/test_blockchain.rb
301
+ - test/test_blockchair.rb
300
302
  - test/test_btc.rb
301
303
  - test/test_cryptoapis.rb
302
304
  - test/test_fake.rb
@@ -335,6 +337,7 @@ test_files:
335
337
  - test/test__helper.rb
336
338
  - test/test_bitcoinchain.rb
337
339
  - test/test_blockchain.rb
340
+ - test/test_blockchair.rb
338
341
  - test/test_btc.rb
339
342
  - test/test_cryptoapis.rb
340
343
  - test/test_fake.rb