jimson 0.11.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/{CHANGELOG.rdoc → CHANGELOG.md} +36 -14
- data/README.md +26 -14
- data/Rakefile +8 -8
- data/lib/jimson/client.rb +20 -18
- data/lib/jimson/client/error.rb +2 -2
- data/lib/jimson/router.rb +2 -2
- data/lib/jimson/router/map.rb +9 -7
- data/lib/jimson/server.rb +1 -1
- data/lib/jimson/version.rb +1 -1
- data/spec/client_spec.rb +58 -10
- data/spec/router_spec.rb +31 -9
- data/spec/server_spec.rb +1 -1
- metadata +15 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5745486a124bbc3d73284db9d6294aac57eb23d1815e2ad8c98f145f9fc124a9
|
4
|
+
data.tar.gz: 258d62e7212cc0c559d167aacf1f511a5cbf88e2b022ff423634537cefa570a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f2c58014cf11afea61ed91cc2bbb6e4b57bd3622ac58b2c4b4372054ce72c25938bfa72bfbbdc233c4ec1afaa698103d8e889ab22496d580c161ee99858d49e
|
7
|
+
data.tar.gz: c2b1642505d7ff5846c8cb9314be37ec95d409bf466eb126120870cb76ec5b5db0a5cb58693e4e50e932e05c148f5395890dffcd75ea397fb0d9c396fbc7a053
|
@@ -1,35 +1,57 @@
|
|
1
|
-
|
1
|
+
# 0.12.0 / 2021-05-05
|
2
|
+
|
3
|
+
* Major enhancements
|
4
|
+
|
5
|
+
* Add ability to specify custom content type
|
6
|
+
* Add support for named parameters
|
7
|
+
* Add Router :ns_sep option for custom namespace
|
8
|
+
* made it possible to overwrite advanced RestClient values, like ssl_ca_file
|
9
|
+
|
10
|
+
* Minor enhancements
|
11
|
+
|
12
|
+
* Unified Bignum and Fixnum to Integer
|
13
|
+
* Add data returned to invalid response exception
|
14
|
+
* Fix json variable name on invalid json response
|
15
|
+
|
16
|
+
# 0.11.0 / 2016-03-13
|
17
|
+
|
18
|
+
* Minor enhancements
|
19
|
+
|
20
|
+
* Update dependency versions for Ruby 2.*
|
21
|
+
* Remove Gemfile.lock
|
22
|
+
|
23
|
+
# 0.10.0 / 2013-06-28
|
2
24
|
|
3
25
|
* Minor enhancements
|
4
26
|
|
5
27
|
* Update dependency versions
|
6
28
|
|
7
|
-
|
29
|
+
# 0.9.1 / 2012-09-18
|
8
30
|
|
9
31
|
* Bug fixes
|
10
32
|
|
11
33
|
* Allow opts to be passed to Server.with_routes
|
12
34
|
|
13
|
-
|
35
|
+
# 0.9.0 / 2012-08-22
|
14
36
|
|
15
37
|
* Minor enhancements
|
16
38
|
|
17
39
|
* Add show_errors option to server, which will cause application errors to include the error name and the first line of the backtrace
|
18
40
|
|
19
|
-
|
41
|
+
# 0.8.0 / 2012-08-17
|
20
42
|
|
21
43
|
* Major enhancements
|
22
44
|
|
23
45
|
* Add namespaced method calls to client (e.g. 'client[:foo].sum(1,2,3) # calls foo.sum')
|
24
46
|
* Add Server.with_routes to quickly created a routed server
|
25
47
|
|
26
|
-
|
48
|
+
# 0.7.1 / 2012-08-16
|
27
49
|
|
28
50
|
* Bug fixes
|
29
51
|
|
30
52
|
* Fix handling of array params in client, which were erroneously being flattened
|
31
53
|
|
32
|
-
|
54
|
+
# 0.7.0 / 2012-04-13
|
33
55
|
|
34
56
|
* Major enhancements
|
35
57
|
|
@@ -39,13 +61,13 @@
|
|
39
61
|
|
40
62
|
* Fix deprecation warning about RDoc task in Rakefile
|
41
63
|
|
42
|
-
|
64
|
+
# 0.6.0 / 2012-03-14
|
43
65
|
|
44
66
|
* Minor enhancements
|
45
67
|
|
46
68
|
* Add ability to pass options to Rack and RestClient
|
47
69
|
|
48
|
-
|
70
|
+
# 0.5.0 / 2012-03-06
|
49
71
|
|
50
72
|
* Major enhancements
|
51
73
|
|
@@ -55,37 +77,37 @@
|
|
55
77
|
|
56
78
|
* Allow BigNum in 'id' field of request and response
|
57
79
|
|
58
|
-
|
80
|
+
# 0.3.1 / 2011-08-11
|
59
81
|
|
60
82
|
* Minor enhancements
|
61
83
|
|
62
84
|
* Refactor the way the server is intantiated/started to work better with config.ru
|
63
85
|
|
64
|
-
|
86
|
+
# 0.3.0 / 2011-08-11
|
65
87
|
|
66
88
|
* Major enhancements
|
67
89
|
|
68
90
|
* Replace eventmachine-httpserver with rack for more cross-platform goodness
|
69
91
|
|
70
|
-
|
92
|
+
# 0.2.3 / 2011-08-01
|
71
93
|
|
72
94
|
* Bug fixes
|
73
95
|
|
74
96
|
* Fix argument error in client error handling
|
75
97
|
|
76
|
-
|
98
|
+
# 0.2.2 / 2011-07-28
|
77
99
|
|
78
100
|
* Bug fixes
|
79
101
|
|
80
102
|
* Fix invalid local variable error in client error handling
|
81
103
|
|
82
|
-
|
104
|
+
# 0.2.1 / 2011-07-27
|
83
105
|
|
84
106
|
* Bug fixes
|
85
107
|
|
86
108
|
* Fix error in client handling some errors caused by errant 'new' keyword
|
87
109
|
|
88
|
-
|
110
|
+
# 0.2.0 / 2011-07-20
|
89
111
|
|
90
112
|
* Major enhancements
|
91
113
|
|
data/README.md
CHANGED
@@ -1,29 +1,41 @@
|
|
1
1
|
# Jimson
|
2
|
+
|
2
3
|
### JSON-RPC 2.0 Client and Server for Ruby
|
3
|
-
|
4
|
+
|
5
|
+
[![Build Status](https://github.com/chriskite/jimson/actions/workflows/ruby.yml/badge.svg?branch=main)](https://github.com/chriskite/jimson/actions/workflows/ruby.yml)
|
4
6
|
|
5
7
|
## Client: Quick Start
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
require 'jimson'
|
11
|
+
client = Jimson::Client.new("http://www.example.com:8999") # the URL for the JSON-RPC 2.0 server to connect to
|
12
|
+
result = client.sum(1,2) # call the 'sum' method on the RPC server and save the result '3'
|
13
|
+
```
|
9
14
|
|
10
15
|
## Server: Quick Start
|
11
|
-
require 'jimson'
|
12
16
|
|
13
|
-
|
14
|
-
|
17
|
+
```ruby
|
18
|
+
require 'jimson'
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
+
class MyHandler
|
21
|
+
extend Jimson::Handler
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
def sum(a,b)
|
24
|
+
a + b
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
server = Jimson::Server.new(MyHandler.new)
|
29
|
+
server.start # serve with webrick on http://0.0.0.0:8999/
|
30
|
+
```
|
23
31
|
|
24
32
|
## JSON Engine
|
33
|
+
|
25
34
|
Jimson uses multi\_json, so you can load the JSON library of your choice in your application and Jimson will use it automatically.
|
26
35
|
|
27
36
|
For example, require the 'json' gem in your application:
|
28
|
-
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'json'
|
40
|
+
```
|
29
41
|
|
data/Rakefile
CHANGED
@@ -1,26 +1,26 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/lib/')
|
2
|
+
require 'jimson/version'
|
1
3
|
require 'rubygems'
|
2
4
|
require 'rake'
|
3
5
|
require 'rspec/core/rake_task'
|
4
6
|
|
7
|
+
gem 'rubygems-tasks', '~> 0.2'
|
8
|
+
require 'rubygems/tasks'
|
9
|
+
|
10
|
+
Gem::Tasks.new
|
11
|
+
|
5
12
|
desc "Run all specs"
|
6
13
|
RSpec::Core::RakeTask.new(:rspec) do |spec|
|
7
14
|
spec.pattern = 'spec/**/*_spec.rb'
|
8
15
|
end
|
9
16
|
|
10
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
11
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
12
|
-
spec.rcov = true
|
13
|
-
end
|
14
|
-
|
15
17
|
task :default => :rspec
|
16
18
|
|
17
19
|
require 'rdoc/task'
|
18
20
|
|
19
21
|
Rake::RDocTask.new do |rdoc|
|
20
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
21
|
-
|
22
22
|
rdoc.rdoc_dir = 'rdoc'
|
23
|
-
rdoc.title = "jimson #{
|
23
|
+
rdoc.title = "jimson #{Jimson::VERSION}"
|
24
24
|
rdoc.rdoc_files.include('README*')
|
25
25
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
26
26
|
end
|
data/lib/jimson/client.rb
CHANGED
@@ -12,13 +12,14 @@ module Jimson
|
|
12
12
|
rand(10**12)
|
13
13
|
end
|
14
14
|
|
15
|
-
def initialize(url, opts = {}, namespace = nil)
|
15
|
+
def initialize(url, opts = {}, namespace = nil, client_opts = {})
|
16
16
|
@url = url
|
17
17
|
URI.parse(@url) # for the sake of validating the url
|
18
18
|
@batch = []
|
19
19
|
@opts = opts
|
20
20
|
@namespace = namespace
|
21
|
-
@opts[:content_type]
|
21
|
+
@opts[:content_type] ||= 'application/json'
|
22
|
+
@client_opts = client_opts
|
22
23
|
end
|
23
24
|
|
24
25
|
def process_call(sym, args)
|
@@ -45,9 +46,9 @@ module Jimson
|
|
45
46
|
'params' => args,
|
46
47
|
'id' => self.class.make_id
|
47
48
|
})
|
48
|
-
resp = RestClient.
|
49
|
+
resp = RestClient::Request.execute(@client_opts.merge(:method => :post, :url => @url, :payload => post_data, :headers => @opts))
|
49
50
|
if resp.nil? || resp.body.nil? || resp.body.empty?
|
50
|
-
raise Client::Error::InvalidResponse.new
|
51
|
+
raise Client::Error::InvalidResponse.new(resp)
|
51
52
|
end
|
52
53
|
|
53
54
|
return resp.body
|
@@ -55,9 +56,9 @@ module Jimson
|
|
55
56
|
|
56
57
|
def send_batch_request(batch)
|
57
58
|
post_data = MultiJson.encode(batch)
|
58
|
-
resp = RestClient.
|
59
|
+
resp = RestClient::Request.execute(@client_opts.merge(:method => :post, :url => @url, :payload => post_data, :headers => @opts))
|
59
60
|
if resp.nil? || resp.body.nil? || resp.body.empty?
|
60
|
-
raise Client::Error::InvalidResponse.new
|
61
|
+
raise Client::Error::InvalidResponse.new(resp)
|
61
62
|
end
|
62
63
|
|
63
64
|
return resp.body
|
@@ -72,7 +73,7 @@ module Jimson
|
|
72
73
|
end
|
73
74
|
|
74
75
|
def process_single_response(data)
|
75
|
-
raise Client::Error::InvalidResponse.new if !valid_response?(data)
|
76
|
+
raise Client::Error::InvalidResponse.new(data) if !valid_response?(data)
|
76
77
|
|
77
78
|
if !!data['error']
|
78
79
|
code = data['error']['code']
|
@@ -93,17 +94,17 @@ module Jimson
|
|
93
94
|
return false if data.has_key?('error') && data.has_key?('result')
|
94
95
|
|
95
96
|
if data.has_key?('error')
|
96
|
-
if !data['error'].is_a?(Hash) || !data['error'].has_key?('code') || !data['error'].has_key?('message')
|
97
|
+
if !data['error'].is_a?(Hash) || !data['error'].has_key?('code') || !data['error'].has_key?('message')
|
97
98
|
return false
|
98
99
|
end
|
99
100
|
|
100
|
-
if !data['error']['code'].is_a?(
|
101
|
+
if !data['error']['code'].is_a?(Integer) || !data['error']['message'].is_a?(String)
|
101
102
|
return false
|
102
103
|
end
|
103
104
|
end
|
104
105
|
|
105
106
|
return true
|
106
|
-
|
107
|
+
|
107
108
|
rescue
|
108
109
|
return false
|
109
110
|
end
|
@@ -116,13 +117,13 @@ module Jimson
|
|
116
117
|
end
|
117
118
|
|
118
119
|
def send_batch
|
119
|
-
batch = @batch.map(&:first) # get the requests
|
120
|
+
batch = @batch.map(&:first) # get the requests
|
120
121
|
response = send_batch_request(batch)
|
121
122
|
|
122
123
|
begin
|
123
124
|
responses = MultiJson.decode(response)
|
124
125
|
rescue
|
125
|
-
raise Client::Error::InvalidJSON.new(
|
126
|
+
raise Client::Error::InvalidJSON.new(response)
|
126
127
|
end
|
127
128
|
|
128
129
|
process_batch_response(responses)
|
@@ -132,14 +133,14 @@ module Jimson
|
|
132
133
|
end
|
133
134
|
|
134
135
|
class BatchClient < BlankSlate
|
135
|
-
|
136
|
+
|
136
137
|
def initialize(helper)
|
137
138
|
@helper = helper
|
138
139
|
end
|
139
140
|
|
140
141
|
def method_missing(sym, *args, &block)
|
141
142
|
request = Jimson::Request.new(sym.to_s, args)
|
142
|
-
@helper.push_batch_request(request)
|
143
|
+
@helper.push_batch_request(request)
|
143
144
|
end
|
144
145
|
|
145
146
|
end
|
@@ -148,7 +149,7 @@ module Jimson
|
|
148
149
|
reveal :instance_variable_get
|
149
150
|
reveal :inspect
|
150
151
|
reveal :to_s
|
151
|
-
|
152
|
+
|
152
153
|
def self.batch(client)
|
153
154
|
helper = client.instance_variable_get(:@helper)
|
154
155
|
batch_client = BatchClient.new(helper)
|
@@ -156,12 +157,13 @@ module Jimson
|
|
156
157
|
helper.send_batch
|
157
158
|
end
|
158
159
|
|
159
|
-
def initialize(url, opts = {}, namespace = nil)
|
160
|
-
@url, @opts, @namespace = url, opts, namespace
|
161
|
-
@helper = ClientHelper.new(url, opts, namespace)
|
160
|
+
def initialize(url, opts = {}, namespace = nil, client_opts = {})
|
161
|
+
@url, @opts, @namespace, @client_opts = url, opts, namespace, client_opts
|
162
|
+
@helper = ClientHelper.new(url, opts, namespace, client_opts)
|
162
163
|
end
|
163
164
|
|
164
165
|
def method_missing(sym, *args, &block)
|
166
|
+
args = args.first if args.size == 1 && args.first.is_a?(Hash)
|
165
167
|
@helper.process_call(sym, args)
|
166
168
|
end
|
167
169
|
|
data/lib/jimson/client/error.rb
CHANGED
@@ -2,8 +2,8 @@ module Jimson
|
|
2
2
|
class Client
|
3
3
|
module Error
|
4
4
|
class InvalidResponse < StandardError
|
5
|
-
def initialize()
|
6
|
-
super(
|
5
|
+
def initialize(response = nil)
|
6
|
+
super("Invalid or empty response from server:\n#{response.inspect}")
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
data/lib/jimson/router.rb
CHANGED
data/lib/jimson/router/map.rb
CHANGED
@@ -7,8 +7,10 @@ module Jimson
|
|
7
7
|
#
|
8
8
|
class Map
|
9
9
|
|
10
|
-
def initialize
|
10
|
+
def initialize(opts = {})
|
11
11
|
@routes = {}
|
12
|
+
@opts = opts
|
13
|
+
@ns_sep = opts[:ns_sep] || '.'
|
12
14
|
end
|
13
15
|
|
14
16
|
#
|
@@ -28,7 +30,7 @@ module Jimson
|
|
28
30
|
@routes[ns.to_s] = handler
|
29
31
|
else
|
30
32
|
# passed a block for nested namespacing
|
31
|
-
map = Jimson::Router::Map.new
|
33
|
+
map = Jimson::Router::Map.new(@opts)
|
32
34
|
@routes[ns.to_s] = map
|
33
35
|
map.instance_eval &block
|
34
36
|
end
|
@@ -38,11 +40,11 @@ module Jimson
|
|
38
40
|
# Return the handler for a (possibly namespaced) method name
|
39
41
|
#
|
40
42
|
def handler_for_method(method)
|
41
|
-
parts = method.split(
|
42
|
-
ns = (method.index(
|
43
|
+
parts = method.split(@ns_sep)
|
44
|
+
ns = (method.index(@ns_sep) == nil ? '' : parts.first)
|
43
45
|
handler = @routes[ns]
|
44
46
|
if handler.is_a?(Jimson::Router::Map)
|
45
|
-
return handler.handler_for_method(parts[1..-1].join(
|
47
|
+
return handler.handler_for_method(parts[1..-1].join(@ns_sep))
|
46
48
|
end
|
47
49
|
handler
|
48
50
|
end
|
@@ -51,7 +53,7 @@ module Jimson
|
|
51
53
|
# Strip off the namespace part of a method and return the bare method name
|
52
54
|
#
|
53
55
|
def strip_method_namespace(method)
|
54
|
-
method.split(
|
56
|
+
method.split(@ns_sep).last
|
55
57
|
end
|
56
58
|
|
57
59
|
#
|
@@ -59,7 +61,7 @@ module Jimson
|
|
59
61
|
#
|
60
62
|
def jimson_methods
|
61
63
|
arr = @routes.keys.map do |ns|
|
62
|
-
prefix = (ns == '' ? '' : "#{ns}
|
64
|
+
prefix = (ns == '' ? '' : "#{ns}#{@ns_sep}")
|
63
65
|
handler = @routes[ns]
|
64
66
|
if handler.is_a?(Jimson::Router::Map)
|
65
67
|
handler.jimson_methods
|
data/lib/jimson/server.rb
CHANGED
data/lib/jimson/version.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -36,7 +36,7 @@ module Jimson
|
|
36
36
|
'id' => 1
|
37
37
|
})
|
38
38
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
39
|
-
RestClient.should_receive(:
|
39
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
40
40
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
41
41
|
@client[:foo].sum(1, 2, 3).should == 42
|
42
42
|
end
|
@@ -50,7 +50,7 @@ module Jimson
|
|
50
50
|
'id' => 1
|
51
51
|
})
|
52
52
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
53
|
-
RestClient.should_receive(:
|
53
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
54
54
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
55
55
|
@client[:foo][:bar].sum(1, 2, 3).should == 42
|
56
56
|
end
|
@@ -66,7 +66,7 @@ module Jimson
|
|
66
66
|
'id' => 1
|
67
67
|
})
|
68
68
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
69
|
-
RestClient.should_receive(:
|
69
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
70
70
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
71
71
|
@client['foo', 1, 2, 3].should == 42
|
72
72
|
end
|
@@ -80,7 +80,7 @@ module Jimson
|
|
80
80
|
'id' => 1
|
81
81
|
})
|
82
82
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
83
|
-
RestClient.should_receive(:
|
83
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
84
84
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
85
85
|
@client['foo', [1, 2], 3].should == 42
|
86
86
|
end
|
@@ -100,7 +100,7 @@ module Jimson
|
|
100
100
|
end
|
101
101
|
it "sends a valid JSON-RPC request and returns the result" do
|
102
102
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
103
|
-
RestClient.should_receive(:
|
103
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: @expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
104
104
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
105
105
|
client = Client.new(SPEC_URL)
|
106
106
|
client.foo(1,2,3).should == 42
|
@@ -108,11 +108,27 @@ module Jimson
|
|
108
108
|
|
109
109
|
it "sends a valid JSON-RPC request with custom options" do
|
110
110
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
111
|
-
RestClient.should_receive(:
|
111
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: @expected, headers: {:content_type => 'application/json', :timeout => 10000}).and_return(@resp_mock)
|
112
112
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
113
113
|
client = Client.new(SPEC_URL, :timeout => 10000)
|
114
114
|
client.foo(1,2,3).should == 42
|
115
115
|
end
|
116
|
+
|
117
|
+
it "sends a valid JSON-RPC request with custom content_type" do
|
118
|
+
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
119
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: @expected, headers: {:content_type => 'application/json-rpc', :timeout => 10000}).and_return(@resp_mock)
|
120
|
+
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
121
|
+
client = Client.new(SPEC_URL, :timeout => 10000, :content_type => 'application/json-rpc')
|
122
|
+
client.foo(1,2,3).should == 42
|
123
|
+
end
|
124
|
+
|
125
|
+
it "adds RestClient::Request parameters if given" do
|
126
|
+
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
127
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: @expected, headers: {:content_type => 'application/json-rpc', :timeout => 10000}, ssl_ca_file: 'myca.pem').and_return(@resp_mock)
|
128
|
+
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
129
|
+
client = Client.new(SPEC_URL, {:timeout => 10000, :content_type => 'application/json-rpc'}, '', {ssl_ca_file: 'myca.pem'})
|
130
|
+
client.foo(1,2,3).should == 42
|
131
|
+
end
|
116
132
|
end
|
117
133
|
|
118
134
|
context "when one of the parameters is an array" do
|
@@ -124,12 +140,44 @@ module Jimson
|
|
124
140
|
'id' => 1
|
125
141
|
})
|
126
142
|
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
127
|
-
RestClient.should_receive(:
|
143
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
128
144
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
129
145
|
client = Client.new(SPEC_URL)
|
130
146
|
client.foo([1,2],3).should == 42
|
131
147
|
end
|
132
148
|
end
|
149
|
+
|
150
|
+
context "when one of the parameters is a hash" do
|
151
|
+
it "sends a correct JSON-RPC request (array is preserved with hash) and returns the results" do
|
152
|
+
expected = MultiJson.encode({
|
153
|
+
'jsonrpc' => '2.0',
|
154
|
+
'method' => 'foo',
|
155
|
+
'params' => [1,{'bar' => 'baz'},3],
|
156
|
+
'id' => 1
|
157
|
+
})
|
158
|
+
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
159
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
160
|
+
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
161
|
+
client = Client.new(SPEC_URL)
|
162
|
+
client.foo(1, {'bar' => 'baz'}, 3).should == 42
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context "when using named parameters" do
|
167
|
+
it "sends a correct JSON-RPC request (named parameters are preserved) and returns the result" do
|
168
|
+
expected = MultiJson.encode({
|
169
|
+
'jsonrpc' => '2.0',
|
170
|
+
'method' => 'foo',
|
171
|
+
'params' => {'bar' => 'baz'},
|
172
|
+
'id' => 1
|
173
|
+
})
|
174
|
+
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
175
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: expected, headers: {:content_type => 'application/json'}).and_return(@resp_mock)
|
176
|
+
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
177
|
+
client = Client.new(SPEC_URL)
|
178
|
+
client.foo({'bar' => 'baz'}).should == 42
|
179
|
+
end
|
180
|
+
end
|
133
181
|
end
|
134
182
|
|
135
183
|
describe "sending a batch request" do
|
@@ -138,7 +186,7 @@ module Jimson
|
|
138
186
|
{"jsonrpc" => "2.0", "method" => "sum", "params" => [1,2,4], "id" => "1"},
|
139
187
|
{"jsonrpc" => "2.0", "method" => "subtract", "params" => [42,23], "id" => "2"},
|
140
188
|
{"jsonrpc" => "2.0", "method" => "foo_get", "params" => [{"name" => "myself"}], "id" => "5"},
|
141
|
-
{"jsonrpc" => "2.0", "method" => "get_data", "id" => "9"}
|
189
|
+
{"jsonrpc" => "2.0", "method" => "get_data", "id" => "9"}
|
142
190
|
])
|
143
191
|
|
144
192
|
response = MultiJson.encode([
|
@@ -149,9 +197,9 @@ module Jimson
|
|
149
197
|
])
|
150
198
|
|
151
199
|
ClientHelper.stub(:make_id).and_return('1', '2', '5', '9')
|
152
|
-
RestClient.should_receive(:
|
200
|
+
RestClient::Request.should_receive(:execute).with(method: :post, url: SPEC_URL, payload: batch, headers: {:content_type => 'application/json'}, ssl_ca_file: 'myca.pem').and_return(@resp_mock)
|
153
201
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
154
|
-
client = Client.new(SPEC_URL)
|
202
|
+
client = Client.new(SPEC_URL, {}, '', {ssl_ca_file: 'myca.pem'})
|
155
203
|
|
156
204
|
sum = subtract = foo = data = nil
|
157
205
|
Jimson::Client.batch(client) do |batch|
|
data/spec/router_spec.rb
CHANGED
@@ -3,7 +3,8 @@ require 'spec_helper'
|
|
3
3
|
module Jimson
|
4
4
|
describe Router do
|
5
5
|
|
6
|
-
let(:router) { Router.new }
|
6
|
+
let(:router) { Router.new(opts) }
|
7
|
+
let(:opts) { {} }
|
7
8
|
|
8
9
|
class RouterFooHandler
|
9
10
|
extend Jimson::Handler
|
@@ -44,7 +45,7 @@ module Jimson
|
|
44
45
|
end
|
45
46
|
|
46
47
|
context 'when given nested namespaces' do
|
47
|
-
|
48
|
+
before {
|
48
49
|
router.draw do
|
49
50
|
root RouterFooHandler
|
50
51
|
namespace 'ns1' do
|
@@ -52,22 +53,43 @@ module Jimson
|
|
52
53
|
namespace 'ns2', RouterBarHandler
|
53
54
|
end
|
54
55
|
end
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
}
|
57
|
+
context 'default ns_sep' do
|
58
|
+
it 'takes a block with a DSL to set the root and namespaces' do
|
59
|
+
router.handler_for_method('hi').should be_a(RouterFooHandler)
|
60
|
+
router.handler_for_method('ns1.hi').should be_a(RouterBazHandler)
|
61
|
+
router.handler_for_method('ns1.ns2.hi').should be_a(RouterBarHandler)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
context 'custom ns_sep' do
|
65
|
+
let(:opts) { {ns_sep: '::'} }
|
66
|
+
it 'takes a block with a DSL to set the root and namespaces' do
|
67
|
+
router.handler_for_method('hi').should be_a(RouterFooHandler)
|
68
|
+
router.handler_for_method('ns1::hi').should be_a(RouterBazHandler)
|
69
|
+
router.handler_for_method('ns1::ns2::hi').should be_a(RouterBarHandler)
|
70
|
+
end
|
59
71
|
end
|
72
|
+
|
60
73
|
end
|
61
74
|
end
|
62
75
|
|
63
76
|
describe '#jimson_methods' do
|
64
|
-
|
77
|
+
before {
|
65
78
|
router.draw do
|
66
79
|
root RouterFooHandler
|
67
80
|
namespace 'foo', RouterBarHandler
|
68
81
|
end
|
69
|
-
|
70
|
-
|
82
|
+
}
|
83
|
+
context 'default ns_sep' do
|
84
|
+
it 'returns an array of namespaced method names from all registered handlers' do
|
85
|
+
router.jimson_methods.sort.should == ['hi', 'foo.bye'].sort
|
86
|
+
end
|
87
|
+
end
|
88
|
+
context 'custom ns_sep' do
|
89
|
+
let(:opts) { {ns_sep: '::'} }
|
90
|
+
it 'returns an array of namespaced method names from all registered handlers' do
|
91
|
+
router.jimson_methods.sort.should == ['hi', 'foo::bye'].sort
|
92
|
+
end
|
71
93
|
end
|
72
94
|
end
|
73
95
|
|
data/spec/server_spec.rb
CHANGED
metadata
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jimson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Kite
|
8
|
-
|
8
|
+
- Gilbert
|
9
|
+
- Bodo Tasche
|
10
|
+
autorequire:
|
9
11
|
bindir: bin
|
10
12
|
cert_chain: []
|
11
|
-
date:
|
13
|
+
date: 2021-05-05 00:00:00.000000000 Z
|
12
14
|
dependencies:
|
13
15
|
- !ruby/object:Gem::Dependency
|
14
16
|
name: blankslate
|
15
17
|
requirement: !ruby/object:Gem::Requirement
|
16
18
|
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '3.1'
|
20
19
|
- - ">="
|
21
20
|
- !ruby/object:Gem::Version
|
22
21
|
version: 3.1.3
|
@@ -24,9 +23,6 @@ dependencies:
|
|
24
23
|
prerelease: false
|
25
24
|
version_requirements: !ruby/object:Gem::Requirement
|
26
25
|
requirements:
|
27
|
-
- - "~>"
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '3.1'
|
30
26
|
- - ">="
|
31
27
|
- !ruby/object:Gem::Version
|
32
28
|
version: 3.1.3
|
@@ -34,9 +30,6 @@ dependencies:
|
|
34
30
|
name: rest-client
|
35
31
|
requirement: !ruby/object:Gem::Requirement
|
36
32
|
requirements:
|
37
|
-
- - "~>"
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
version: '1'
|
40
33
|
- - ">="
|
41
34
|
- !ruby/object:Gem::Version
|
42
35
|
version: 1.7.3
|
@@ -44,9 +37,6 @@ dependencies:
|
|
44
37
|
prerelease: false
|
45
38
|
version_requirements: !ruby/object:Gem::Requirement
|
46
39
|
requirements:
|
47
|
-
- - "~>"
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version: '1'
|
50
40
|
- - ">="
|
51
41
|
- !ruby/object:Gem::Version
|
52
42
|
version: 1.7.3
|
@@ -54,9 +44,6 @@ dependencies:
|
|
54
44
|
name: multi_json
|
55
45
|
requirement: !ruby/object:Gem::Requirement
|
56
46
|
requirements:
|
57
|
-
- - "~>"
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
version: '1'
|
60
47
|
- - ">="
|
61
48
|
- !ruby/object:Gem::Version
|
62
49
|
version: 1.11.2
|
@@ -64,9 +51,6 @@ dependencies:
|
|
64
51
|
prerelease: false
|
65
52
|
version_requirements: !ruby/object:Gem::Requirement
|
66
53
|
requirements:
|
67
|
-
- - "~>"
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: '1'
|
70
54
|
- - ">="
|
71
55
|
- !ruby/object:Gem::Version
|
72
56
|
version: 1.11.2
|
@@ -74,9 +58,6 @@ dependencies:
|
|
74
58
|
name: rack
|
75
59
|
requirement: !ruby/object:Gem::Requirement
|
76
60
|
requirements:
|
77
|
-
- - "~>"
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
version: '1'
|
80
61
|
- - ">="
|
81
62
|
- !ruby/object:Gem::Version
|
82
63
|
version: 1.4.5
|
@@ -84,9 +65,6 @@ dependencies:
|
|
84
65
|
prerelease: false
|
85
66
|
version_requirements: !ruby/object:Gem::Requirement
|
86
67
|
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '1'
|
90
68
|
- - ">="
|
91
69
|
- !ruby/object:Gem::Version
|
92
70
|
version: 1.4.5
|
@@ -158,14 +136,14 @@ dependencies:
|
|
158
136
|
- - ">="
|
159
137
|
- !ruby/object:Gem::Version
|
160
138
|
version: 4.2.2
|
161
|
-
description:
|
162
|
-
email:
|
139
|
+
description:
|
140
|
+
email:
|
163
141
|
executables: []
|
164
142
|
extensions: []
|
165
143
|
extra_rdoc_files:
|
166
144
|
- README.md
|
167
145
|
files:
|
168
|
-
- CHANGELOG.
|
146
|
+
- CHANGELOG.md
|
169
147
|
- LICENSE.txt
|
170
148
|
- README.md
|
171
149
|
- Rakefile
|
@@ -185,10 +163,11 @@ files:
|
|
185
163
|
- spec/router_spec.rb
|
186
164
|
- spec/server_spec.rb
|
187
165
|
- spec/spec_helper.rb
|
188
|
-
homepage:
|
189
|
-
licenses:
|
166
|
+
homepage: https://github.com/bitboxer/jimson.git
|
167
|
+
licenses:
|
168
|
+
- MIT
|
190
169
|
metadata: {}
|
191
|
-
post_install_message:
|
170
|
+
post_install_message:
|
192
171
|
rdoc_options: []
|
193
172
|
require_paths:
|
194
173
|
- lib
|
@@ -203,14 +182,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
203
182
|
- !ruby/object:Gem::Version
|
204
183
|
version: '0'
|
205
184
|
requirements: []
|
206
|
-
|
207
|
-
|
208
|
-
signing_key:
|
185
|
+
rubygems_version: 3.1.2
|
186
|
+
signing_key:
|
209
187
|
specification_version: 4
|
210
188
|
summary: JSON-RPC 2.0 client and server
|
211
189
|
test_files:
|
190
|
+
- spec/router_spec.rb
|
212
191
|
- spec/client_spec.rb
|
213
192
|
- spec/handler_spec.rb
|
214
|
-
- spec/router_spec.rb
|
215
193
|
- spec/spec_helper.rb
|
216
194
|
- spec/server_spec.rb
|