jimson 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/Rakefile +1 -5
- data/VERSION +1 -1
- data/lib/jimson/client.rb +12 -4
- data/lib/jimson/router/map.rb +2 -0
- data/lib/jimson/server.rb +9 -0
- data/spec/client_spec.rb +30 -0
- data/spec/router_spec.rb +20 -12
- data/spec/server_spec.rb +28 -0
- metadata +113 -99
data/CHANGELOG.rdoc
CHANGED
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/lib/jimson/client.rb
CHANGED
@@ -12,11 +12,12 @@ module Jimson
|
|
12
12
|
rand(10**12)
|
13
13
|
end
|
14
14
|
|
15
|
-
def initialize(url, opts={})
|
15
|
+
def initialize(url, opts = {}, namespace = nil)
|
16
16
|
@url = url
|
17
17
|
URI.parse(@url) # for the sake of validating the url
|
18
18
|
@batch = []
|
19
19
|
@opts = opts
|
20
|
+
@namespace = namespace
|
20
21
|
@opts[:content_type] = 'application/json'
|
21
22
|
end
|
22
23
|
|
@@ -37,9 +38,10 @@ module Jimson
|
|
37
38
|
end
|
38
39
|
|
39
40
|
def send_single_request(method, args)
|
41
|
+
namespaced_method = @namespace.nil? ? method : "#@namespace#{method}"
|
40
42
|
post_data = MultiJson.encode({
|
41
43
|
'jsonrpc' => JSON_RPC_VERSION,
|
42
|
-
'method' =>
|
44
|
+
'method' => namespaced_method,
|
43
45
|
'params' => args,
|
44
46
|
'id' => self.class.make_id
|
45
47
|
})
|
@@ -154,8 +156,9 @@ module Jimson
|
|
154
156
|
helper.send_batch
|
155
157
|
end
|
156
158
|
|
157
|
-
def initialize(url, opts={})
|
158
|
-
@
|
159
|
+
def initialize(url, opts = {}, namespace = nil)
|
160
|
+
@url, @opts, @namespace = url, opts, namespace
|
161
|
+
@helper = ClientHelper.new(url, opts, namespace)
|
159
162
|
end
|
160
163
|
|
161
164
|
def method_missing(sym, *args, &block)
|
@@ -163,6 +166,11 @@ module Jimson
|
|
163
166
|
end
|
164
167
|
|
165
168
|
def [](method, *args)
|
169
|
+
if method.is_a?(Symbol)
|
170
|
+
# namespace requested
|
171
|
+
new_ns = @namespace.nil? ? "#{method}." : "#@namespace#{method}."
|
172
|
+
return Client.new(@url, @opts, new_ns)
|
173
|
+
end
|
166
174
|
@helper.process_call(method, args)
|
167
175
|
end
|
168
176
|
|
data/lib/jimson/router/map.rb
CHANGED
@@ -15,6 +15,7 @@ module Jimson
|
|
15
15
|
# Set the root handler, i.e. the handler used for a bare method like 'foo'
|
16
16
|
#
|
17
17
|
def root(handler)
|
18
|
+
handler = handler.new if handler.is_a?(Class)
|
18
19
|
@routes[''] = handler
|
19
20
|
end
|
20
21
|
|
@@ -23,6 +24,7 @@ module Jimson
|
|
23
24
|
#
|
24
25
|
def namespace(ns, handler = nil, &block)
|
25
26
|
if !!handler
|
27
|
+
handler = handler.new if handler.is_a?(Class)
|
26
28
|
@routes[ns.to_s] = handler
|
27
29
|
else
|
28
30
|
# passed a block for nested namespacing
|
data/lib/jimson/server.rb
CHANGED
@@ -29,6 +29,15 @@ module Jimson
|
|
29
29
|
|
30
30
|
attr_accessor :router, :host, :port, :opts
|
31
31
|
|
32
|
+
#
|
33
|
+
# Create a Server with routes defined
|
34
|
+
#
|
35
|
+
def self.with_routes(&block)
|
36
|
+
router = Router.new
|
37
|
+
router.send(:draw, &block)
|
38
|
+
self.new(router)
|
39
|
+
end
|
40
|
+
|
32
41
|
#
|
33
42
|
# +router_or_handler+ is an instance of Jimson::Router or extends Jimson::Handler
|
34
43
|
#
|
data/spec/client_spec.rb
CHANGED
@@ -27,6 +27,36 @@ module Jimson
|
|
27
27
|
@client = Client.new(SPEC_URL)
|
28
28
|
end
|
29
29
|
|
30
|
+
context "when using a symbol to specify a namespace" do
|
31
|
+
it "sends the method prefixed with the namespace in the request" do
|
32
|
+
expected = MultiJson.encode({
|
33
|
+
'jsonrpc' => '2.0',
|
34
|
+
'method' => 'foo.sum',
|
35
|
+
'params' => [1,2,3],
|
36
|
+
'id' => 1
|
37
|
+
})
|
38
|
+
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
39
|
+
RestClient.should_receive(:post).with(SPEC_URL, expected, {:content_type => 'application/json'}).and_return(@resp_mock)
|
40
|
+
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
41
|
+
@client[:foo].sum(1, 2, 3).should == 42
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when the namespace is nested" do
|
45
|
+
it "sends the method prefixed with the full namespace in the request" do
|
46
|
+
expected = MultiJson.encode({
|
47
|
+
'jsonrpc' => '2.0',
|
48
|
+
'method' => 'foo.bar.sum',
|
49
|
+
'params' => [1,2,3],
|
50
|
+
'id' => 1
|
51
|
+
})
|
52
|
+
response = MultiJson.encode(BOILERPLATE.merge({'result' => 42}))
|
53
|
+
RestClient.should_receive(:post).with(SPEC_URL, expected, {:content_type => 'application/json'}).and_return(@resp_mock)
|
54
|
+
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
55
|
+
@client[:foo][:bar].sum(1, 2, 3).should == 42
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
30
60
|
context "when sending positional arguments" do
|
31
61
|
it "sends a request with the correct method and args" do
|
32
62
|
expected = MultiJson.encode({
|
data/spec/router_spec.rb
CHANGED
@@ -21,33 +21,41 @@ module Jimson
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
class RouterBazHandler
|
25
|
+
extend Jimson::Handler
|
26
|
+
|
27
|
+
def meh
|
28
|
+
'mehkayla'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
24
32
|
|
25
33
|
describe '#draw' do
|
26
34
|
context 'when given non-nested namespaces' do
|
27
35
|
it 'takes a block with a DSL to set the root and namespaces' do
|
28
36
|
router.draw do
|
29
|
-
root
|
30
|
-
namespace 'ns',
|
37
|
+
root RouterFooHandler
|
38
|
+
namespace 'ns', RouterBarHandler
|
31
39
|
end
|
32
40
|
|
33
|
-
router.handler_for_method('hi').should
|
34
|
-
router.handler_for_method('ns.hi').should
|
41
|
+
router.handler_for_method('hi').should be_a(RouterFooHandler)
|
42
|
+
router.handler_for_method('ns.hi').should be_a(RouterBarHandler)
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
38
46
|
context 'when given nested namespaces' do
|
39
47
|
it 'takes a block with a DSL to set the root and namespaces' do
|
40
48
|
router.draw do
|
41
|
-
root
|
49
|
+
root RouterFooHandler
|
42
50
|
namespace 'ns1' do
|
43
|
-
root
|
44
|
-
namespace 'ns2',
|
51
|
+
root RouterBazHandler
|
52
|
+
namespace 'ns2', RouterBarHandler
|
45
53
|
end
|
46
54
|
end
|
47
55
|
|
48
|
-
router.handler_for_method('hi').should
|
49
|
-
router.handler_for_method('ns1.hi').should
|
50
|
-
router.handler_for_method('ns1.ns2.hi').should
|
56
|
+
router.handler_for_method('hi').should be_a(RouterFooHandler)
|
57
|
+
router.handler_for_method('ns1.hi').should be_a(RouterBazHandler)
|
58
|
+
router.handler_for_method('ns1.ns2.hi').should be_a(RouterBarHandler)
|
51
59
|
end
|
52
60
|
end
|
53
61
|
end
|
@@ -55,8 +63,8 @@ module Jimson
|
|
55
63
|
describe '#jimson_methods' do
|
56
64
|
it 'returns an array of namespaced method names from all registered handlers' do
|
57
65
|
router.draw do
|
58
|
-
root RouterFooHandler
|
59
|
-
namespace 'foo', RouterBarHandler
|
66
|
+
root RouterFooHandler
|
67
|
+
namespace 'foo', RouterBarHandler
|
60
68
|
end
|
61
69
|
|
62
70
|
router.jimson_methods.sort.should == ['hi', 'foo.bye'].sort
|
data/spec/server_spec.rb
CHANGED
@@ -395,5 +395,33 @@ module Jimson
|
|
395
395
|
end
|
396
396
|
end
|
397
397
|
end
|
398
|
+
|
399
|
+
describe ".with_routes" do
|
400
|
+
it "creates a server with a router by passing the block to Router#draw" do
|
401
|
+
app = Server.with_routes do
|
402
|
+
root TestHandler.new
|
403
|
+
namespace 'foo', OtherHandler.new
|
404
|
+
end
|
405
|
+
|
406
|
+
# have to make a new Rack::Test browser since this server is different than the normal one
|
407
|
+
browser = Rack::Test::Session.new(Rack::MockSession.new(app))
|
408
|
+
|
409
|
+
req = {
|
410
|
+
'jsonrpc' => '2.0',
|
411
|
+
'method' => 'foo.multiply',
|
412
|
+
'params' => [2, 3],
|
413
|
+
'id' => 1
|
414
|
+
}
|
415
|
+
browser.post '/', MultiJson.encode(req), {'Content-Type' => 'application/json'}
|
416
|
+
|
417
|
+
browser.last_response.should be_ok
|
418
|
+
resp = MultiJson.decode(browser.last_response.body)
|
419
|
+
resp.should == {
|
420
|
+
'jsonrpc' => '2.0',
|
421
|
+
'result' => 6,
|
422
|
+
'id' => 1
|
423
|
+
}
|
424
|
+
end
|
425
|
+
end
|
398
426
|
end
|
399
427
|
end
|
metadata
CHANGED
@@ -1,120 +1,134 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: jimson
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.7.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
|
7
|
+
authors:
|
8
|
+
- Chris Kite
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
12
|
+
date: 2012-08-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: blankslate
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.1.2.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.1.2.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rest-client
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.6.3
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.6.3
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: multi_json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.1.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rack
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.3'
|
59
78
|
description:
|
60
79
|
email:
|
61
80
|
executables: []
|
62
|
-
|
63
81
|
extensions: []
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
- spec/server_spec.rb
|
82
|
+
extra_rdoc_files:
|
83
|
+
- README.md
|
84
|
+
files:
|
85
|
+
- VERSION
|
86
|
+
- LICENSE.txt
|
87
|
+
- CHANGELOG.rdoc
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/jimson/request.rb
|
91
|
+
- lib/jimson/router.rb
|
92
|
+
- lib/jimson/response.rb
|
93
|
+
- lib/jimson/router/map.rb
|
94
|
+
- lib/jimson/server.rb
|
95
|
+
- lib/jimson/client/error.rb
|
96
|
+
- lib/jimson/client.rb
|
97
|
+
- lib/jimson/handler.rb
|
98
|
+
- lib/jimson/server/error.rb
|
99
|
+
- lib/jimson.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/router_spec.rb
|
102
|
+
- spec/client_spec.rb
|
103
|
+
- spec/handler_spec.rb
|
104
|
+
- spec/server_spec.rb
|
88
105
|
homepage: http://www.github.com/chriskite/jimson
|
89
106
|
licenses: []
|
90
|
-
|
91
107
|
post_install_message:
|
92
108
|
rdoc_options: []
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
112
|
none: false
|
98
|
-
requirements:
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
118
|
none: false
|
104
|
-
requirements:
|
105
|
-
|
106
|
-
|
107
|
-
|
119
|
+
requirements:
|
120
|
+
- - ! '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
108
123
|
requirements: []
|
109
|
-
|
110
124
|
rubyforge_project:
|
111
|
-
rubygems_version: 1.8.
|
125
|
+
rubygems_version: 1.8.21
|
112
126
|
signing_key:
|
113
127
|
specification_version: 3
|
114
128
|
summary: JSON-RPC 2.0 client and server
|
115
|
-
test_files:
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
129
|
+
test_files:
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
- spec/router_spec.rb
|
132
|
+
- spec/client_spec.rb
|
133
|
+
- spec/handler_spec.rb
|
134
|
+
- spec/server_spec.rb
|