json-rpc 0.1.5 → 0.1.6
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/lib/json-rpc.rb +5 -3
- data/test/functional/#app-mixed.ru# +75 -0
- data/test/functional/app-mixed.ru +26 -33
- data/test/functional/rpc-test.rb +33 -18
- metadata +6 -5
data/lib/json-rpc.rb
CHANGED
@@ -13,11 +13,13 @@ module JsonRpc
|
|
13
13
|
request = Rpc::parse env
|
14
14
|
status = Rpc::validate request
|
15
15
|
result = Rpc::route request, self
|
16
|
-
|
17
|
-
|
16
|
+
rescue Exception, Rpc::Error => e
|
17
|
+
ecb.call(e) if ecb
|
18
|
+
unless e.is_a?(Rpc::Error)
|
19
|
+
e = Rpc::error :internal_error
|
20
|
+
end
|
18
21
|
status = e.status
|
19
22
|
result = e.result
|
20
|
-
ecb.call(e) if ecb
|
21
23
|
end
|
22
24
|
|
23
25
|
header = {'Content-Type' => Rpc::ContentType}
|
@@ -0,0 +1,75 @@
|
|
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 initialize
|
47
|
+
@simple = SimpleApp.new
|
48
|
+
@simple.methods.find{ |name| name.to_s =~ /^rpc_/}.each do |name|
|
49
|
+
self.
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def method_missing method, *args
|
54
|
+
result = Rpc::AsyncResult.new
|
55
|
+
EventMachine::next_tick do
|
56
|
+
e = catch(:Error) {
|
57
|
+
result.reply SimpleApp.new.send(method, *args)
|
58
|
+
result.succeed
|
59
|
+
nil
|
60
|
+
}
|
61
|
+
result.failed e[:code], e[:msg] if e
|
62
|
+
end
|
63
|
+
result
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
map '/rpc-sync' do
|
69
|
+
run SimpleApp.new
|
70
|
+
end
|
71
|
+
|
72
|
+
map '/rpc-async' do
|
73
|
+
run AsyncApp.new
|
74
|
+
end
|
75
|
+
|
@@ -44,68 +44,61 @@ class AsyncApp
|
|
44
44
|
alias :call :rpc_call
|
45
45
|
|
46
46
|
def rpc_sum a, b
|
47
|
-
|
48
|
-
|
49
|
-
result.reply a + b
|
50
|
-
result.succeed
|
47
|
+
wrap_async do
|
48
|
+
a + b
|
51
49
|
end
|
52
|
-
result
|
53
50
|
end
|
54
51
|
|
55
52
|
def rpc_true
|
56
|
-
|
57
|
-
|
58
|
-
result.reply true
|
59
|
-
result.succeed
|
53
|
+
wrap_async do
|
54
|
+
true
|
60
55
|
end
|
61
|
-
result
|
62
56
|
end
|
63
57
|
|
64
58
|
def rpc_false
|
65
|
-
|
66
|
-
|
67
|
-
result.reply false
|
68
|
-
result.succeed
|
59
|
+
wrap_async do
|
60
|
+
false
|
69
61
|
end
|
70
|
-
result
|
71
62
|
end
|
72
63
|
|
73
64
|
def rpc_array
|
74
|
-
|
75
|
-
|
76
|
-
result.reply [1,2,3]
|
77
|
-
result.succeed
|
65
|
+
wrap_async do
|
66
|
+
[1,2,3]
|
78
67
|
end
|
79
|
-
result
|
80
68
|
end
|
81
69
|
|
82
70
|
def rpc_empty_array
|
83
|
-
|
84
|
-
|
85
|
-
result.reply []
|
86
|
-
result.succeed
|
71
|
+
wrap_async do
|
72
|
+
[]
|
87
73
|
end
|
88
|
-
result
|
89
74
|
end
|
90
75
|
|
91
76
|
def rpc_hash
|
92
|
-
|
93
|
-
|
94
|
-
result.reply("a" => "Apple", "b" => "Banana")
|
95
|
-
result.succeed
|
77
|
+
wrap_async do
|
78
|
+
{"a" => "Apple", "b" => "Banana"}
|
96
79
|
end
|
97
|
-
result
|
98
80
|
end
|
99
81
|
|
100
82
|
def rpc_repeat *params
|
83
|
+
wrap_async do
|
84
|
+
params
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def wrap_async &block
|
101
91
|
result = Rpc::AsyncResult.new
|
102
92
|
EventMachine::next_tick do
|
103
|
-
|
104
|
-
|
93
|
+
e = catch(:Error) {
|
94
|
+
result.reply block.call result
|
95
|
+
result.succeed
|
96
|
+
nil
|
97
|
+
}
|
98
|
+
result.failed e[:code], e[:msg] if e
|
105
99
|
end
|
106
100
|
result
|
107
101
|
end
|
108
|
-
|
109
102
|
end
|
110
103
|
|
111
104
|
map '/rpc-sync' do
|
data/test/functional/rpc-test.rb
CHANGED
@@ -1,22 +1,29 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require '
|
3
|
-
require 'uri'
|
4
|
-
require 'cgi'
|
2
|
+
require 'mechanize'
|
5
3
|
require 'json'
|
6
4
|
require 'pp'
|
7
|
-
|
5
|
+
require 'cgi'
|
8
6
|
|
9
7
|
class RpcTest < Test::Unit::TestCase
|
10
8
|
|
9
|
+
attr_reader :session
|
10
|
+
|
11
11
|
def initialize *args
|
12
12
|
@rpc_path = self.is_a?(SyncTest) ? '/rpc-sync' : '/rpc-async'
|
13
|
-
@rpc_url =
|
13
|
+
@rpc_url = 'http://localhost:4242'
|
14
|
+
@get_off = @post_off = false
|
15
|
+
new_session
|
14
16
|
super *args
|
15
17
|
end
|
16
18
|
|
19
|
+
attr_accessor :post_off, :get_off
|
20
|
+
|
17
21
|
def call method_rpc, *params, &blk
|
22
|
+
types = []
|
23
|
+
types.push :get unless @get_off
|
24
|
+
types.push :post unless @post_off
|
18
25
|
if blk
|
19
|
-
|
26
|
+
types.each do |method_http|
|
20
27
|
blk.call rpc_call(method_http, method_rpc, *params)
|
21
28
|
end
|
22
29
|
else
|
@@ -24,6 +31,13 @@ class RpcTest < Test::Unit::TestCase
|
|
24
31
|
end
|
25
32
|
end
|
26
33
|
|
34
|
+
def new_session session = nil
|
35
|
+
session = Mechanize.new unless session
|
36
|
+
priv_session = @session
|
37
|
+
@session = session
|
38
|
+
priv_session
|
39
|
+
end
|
40
|
+
|
27
41
|
private
|
28
42
|
def rpc_call method_http, method_rpc, *params
|
29
43
|
rpc_request = {
|
@@ -32,18 +46,19 @@ class RpcTest < Test::Unit::TestCase
|
|
32
46
|
:params => params,
|
33
47
|
}
|
34
48
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
49
|
+
case method_http
|
50
|
+
when :get
|
51
|
+
#rpc_request[:params] = CGI::escape(rpc_request[:params].to_json)
|
52
|
+
#http.get @rpc_path + "?" + rpc_request.map{|a,b| "#{a}=#{b}"}.join("&")
|
53
|
+
rpc_request[:params] = rpc_request[:params].to_json
|
54
|
+
res = @session.get(@rpc_url + @rpc_path, rpc_request)
|
55
|
+
when :post
|
56
|
+
res = @session.post(@rpc_url + @rpc_path, rpc_request.to_json)
|
57
|
+
# req = Net::HTTP::Post.new(@rpc_path)
|
58
|
+
# req.body = rpc_request.to_json
|
59
|
+
# http.request(req)
|
60
|
+
else
|
61
|
+
raise "Unknown HTTP method #{method_http}"
|
47
62
|
end
|
48
63
|
|
49
64
|
JSON::parse(res.body)
|
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
|
+
- 6
|
9
|
+
version: 0.1.6
|
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-
|
17
|
+
date: 2011-07-05 00:00:00 +03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -28,10 +28,11 @@ extra_rdoc_files: []
|
|
28
28
|
|
29
29
|
files:
|
30
30
|
- lib/json-rpc.rb
|
31
|
-
- test/unit/test_rpc_module.rb
|
32
|
-
- test/functional/test-mixed.rb
|
33
31
|
- test/functional/app-mixed.ru
|
32
|
+
- test/functional/test-mixed.rb
|
33
|
+
- test/functional/#app-mixed.ru#
|
34
34
|
- test/functional/rpc-test.rb
|
35
|
+
- test/unit/test_rpc_module.rb
|
35
36
|
- example/async_app.ru
|
36
37
|
- example/rack_app.ru
|
37
38
|
- README.md
|