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 CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.8.0 / 2012-08-17
2
+
3
+ * Major enhancements
4
+
5
+ * Add namespaced method calls to client (e.g. 'client[:foo].sum(1,2,3) # calls foo.sum')
6
+ * Add Server.with_routes to quickly created a routed server
7
+
1
8
  == 0.7.1 / 2012-08-16
2
9
 
3
10
  * Bug fixes
data/Rakefile CHANGED
@@ -14,11 +14,7 @@ end
14
14
 
15
15
  task :default => :rspec
16
16
 
17
- begin
18
- require 'rdoc/task'
19
- rescue LoadError
20
- require 'rake/rdoctask'
21
- end
17
+ require 'rdoc/task'
22
18
 
23
19
  Rake::RDocTask.new do |rdoc|
24
20
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
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' => 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
- @helper = ClientHelper.new(url, opts)
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
 
@@ -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 'foo'
30
- namespace 'ns', 'bar'
37
+ root RouterFooHandler
38
+ namespace 'ns', RouterBarHandler
31
39
  end
32
40
 
33
- router.handler_for_method('hi').should == 'foo'
34
- router.handler_for_method('ns.hi').should == 'bar'
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 'foo'
49
+ root RouterFooHandler
42
50
  namespace 'ns1' do
43
- root 'blah'
44
- namespace 'ns2', 'bar'
51
+ root RouterBazHandler
52
+ namespace 'ns2', RouterBarHandler
45
53
  end
46
54
  end
47
55
 
48
- router.handler_for_method('hi').should == 'foo'
49
- router.handler_for_method('ns1.hi').should == 'blah'
50
- router.handler_for_method('ns1.ns2.hi').should == 'bar'
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.new
59
- namespace 'foo', RouterBarHandler.new
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
- - Chris Kite
7
+ authors:
8
+ - Chris Kite
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-08-16 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: blankslate
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 2.1.2.3
24
- type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: rest-client
28
- prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
30
- none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: 1.6.3
35
- type: :runtime
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: multi_json
39
- prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 1.1.0
46
- type: :runtime
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
49
- name: rack
50
- prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "1.3"
57
- type: :runtime
58
- version_requirements: *id004
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
- extra_rdoc_files:
66
- - README.md
67
- files:
68
- - VERSION
69
- - LICENSE.txt
70
- - CHANGELOG.rdoc
71
- - README.md
72
- - Rakefile
73
- - lib/jimson.rb
74
- - lib/jimson/request.rb
75
- - lib/jimson/router.rb
76
- - lib/jimson/response.rb
77
- - lib/jimson/server.rb
78
- - lib/jimson/client.rb
79
- - lib/jimson/handler.rb
80
- - lib/jimson/router/map.rb
81
- - lib/jimson/client/error.rb
82
- - lib/jimson/server/error.rb
83
- - spec/spec_helper.rb
84
- - spec/router_spec.rb
85
- - spec/client_spec.rb
86
- - spec/handler_spec.rb
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
- require_paths:
95
- - lib
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
- - !ruby/object:Gem::Version
101
- version: "0"
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
- - !ruby/object:Gem::Version
107
- version: "0"
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
108
123
  requirements: []
109
-
110
124
  rubyforge_project:
111
- rubygems_version: 1.8.15
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
- - spec/spec_helper.rb
117
- - spec/router_spec.rb
118
- - spec/client_spec.rb
119
- - spec/handler_spec.rb
120
- - spec/server_spec.rb
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