coinrpc 2.0.1 → 3.0.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: f80efb4ad16cc2494f8c143fa84607c207317cfe0594f086d59fa068a4807072
4
- data.tar.gz: d9c5104d11491b5495eda5aa23eb275d576ccd5d7e934c79d22309abc81727b5
3
+ metadata.gz: a3493555e3ce9e403bbd0d884396b0c6ccf803f7757e8a6bbab45408df54dfc6
4
+ data.tar.gz: 35e3547a5fe14792629730f271c0e6148bae8be395f82b76ff5a49994629d299
5
5
  SHA512:
6
- metadata.gz: 9dc237a79a5084bb2457faefdf885d49f5c50c5543ffa9f9c65ac0b66d7fa8454559433caae0f92d08c82dbf7124809b8810a815194ac21cd56125f5e35e4fa8
7
- data.tar.gz: 35f065275ae97da765aee7054ec77836928356d8d2efbd17fdd046f6337d71c6fb6c8ae997258c1fd7c7d60e167c2f5bf52fc1faece9cce387e8d9ba59c1e3ce
6
+ metadata.gz: 8849abd81ed97fadc7ce19778cef998b64536af4142cbf0e249d4eea777e386f565a5dc26ccb766cd78d670a627f8dc2b946880f8f24f122a474157b8d11b363
7
+ data.tar.gz: db827e4d80c96fc4dddcc79e1fea46c2f69aab1aa6afbad68222616c5c91ac21651d39a60853cb85024f22a8c0fbeae56d56c18f9a362b38f653862e06be3b7d
data/coinrpc.gemspec CHANGED
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake", ">= 13.0", "< 14.0"
24
24
  spec.add_runtime_dependency "json", "~> 2.0", ">= 2.3.1", "< 3.0"
25
25
  spec.add_runtime_dependency "oj", ">= 3.10", "< 4.0"
26
- spec.add_runtime_dependency "http", ">= 4.4", "< 5.2"
26
+ spec.add_runtime_dependency "typhoeus", ">= 1.0", "< 2.0"
27
27
  end
@@ -1,3 +1,3 @@
1
1
  module CoinRPC
2
- VERSION = "2.0.1"
2
+ VERSION = "3.0.0".freeze
3
3
  end
data/lib/coinrpc.rb CHANGED
@@ -1,26 +1,38 @@
1
- require "http"
1
+ require "uri"
2
+ require "typhoeus"
2
3
  require "oj"
3
4
  require "json"
5
+ require "securerandom"
4
6
  require "coinrpc/version"
5
7
 
6
8
  # let OJ mimic JSON
7
9
  Oj.mimic_JSON
8
10
 
9
11
  module CoinRPC
12
+
13
+ class TimeoutError < Exception; end
14
+ class UnsuccessfulError < Exception; end
15
+ class NoResponseError < Exception; end
16
+
10
17
  class Client
11
18
 
12
19
  JSONRPC_V1_1 = "1.1".freeze
13
20
  JSONRPC_V2_0 = "2.0".freeze
21
+ TIMEOUT = 60.freeze
14
22
 
15
23
  def initialize(url)
16
24
 
17
25
  urinfo = URI.parse(url)
18
26
 
19
- @client = HTTP.persistent("http://#{urinfo.host}:#{urinfo.port}").timeout(60).basic_auth({:user => urinfo.user, :pass => urinfo.password})
20
- @id = rand(1000000)
21
-
27
+ @options = {:timeout => TIMEOUT, :userpwd => "#{urinfo.user}:#{urinfo.password}", :headers => {"User-Agent" => "CoinRPC/#{CoinRPC::VERSION}"}}.freeze
28
+ @url = "http://#{urinfo.host}:#{urinfo.port}/".freeze
29
+
22
30
  end
23
-
31
+
32
+ def random_id
33
+ SecureRandom.hex(4).to_i(16)
34
+ end
35
+
24
36
  def method_missing(method, *args)
25
37
 
26
38
  fixed_method = method.to_s.gsub(/\_/,"").freeze
@@ -28,9 +40,9 @@ module CoinRPC
28
40
 
29
41
  if args[0].is_a?(Array) and args[0].size > 0 then
30
42
  # batch request
31
- post_data = args.map{|arg| {:jsonrpc => JSONRPC_V2_0, :method => fixed_method, :params => arg, :id => (@id += 1)} }
43
+ post_data = args.map{|arg| {:jsonrpc => JSONRPC_V2_0, :method => fixed_method, :params => arg, :id => random_id} }
32
44
  else
33
- post_data = {:method => fixed_method, :params => args, :jsonrpc => JSONRPC_V1_1, :id => (@id += 1)}
45
+ post_data = {:method => fixed_method, :params => args, :jsonrpc => JSONRPC_V1_1, :id => random_id}
34
46
  end
35
47
 
36
48
  api_call(post_data)
@@ -40,10 +52,14 @@ module CoinRPC
40
52
  private
41
53
  def api_call(params)
42
54
 
43
- response = @client.post("/", :body => Oj.dump(params, mode: :compat)).to_s
55
+ response = Typhoeus::Request.post(@url, :body => Oj.dump(params, :mode => :compat), **@options)
56
+
57
+ raise CoinRPC::TimeoutError if response.timed_out?
58
+ raise CoinRPC::NoResponseError if response.code.eql?(0)
59
+ raise CoinRPC::UnsuccessfulError unless response.success?
44
60
 
45
- # this won"t work without Oj.mimic_JSON
46
- result = Oj.strict_load(response, :decimal_class => BigDecimal)
61
+ # this won't work without Oj.mimic_JSON
62
+ result = Oj.strict_load(response.body, :decimal_class => BigDecimal)
47
63
 
48
64
  raise result["error"]["message"] if result.is_a?(Hash) and !result["error"].nil?
49
65
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coinrpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Atif Nazir
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-02 00:00:00.000000000 Z
11
+ date: 2022-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,25 +97,25 @@ dependencies:
97
97
  - !ruby/object:Gem::Version
98
98
  version: '4.0'
99
99
  - !ruby/object:Gem::Dependency
100
- name: http
100
+ name: typhoeus
101
101
  requirement: !ruby/object:Gem::Requirement
102
102
  requirements:
103
103
  - - ">="
104
104
  - !ruby/object:Gem::Version
105
- version: '4.4'
105
+ version: '1.0'
106
106
  - - "<"
107
107
  - !ruby/object:Gem::Version
108
- version: '5.2'
108
+ version: '2.0'
109
109
  type: :runtime
110
110
  prerelease: false
111
111
  version_requirements: !ruby/object:Gem::Requirement
112
112
  requirements:
113
113
  - - ">="
114
114
  - !ruby/object:Gem::Version
115
- version: '4.4'
115
+ version: '1.0'
116
116
  - - "<"
117
117
  - !ruby/object:Gem::Version
118
- version: '5.2'
118
+ version: '2.0'
119
119
  description: This Ruby Gem is a fast RPC client for Bitcoin Core style software, including
120
120
  Dogecoin, Litecoin, and other similar software that implements Bitcoin Core RPC
121
121
  interfaces.