jimson 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +5 -0
- data/VERSION +1 -1
- data/lib/jimson.rb +0 -5
- data/lib/jimson/client.rb +12 -9
- data/lib/jimson/server.rb +6 -1
- data/lib/jimson/server_error.rb +6 -0
- data/spec/client_spec.rb +2 -5
- data/spec/server_spec.rb +12 -13
- data/spec/spec_helper.rb +2 -8
- metadata +4 -4
data/CHANGELOG.rdoc
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/jimson.rb
CHANGED
data/lib/jimson/client.rb
CHANGED
@@ -1,17 +1,20 @@
|
|
1
|
-
require '
|
1
|
+
require 'rest-client'
|
2
|
+
require 'jimson/server_error'
|
3
|
+
require 'jimson/client_error'
|
4
|
+
require 'jimson/request'
|
5
|
+
require 'jimson/response'
|
2
6
|
|
3
7
|
module Jimson
|
4
8
|
class ClientHelper
|
9
|
+
JSON_RPC_VERSION = '2.0'
|
5
10
|
|
6
11
|
def self.make_id
|
7
12
|
rand(10**12)
|
8
13
|
end
|
9
14
|
|
10
15
|
def initialize(url)
|
11
|
-
@
|
12
|
-
|
13
|
-
@path = uri.path
|
14
|
-
@http.base_url = "#{uri.scheme}://#{uri.host}:#{uri.port}"
|
16
|
+
@url = url
|
17
|
+
URI.parse(@url) # for the sake of validating the url
|
15
18
|
@batch = []
|
16
19
|
end
|
17
20
|
|
@@ -29,12 +32,12 @@ module Jimson
|
|
29
32
|
|
30
33
|
def send_single_request(method, args)
|
31
34
|
post_data = {
|
32
|
-
'jsonrpc' =>
|
35
|
+
'jsonrpc' => JSON_RPC_VERSION,
|
33
36
|
'method' => method,
|
34
37
|
'params' => args,
|
35
38
|
'id' => self.class.make_id
|
36
39
|
}.to_json
|
37
|
-
resp =
|
40
|
+
resp = RestClient.post(@url, post_data, :content_type => 'application/json')
|
38
41
|
if resp.nil? || resp.body.nil? || resp.body.empty?
|
39
42
|
raise Jimson::ClientError::InvalidResponse.new
|
40
43
|
end
|
@@ -47,7 +50,7 @@ module Jimson
|
|
47
50
|
|
48
51
|
def send_batch_request(batch)
|
49
52
|
post_data = batch.to_json
|
50
|
-
resp =
|
53
|
+
resp = RestClient.post(@url, post_data, :content_type => 'application/json')
|
51
54
|
if resp.nil? || resp.body.nil? || resp.body.empty?
|
52
55
|
raise Jimson::ClientError::InvalidResponse.new
|
53
56
|
end
|
@@ -84,7 +87,7 @@ module Jimson
|
|
84
87
|
def valid_response?(data)
|
85
88
|
return false if !data.is_a?(Hash)
|
86
89
|
|
87
|
-
return false if data['jsonrpc'] !=
|
90
|
+
return false if data['jsonrpc'] != JSON_RPC_VERSION
|
88
91
|
|
89
92
|
return false if !data.has_key?('id')
|
90
93
|
|
data/lib/jimson/server.rb
CHANGED
@@ -2,11 +2,14 @@ require 'eventmachine'
|
|
2
2
|
require 'evma_httpserver'
|
3
3
|
require 'logger'
|
4
4
|
require 'json'
|
5
|
+
require 'jimson/server_error'
|
5
6
|
|
6
7
|
module Jimson
|
7
8
|
class HttpServer < EM::Connection
|
8
9
|
include EM::HttpServer
|
9
10
|
|
11
|
+
JSON_RPC_VERSION = '2.0'
|
12
|
+
|
10
13
|
def self.handler=(handler)
|
11
14
|
@@handler = handler
|
12
15
|
end
|
@@ -76,7 +79,7 @@ module Jimson
|
|
76
79
|
return false if request.has_key?(key) && !types.any? { |type| request[key].is_a?(type) }
|
77
80
|
end
|
78
81
|
|
79
|
-
return false if request['jsonrpc'] !=
|
82
|
+
return false if request['jsonrpc'] != JSON_RPC_VERSION
|
80
83
|
|
81
84
|
true
|
82
85
|
end
|
@@ -93,6 +96,8 @@ module Jimson
|
|
93
96
|
raise Jimson::ServerError::MethodNotFound.new
|
94
97
|
rescue ArgumentError
|
95
98
|
raise Jimson::ServerError::InvalidParams.new
|
99
|
+
rescue
|
100
|
+
raise Jimson::ServerError::ApplicationError.new($!)
|
96
101
|
end
|
97
102
|
|
98
103
|
response = success_response(request, result)
|
data/lib/jimson/server_error.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -3,9 +3,6 @@ module Jimson
|
|
3
3
|
BOILERPLATE = {'jsonrpc' => '2.0', 'id' => 1}
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
@http_mock = mock('http')
|
7
|
-
Patron::Session.stub!(:new).and_return(@http_mock)
|
8
|
-
@http_mock.should_receive(:base_url=).with(SPEC_URL)
|
9
6
|
@resp_mock = mock('http_response')
|
10
7
|
ClientHelper.stub!(:make_id).and_return(1)
|
11
8
|
end
|
@@ -25,7 +22,7 @@ module Jimson
|
|
25
22
|
end
|
26
23
|
it "sends a valid JSON-RPC request and returns the result" do
|
27
24
|
response = BOILERPLATE.merge({'result' => 42}).to_json
|
28
|
-
|
25
|
+
RestClient.should_receive(:post).with(SPEC_URL, @expected, {:content_type => 'application/json'}).and_return(@resp_mock)
|
29
26
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
30
27
|
client = Client.new(SPEC_URL)
|
31
28
|
client.foo(1,2,3).should == 42
|
@@ -50,7 +47,7 @@ module Jimson
|
|
50
47
|
].to_json
|
51
48
|
|
52
49
|
ClientHelper.stub!(:make_id).and_return('1', '2', '5', '9')
|
53
|
-
|
50
|
+
RestClient.should_receive(:post).with(SPEC_URL, batch, {:content_type => 'application/json'}).and_return(@resp_mock)
|
54
51
|
@resp_mock.should_receive(:body).at_least(:once).and_return(response)
|
55
52
|
client = Client.new(SPEC_URL)
|
56
53
|
|
data/spec/server_spec.rb
CHANGED
@@ -12,8 +12,7 @@ module Jimson
|
|
12
12
|
'id' => nil
|
13
13
|
}
|
14
14
|
before(:each) do
|
15
|
-
@
|
16
|
-
@sess.base_url = SPEC_URL
|
15
|
+
@url = SPEC_URL
|
17
16
|
end
|
18
17
|
|
19
18
|
describe "receiving a request with positional parameters" do
|
@@ -25,7 +24,7 @@ module Jimson
|
|
25
24
|
'params' => [24, 20],
|
26
25
|
'id' => 1
|
27
26
|
}
|
28
|
-
resp = JSON.parse(
|
27
|
+
resp = JSON.parse(RestClient.post(@url, req.to_json).body)
|
29
28
|
resp.should == {
|
30
29
|
'jsonrpc' => '2.0',
|
31
30
|
'result' => 4,
|
@@ -44,7 +43,7 @@ module Jimson
|
|
44
43
|
'params' => {'subtrahend'=> 20, 'minuend' => 24},
|
45
44
|
'id' => 1
|
46
45
|
}
|
47
|
-
resp = JSON.parse(
|
46
|
+
resp = JSON.parse(RestClient.post(@url, req.to_json).body)
|
48
47
|
resp.should == {
|
49
48
|
'jsonrpc' => '2.0',
|
50
49
|
'result' => 4,
|
@@ -62,7 +61,7 @@ module Jimson
|
|
62
61
|
'method' => 'update',
|
63
62
|
'params' => [1,2,3,4,5]
|
64
63
|
}
|
65
|
-
resp =
|
64
|
+
resp = RestClient.post(@url, req.to_json).body
|
66
65
|
resp.should be_empty
|
67
66
|
end
|
68
67
|
end
|
@@ -75,7 +74,7 @@ module Jimson
|
|
75
74
|
'method' => 'foobar',
|
76
75
|
'id' => 1
|
77
76
|
}
|
78
|
-
resp = JSON.parse(
|
77
|
+
resp = JSON.parse(RestClient.post(@url, req.to_json).body)
|
79
78
|
resp.should == {
|
80
79
|
'jsonrpc' => '2.0',
|
81
80
|
'error' => {
|
@@ -95,7 +94,7 @@ module Jimson
|
|
95
94
|
'params' => [1,2,3],
|
96
95
|
'id' => 1
|
97
96
|
}
|
98
|
-
resp = JSON.parse(
|
97
|
+
resp = JSON.parse(RestClient.post(@url, req.to_json).body)
|
99
98
|
resp.should == {
|
100
99
|
'jsonrpc' => '2.0',
|
101
100
|
'error' => {
|
@@ -115,7 +114,7 @@ module Jimson
|
|
115
114
|
'id' => 1
|
116
115
|
}.to_json
|
117
116
|
req += '}' # make the json invalid
|
118
|
-
resp = JSON.parse(
|
117
|
+
resp = JSON.parse(RestClient.post(@url, req).body)
|
119
118
|
resp.should == {
|
120
119
|
'jsonrpc' => '2.0',
|
121
120
|
'error' => {
|
@@ -134,7 +133,7 @@ module Jimson
|
|
134
133
|
'jsonrpc' => '2.0',
|
135
134
|
'method' => 1 # method as int is invalid
|
136
135
|
}.to_json
|
137
|
-
resp = JSON.parse(
|
136
|
+
resp = JSON.parse(RestClient.post(@url, req).body)
|
138
137
|
resp.should == INVALID_RESPONSE_EXPECTATION
|
139
138
|
end
|
140
139
|
end
|
@@ -142,7 +141,7 @@ module Jimson
|
|
142
141
|
context "when the request is an empty batch" do
|
143
142
|
it "returns an error response" do
|
144
143
|
req = [].to_json
|
145
|
-
resp = JSON.parse(
|
144
|
+
resp = JSON.parse(RestClient.post(@url, req).body)
|
146
145
|
resp.should == INVALID_RESPONSE_EXPECTATION
|
147
146
|
end
|
148
147
|
end
|
@@ -150,7 +149,7 @@ module Jimson
|
|
150
149
|
context "when the request is an invalid batch" do
|
151
150
|
it "returns an error response" do
|
152
151
|
req = [1,2].to_json
|
153
|
-
resp = JSON.parse(
|
152
|
+
resp = JSON.parse(RestClient.post(@url, req).body)
|
154
153
|
resp.should == [INVALID_RESPONSE_EXPECTATION, INVALID_RESPONSE_EXPECTATION]
|
155
154
|
end
|
156
155
|
end
|
@@ -167,7 +166,7 @@ module Jimson
|
|
167
166
|
{'jsonrpc' => '2.0', 'method' => 'foo.get', 'params' => {'name' => 'myself'}, 'id' => '5'},
|
168
167
|
{'jsonrpc' => '2.0', 'method' => 'get_data', 'id' => '9'}
|
169
168
|
].to_json
|
170
|
-
resp = JSON.parse(
|
169
|
+
resp = JSON.parse(RestClient.post(@url, reqs).body)
|
171
170
|
resp.should == [
|
172
171
|
{'jsonrpc' => '2.0', 'result' => 7, 'id' => '1'},
|
173
172
|
{'jsonrpc' => '2.0', 'result' => 19, 'id' => '2'},
|
@@ -192,7 +191,7 @@ module Jimson
|
|
192
191
|
'params' => [1,2,3,4,5]
|
193
192
|
}
|
194
193
|
]
|
195
|
-
resp =
|
194
|
+
resp = RestClient.post(@url, req.to_json).body
|
196
195
|
resp.should be_empty
|
197
196
|
end
|
198
197
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,18 +1,12 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
$:.unshift(File.dirname(__FILE__) + '/../lib/')
|
3
|
-
require 'jimson'
|
3
|
+
require 'jimson/server'
|
4
|
+
require 'jimson/client'
|
4
5
|
require 'open-uri'
|
5
6
|
require 'json'
|
6
|
-
require 'patron'
|
7
|
-
require 'fakeweb'
|
8
7
|
|
9
8
|
SPEC_URL = 'http://localhost:8999'
|
10
9
|
|
11
|
-
|
12
|
-
def fake_response(json)
|
13
|
-
FakeWeb.register_uri(:post, SPEC_URL, :body => json)
|
14
|
-
end
|
15
|
-
|
16
10
|
pid = Process.spawn(File.dirname(__FILE__) + '/em_helper.rb')
|
17
11
|
|
18
12
|
RSpec.configure do |config|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jimson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chris Kite
|
@@ -10,18 +10,18 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-20 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
17
|
+
name: rest-client
|
18
18
|
prerelease: false
|
19
19
|
requirement: &id001 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ">="
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version:
|
24
|
+
version: 1.6.3
|
25
25
|
type: :runtime
|
26
26
|
version_requirements: *id001
|
27
27
|
- !ruby/object:Gem::Dependency
|