sibit 0.7.8 → 0.8.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: c0241240816fa4582989be16e381b3dd3a97f541923cb2edc469058243ec3a9e
4
- data.tar.gz: 23f53e4099aeff681150625deb1d89b800229fc815142942d914ff253288a023
3
+ metadata.gz: fd8d72ab658b0424c135882a0787001395a69db720bc8283cf6b2155729c5a96
4
+ data.tar.gz: 37c2fab8a0bd840620e57c2a38ac831dcab13232f6274299c11477675e41734a
5
5
  SHA512:
6
- metadata.gz: 9235cfad8bb3de94b66b4f9574c826e305b32957abbff951617db697e8ac5330181c8bd23b4503cbb9e2932bcfe7c49c5a8122f7feff51d5a61966ddc9ae4e53
7
- data.tar.gz: 2be08bc2fa281449a956b0039af69585dd6f35425080e414cf56eb9594e283ce309122f647e72609afb40a963a3e760d7cda7d5b368e1a2064f25588f7174242
6
+ metadata.gz: 9e70e784fdf68578e1919ce2989cdfb53c8fc50289e41b57d5df0429fdb28d6679eaf0e558f34112517019f372b16f0c2212b45b7c5104ad3bafd704cf3516b8
7
+ data.tar.gz: 11123bef8e29dde1689907f4536325f807510e9dd3dd10002afea284925176b37543060083ea17a8934e72d43a88cb2fc51c86697938d565bcfceb0d1487b937
data/.rubocop.yml CHANGED
@@ -18,7 +18,7 @@ Layout/MultilineMethodCallIndentation:
18
18
  Metrics/ParameterLists:
19
19
  Max: 6
20
20
  Metrics/ClassLength:
21
- Max: 120
21
+ Max: 150
22
22
  Metrics/AbcSize:
23
23
  Enabled: false
24
24
  Metrics/BlockLength:
data/README.md CHANGED
@@ -10,7 +10,9 @@
10
10
  [![PDD status](http://www.0pdd.com/svg?name=yegor256/sibit)](http://www.0pdd.com/p?name=yegor256/sibit)
11
11
  [![Gem Version](https://badge.fury.io/rb/sibit.svg)](http://badge.fury.io/rb/sibit)
12
12
  [![Maintainability](https://api.codeclimate.com/v1/badges/74c909f06d4afa0d8001/maintainability)](https://codeclimate.com/github/yegor256/sibit/maintainability)
13
+
13
14
  [![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/sibit.svg)](https://codecov.io/github/yegor256/sibit?branch=master)
15
+ [![Hits-of-Code](https://hitsofcode.com/github/yegor256/sibit)](https://hitsofcode.com/view/github/yegor256/sibit)
14
16
 
15
17
  To understand how Bitcoin protocol works, I recommend you watching
16
18
  this [short video](https://www.youtube.com/watch?v=IV9pRBq5A4g).
data/bin/sibit CHANGED
@@ -38,6 +38,7 @@ Commands are:
38
38
  balance: Check the balance of the Bitcoin address
39
39
  pay: Send a new Bitcoin transaction
40
40
  Options are:"
41
+ o.string '--proxy', 'HTTPS proxy for all requests, e.g. "localhost:3128"'
41
42
  o.bool '--help', 'Read this: https://github.com/yegor256/sibit' do
42
43
  puts o
43
44
  exit
@@ -48,7 +49,10 @@ Options are:"
48
49
  raise "#{ex.message}"
49
50
  end
50
51
  raise 'Try --help' if opts.arguments.empty?
51
- sibit = Sibit.new(log: opts[:verbose] ? STDOUT : nil)
52
+ sibit = Sibit.new(
53
+ log: opts[:verbose] ? STDOUT : nil,
54
+ http: opts[:proxy] ? Sibit.proxy_http(opts[:proxy]) : Sibit.default_http
55
+ )
52
56
  case opts.arguments[0]
53
57
  when 'price'
54
58
  puts sibit.price
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.7.8'
29
+ VERSION = '0.8.0'
30
30
  end
data/lib/sibit.rb CHANGED
@@ -24,6 +24,7 @@ require 'net/http'
24
24
  require 'uri'
25
25
  require 'bitcoin'
26
26
  require 'json'
27
+ require 'cgi'
27
28
 
28
29
  # Sibit main class.
29
30
  #
@@ -68,14 +69,30 @@ class Sibit
68
69
  end
69
70
  end
70
71
 
72
+ # This HTTP client will be used by default.
73
+ def self.default_http
74
+ http = Net::HTTP.new('blockchain.info', 443)
75
+ http.use_ssl = true
76
+ http
77
+ end
78
+
79
+ # This HTTP client with proxy.
80
+ def self.proxy_http(addr)
81
+ host, port = addr.split(':')
82
+ http = Net::HTTP.new('blockchain.info', 443, host, port.to_i)
83
+ http.use_ssl = true
84
+ http
85
+ end
86
+
71
87
  # Constructor.
72
88
  #
73
89
  # You may provide the log you want to see the messages in. If you don't
74
90
  # provide anything, the console will be used. The object you provide
75
91
  # has to respond to the method +info+ or +puts+ in order to receive logging
76
92
  # messages.
77
- def initialize(log: STDOUT)
93
+ def initialize(log: STDOUT, http: Sibit.default_http)
78
94
  @log = log
95
+ @http = http
79
96
  end
80
97
 
81
98
  # Current price of 1 BTC.
@@ -181,7 +198,12 @@ class Sibit
181
198
  # response for correctness.
182
199
  def get_json(uri)
183
200
  start = Time.now
184
- res = Net::HTTP.get_response(URI('https://blockchain.info' + uri))
201
+ res = @http.get(
202
+ uri,
203
+ 'Accept' => 'text/plain',
204
+ 'User-Agent' => user_agent,
205
+ 'Accept-Encoding' => ''
206
+ )
185
207
  raise Error, "Failed to retrieve #{uri} (#{res.code}): #{res.body}" unless res.code == '200'
186
208
  info("GET #{uri}: #{res.code}/#{res.body.length}b in #{age(start)}")
187
209
  JSON.parse(res.body)
@@ -224,8 +246,15 @@ class Sibit
224
246
 
225
247
  def post_tx(body)
226
248
  start = Time.now
227
- uri = URI('https://blockchain.info/pushtx')
228
- res = Net::HTTP.post_form(uri, tx: body)
249
+ uri = '/pushtx'
250
+ res = @http.post(
251
+ '/pushtx',
252
+ "tx=#{CGI.escape(body)}",
253
+ 'Accept' => 'text/plain',
254
+ 'User-Agent' => user_agent,
255
+ 'Accept-Encoding' => '',
256
+ 'Content-Type' => 'application/x-www-form-urlencoded'
257
+ )
229
258
  raise Error, "Failed to post tx to #{uri}: #{res.code}\n#{res.body}" unless res.code == '200'
230
259
  info("POST #{uri}: #{res.code} in #{age(start)}")
231
260
  end
@@ -237,4 +266,8 @@ class Sibit
237
266
  @log.puts(msg)
238
267
  end
239
268
  end
269
+
270
+ def user_agent
271
+ "Anonymous/#{Sibit::VERSION}"
272
+ end
240
273
  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.7.8
4
+ version: 0.8.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: 2019-04-19 00:00:00.000000000 Z
11
+ date: 2019-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace