jsonrpc-client 0.0.3 → 0.0.4
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.
- data/lib/jsonrpc/client.rb +17 -2
- data/lib/jsonrpc/version.rb +1 -1
- data/spec/client_spec.rb +6 -6
- metadata +4 -4
data/lib/jsonrpc/client.rb
CHANGED
@@ -6,6 +6,14 @@ require 'jsonrpc/error'
|
|
6
6
|
require 'jsonrpc/version'
|
7
7
|
|
8
8
|
module JSONRPC
|
9
|
+
def self.logger=(logger)
|
10
|
+
@logger = logger
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.logger
|
14
|
+
@logger
|
15
|
+
end
|
16
|
+
|
9
17
|
class Base < BasicObject
|
10
18
|
JSON_RPC_VERSION = '2.0'
|
11
19
|
|
@@ -61,7 +69,10 @@ module JSONRPC
|
|
61
69
|
private
|
62
70
|
def send_batch_request(batch)
|
63
71
|
post_data = ::MultiJson.encode(batch)
|
64
|
-
resp = ::Faraday.
|
72
|
+
resp = ::Faraday.new { |connection|
|
73
|
+
connection.response :logger, ::JSONRPC.logger
|
74
|
+
connection.adapter ::Faraday.default_adapter
|
75
|
+
}.post(@url, post_data, @opts)
|
65
76
|
if resp.nil? || resp.body.nil? || resp.body.empty?
|
66
77
|
raise ::JSONRPC::Error::InvalidResponse.new
|
67
78
|
end
|
@@ -127,7 +138,11 @@ module JSONRPC
|
|
127
138
|
'params' => args,
|
128
139
|
'id' => ::JSONRPC::Base.make_id
|
129
140
|
})
|
130
|
-
resp = ::Faraday.
|
141
|
+
resp = ::Faraday.new { |connection|
|
142
|
+
connection.response :logger, ::JSONRPC.logger
|
143
|
+
connection.adapter ::Faraday.default_adapter
|
144
|
+
}.post(@url, post_data, (options || {}).merge(@opts))
|
145
|
+
|
131
146
|
if resp.nil? || resp.body.nil? || resp.body.empty?
|
132
147
|
raise ::JSONRPC::Error::InvalidResponse.new
|
133
148
|
end
|
data/lib/jsonrpc/version.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -39,14 +39,14 @@ module JSONRPC
|
|
39
39
|
|
40
40
|
context "when using an array of args" do
|
41
41
|
it "sends a request with the correct method and args" do
|
42
|
-
Faraday.should_receive(:post).with(SPEC_URL, expected, {:content_type => 'application/json'}).and_return(@resp_mock)
|
42
|
+
Faraday::Connection.any_instance.should_receive(:post).with(SPEC_URL, expected, {:content_type => 'application/json'}).and_return(@resp_mock)
|
43
43
|
@client.invoke('foo', [1, 2, 3]).should == 42
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
context "with headers" do
|
48
48
|
it "adds additional headers" do
|
49
|
-
Faraday.should_receive(:post).with(SPEC_URL, expected, {:content_type => 'application/json', "X-FOO" => "BAR"}).and_return(@resp_mock)
|
49
|
+
Faraday::Connection.any_instance.should_receive(:post).with(SPEC_URL, expected, {:content_type => 'application/json', "X-FOO" => "BAR"}).and_return(@resp_mock)
|
50
50
|
@client.invoke('foo', [1, 2, 3], "X-FOO" => "BAR").should == 42
|
51
51
|
end
|
52
52
|
end
|
@@ -64,7 +64,7 @@ module JSONRPC
|
|
64
64
|
end
|
65
65
|
it "sends a valid JSON-RPC request and returns the result" do
|
66
66
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
67
|
-
Faraday.should_receive(:post).with(SPEC_URL, @expected, {:content_type => 'application/json'}).and_return(@resp_mock)
|
67
|
+
Faraday::Connection.any_instance.should_receive(:post).with(SPEC_URL, @expected, {:content_type => 'application/json'}).and_return(@resp_mock)
|
68
68
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
69
69
|
client = Client.new(SPEC_URL)
|
70
70
|
client.foo(1,2,3).should == 42
|
@@ -72,7 +72,7 @@ module JSONRPC
|
|
72
72
|
|
73
73
|
it "sends a valid JSON-RPC request with custom options" do
|
74
74
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
75
|
-
Faraday.should_receive(:post).with(SPEC_URL, @expected, {:content_type => 'application/json', :timeout => 10000}).and_return(@resp_mock)
|
75
|
+
Faraday::Connection.any_instance.should_receive(:post).with(SPEC_URL, @expected, {:content_type => 'application/json', :timeout => 10000}).and_return(@resp_mock)
|
76
76
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
77
77
|
client = Client.new(SPEC_URL, :timeout => 10000)
|
78
78
|
client.foo(1,2,3).should == 42
|
@@ -80,7 +80,7 @@ module JSONRPC
|
|
80
80
|
|
81
81
|
it "sends a valid JSON-RPC request with custom content_type" do
|
82
82
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
83
|
-
Faraday.should_receive(:post).with(SPEC_URL, @expected, {:content_type => 'application/json-rpc', :timeout => 10000}).and_return(@resp_mock)
|
83
|
+
Faraday::Connection.any_instance.should_receive(:post).with(SPEC_URL, @expected, {:content_type => 'application/json-rpc', :timeout => 10000}).and_return(@resp_mock)
|
84
84
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
85
85
|
client = Client.new(SPEC_URL, :timeout => 10000, :content_type => 'application/json-rpc')
|
86
86
|
client.foo(1,2,3).should == 42
|
@@ -105,7 +105,7 @@ module JSONRPC
|
|
105
105
|
])
|
106
106
|
|
107
107
|
Base.stub!(:make_id).and_return('1', '2', '5', '9')
|
108
|
-
Faraday.should_receive(:post).with(SPEC_URL, batch, {:content_type => 'application/json'}).and_return(@resp_mock)
|
108
|
+
Faraday::Connection.any_instance.should_receive(:post).with(SPEC_URL, batch, {:content_type => 'application/json'}).and_return(@resp_mock)
|
109
109
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
110
110
|
client = Client.new(SPEC_URL)
|
111
111
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonrpc-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
segments:
|
112
112
|
- 0
|
113
|
-
hash:
|
113
|
+
hash: 2230552126118589928
|
114
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
115
|
none: false
|
116
116
|
requirements:
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash:
|
122
|
+
hash: 2230552126118589928
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
125
|
rubygems_version: 1.8.24
|