json-rpc 0.1.4 → 0.1.5
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/README.md +1 -1
- data/lib/json-rpc.rb +4 -4
- data/test/functional/app-mixed.ru +118 -0
- data/test/functional/rpc-test.rb +51 -0
- data/test/functional/test-mixed.rb +63 -0
- data/test/{test_rpc_module.rb → unit/test_rpc_module.rb} +0 -0
- metadata +7 -4
data/README.md
CHANGED
data/lib/json-rpc.rb
CHANGED
@@ -23,6 +23,7 @@ module JsonRpc
|
|
23
23
|
header = {'Content-Type' => Rpc::ContentType}
|
24
24
|
|
25
25
|
if result.is_a?(Rpc::AsyncResult)
|
26
|
+
result.env = env
|
26
27
|
result.status = status
|
27
28
|
result.header = header
|
28
29
|
return [-1, {}, result]
|
@@ -53,7 +54,7 @@ module JsonRpc
|
|
53
54
|
def result
|
54
55
|
res = {
|
55
56
|
"id" => id,
|
56
|
-
"jsonrpc" =>
|
57
|
+
"jsonrpc" => Version,
|
57
58
|
"error" => {
|
58
59
|
"code" => code,
|
59
60
|
"message" => msg
|
@@ -117,8 +118,7 @@ module JsonRpc
|
|
117
118
|
end
|
118
119
|
|
119
120
|
def self.forge_response result, id = nil
|
120
|
-
|
121
|
-
{"jsonrpc" => "2.0", "id" => id, "result" => result}.to_json
|
121
|
+
{"jsonrpc" => Version, "id" => id.to_i, "result" => result}.to_json
|
122
122
|
end
|
123
123
|
|
124
124
|
# The class RpcDeferrable is useful helps you to build a Json Rpc server
|
@@ -127,7 +127,7 @@ module JsonRpc
|
|
127
127
|
include EventMachine::Deferrable
|
128
128
|
|
129
129
|
attr_reader :response
|
130
|
-
attr_accessor :id, :status, :header
|
130
|
+
attr_accessor :id, :status, :header, :env
|
131
131
|
|
132
132
|
def initialize env = nil
|
133
133
|
@env = env
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'pp'
|
2
|
+
$: << File::join(File::dirname(__FILE__), "../../")
|
3
|
+
require 'lib/json-rpc'
|
4
|
+
|
5
|
+
class SimpleApp
|
6
|
+
include JsonRpc
|
7
|
+
alias :call :rpc_call
|
8
|
+
|
9
|
+
def rpc_sum a, b
|
10
|
+
a + b
|
11
|
+
end
|
12
|
+
|
13
|
+
def rpc_true
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def rpc_false
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
def rpc_array
|
22
|
+
[1,2,3]
|
23
|
+
end
|
24
|
+
|
25
|
+
def rpc_empty_array
|
26
|
+
[]
|
27
|
+
end
|
28
|
+
|
29
|
+
def rpc_hash
|
30
|
+
{
|
31
|
+
"a" => "Apple",
|
32
|
+
"b" => "Banana"
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def rpc_repeat *params
|
37
|
+
params
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
class AsyncApp
|
43
|
+
include JsonRpc
|
44
|
+
alias :call :rpc_call
|
45
|
+
|
46
|
+
def rpc_sum a, b
|
47
|
+
result = Rpc::AsyncResult.new
|
48
|
+
EventMachine::next_tick do
|
49
|
+
result.reply a + b
|
50
|
+
result.succeed
|
51
|
+
end
|
52
|
+
result
|
53
|
+
end
|
54
|
+
|
55
|
+
def rpc_true
|
56
|
+
result = Rpc::AsyncResult.new
|
57
|
+
EventMachine::next_tick do
|
58
|
+
result.reply true
|
59
|
+
result.succeed
|
60
|
+
end
|
61
|
+
result
|
62
|
+
end
|
63
|
+
|
64
|
+
def rpc_false
|
65
|
+
result = Rpc::AsyncResult.new
|
66
|
+
EventMachine::next_tick do
|
67
|
+
result.reply false
|
68
|
+
result.succeed
|
69
|
+
end
|
70
|
+
result
|
71
|
+
end
|
72
|
+
|
73
|
+
def rpc_array
|
74
|
+
result = Rpc::AsyncResult.new
|
75
|
+
EventMachine::next_tick do
|
76
|
+
result.reply [1,2,3]
|
77
|
+
result.succeed
|
78
|
+
end
|
79
|
+
result
|
80
|
+
end
|
81
|
+
|
82
|
+
def rpc_empty_array
|
83
|
+
result = Rpc::AsyncResult.new
|
84
|
+
EventMachine::next_tick do
|
85
|
+
result.reply []
|
86
|
+
result.succeed
|
87
|
+
end
|
88
|
+
result
|
89
|
+
end
|
90
|
+
|
91
|
+
def rpc_hash
|
92
|
+
result = Rpc::AsyncResult.new
|
93
|
+
EventMachine::next_tick do
|
94
|
+
result.reply("a" => "Apple", "b" => "Banana")
|
95
|
+
result.succeed
|
96
|
+
end
|
97
|
+
result
|
98
|
+
end
|
99
|
+
|
100
|
+
def rpc_repeat *params
|
101
|
+
result = Rpc::AsyncResult.new
|
102
|
+
EventMachine::next_tick do
|
103
|
+
result.reply params
|
104
|
+
result.succeed
|
105
|
+
end
|
106
|
+
result
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
map '/rpc-sync' do
|
112
|
+
run SimpleApp.new
|
113
|
+
end
|
114
|
+
|
115
|
+
map '/rpc-async' do
|
116
|
+
run AsyncApp.new
|
117
|
+
end
|
118
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'cgi'
|
5
|
+
require 'json'
|
6
|
+
require 'pp'
|
7
|
+
|
8
|
+
|
9
|
+
class RpcTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def initialize *args
|
12
|
+
@rpc_path = self.is_a?(SyncTest) ? '/rpc-sync' : '/rpc-async'
|
13
|
+
@rpc_url = URI.parse('http://localhost:4242')
|
14
|
+
super *args
|
15
|
+
end
|
16
|
+
|
17
|
+
def call method_rpc, *params, &blk
|
18
|
+
if blk
|
19
|
+
[:get, :post].each do |method_http|
|
20
|
+
blk.call rpc_call(method_http, method_rpc, *params)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
rpc_call :get, method_rpc, *params
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def rpc_call method_http, method_rpc, *params
|
29
|
+
rpc_request = {
|
30
|
+
:jsonrpc => "2.0",
|
31
|
+
:method => method_rpc,
|
32
|
+
:params => params,
|
33
|
+
}
|
34
|
+
|
35
|
+
res = Net::HTTP.start(@rpc_url.host, @rpc_url.port) do |http|
|
36
|
+
case method_http
|
37
|
+
when :get
|
38
|
+
rpc_request[:params] = CGI::escape(rpc_request[:params].to_json)
|
39
|
+
http.get @rpc_path + "?" + rpc_request.map{|a,b| "#{a}=#{b}"}.join("&")
|
40
|
+
when :post
|
41
|
+
req = Net::HTTP::Post.new(@rpc_path)
|
42
|
+
req.body = rpc_request.to_json
|
43
|
+
http.request(req)
|
44
|
+
else
|
45
|
+
raise "Unknown HTTP method #{method_http}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
JSON::parse(res.body)
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rpc-test'
|
2
|
+
|
3
|
+
module MixedTest
|
4
|
+
def test_true
|
5
|
+
call("true") { |obj|
|
6
|
+
assert_equal true, obj["result"]
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_false
|
11
|
+
call("false") { |obj|
|
12
|
+
assert_equal false, obj["result"]
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_empty_array
|
17
|
+
call("empty_array") { |obj|
|
18
|
+
assert_equal [], obj["result"]
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_array
|
23
|
+
call("array") { |obj|
|
24
|
+
assert_equal [1,2,3], obj["result"]
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_hash
|
29
|
+
call("hash") { |obj|
|
30
|
+
exp_result = {
|
31
|
+
"a" => "Apple",
|
32
|
+
"b" => "Banana"
|
33
|
+
}
|
34
|
+
assert_equal exp_result, obj["result"]
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_repeat_one
|
39
|
+
call("repeat", 42) { |obj|
|
40
|
+
assert_equal [42], obj["result"]
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_repeat_two
|
45
|
+
call("repeat", 21, 21) { |obj|
|
46
|
+
assert_equal [21, 21], obj["result"]
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_repeat_hash
|
51
|
+
call("repeat", "a" => "Apple", "b" => "Banana") { |obj|
|
52
|
+
assert_equal(["a" => "Apple", "b" => "Banana"], obj["result"])
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class SyncTest < RpcTest
|
58
|
+
include MixedTest
|
59
|
+
end
|
60
|
+
|
61
|
+
class AsyncTest < RpcTest
|
62
|
+
include MixedTest
|
63
|
+
end
|
File without changes
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Helios Technologies Ltd.
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-06-
|
17
|
+
date: 2011-06-28 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -28,7 +28,10 @@ extra_rdoc_files: []
|
|
28
28
|
|
29
29
|
files:
|
30
30
|
- lib/json-rpc.rb
|
31
|
-
- test/test_rpc_module.rb
|
31
|
+
- test/unit/test_rpc_module.rb
|
32
|
+
- test/functional/test-mixed.rb
|
33
|
+
- test/functional/app-mixed.ru
|
34
|
+
- test/functional/rpc-test.rb
|
32
35
|
- example/async_app.ru
|
33
36
|
- example/rack_app.ru
|
34
37
|
- README.md
|