json-rpc 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -3
- data/lib/json-rpc.rb +42 -7
- metadata +3 -3
data/README.md
CHANGED
@@ -38,8 +38,6 @@ class AsyncApp
|
|
38
38
|
AsyncResponse = [-1, {}, []].freeze
|
39
39
|
def call env
|
40
40
|
result = rpc_call(env)
|
41
|
-
env['async.callback'].call result
|
42
|
-
AsyncResponse
|
43
41
|
end
|
44
42
|
|
45
43
|
def rpc_sum a, b
|
@@ -56,7 +54,8 @@ end
|
|
56
54
|
Test it works:
|
57
55
|
|
58
56
|
~~~~~~ {sh}
|
59
|
-
$
|
57
|
+
$ thin -R example.ru -p 4242 -d start
|
58
|
+
$ curl "http://localhost:4242/rpc?jsonrpc=2.0&method=sum¶ms=%5B21%2C21%5D"
|
60
59
|
{"jsonrpc":"2.0","id":0,"result":42}
|
61
60
|
~~~~~~
|
62
61
|
|
data/lib/json-rpc.rb
CHANGED
@@ -20,7 +20,15 @@ module JsonRpc
|
|
20
20
|
ecb.call(e) if ecb
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
header = {'Content-Type' => Rpc::ContentType}
|
24
|
+
|
25
|
+
if result.is_a?(Rpc::AsyncResult)
|
26
|
+
result.status = status
|
27
|
+
result.header = header
|
28
|
+
return [-1, {}, result]
|
29
|
+
end
|
30
|
+
|
31
|
+
[status, header, result]
|
24
32
|
end
|
25
33
|
|
26
34
|
module Rpc
|
@@ -43,10 +51,15 @@ module JsonRpc
|
|
43
51
|
@status, @code, @msg, @msg_debug = status, code, msg, msg_debug
|
44
52
|
end
|
45
53
|
def result
|
46
|
-
res = {
|
47
|
-
"
|
54
|
+
res = {
|
55
|
+
"id" => id,
|
56
|
+
"jsonrpc" => "2.0",
|
57
|
+
"error" => {
|
58
|
+
"code" => code,
|
59
|
+
"message" => msg
|
60
|
+
}
|
48
61
|
}
|
49
|
-
res.delete_if { |k, v| v == nil}
|
62
|
+
res.delete_if { |k, v| v == nil }
|
50
63
|
res.to_json
|
51
64
|
end
|
52
65
|
def to_s
|
@@ -73,14 +86,14 @@ module JsonRpc
|
|
73
86
|
case env["REQUEST_METHOD"]
|
74
87
|
when "POST"
|
75
88
|
JSON.parse(env["rack.input"].read)
|
76
|
-
when
|
89
|
+
when /^(GET|HEAD)$/
|
77
90
|
req = Rack::Request.new(env)
|
78
91
|
obj = req.params
|
79
92
|
obj["id"] = obj["id"].to_i
|
80
93
|
obj["params"] = obj["params"] ? JSON::parse(obj["params"]) : []
|
81
94
|
obj
|
82
95
|
else
|
83
|
-
raise error :invalid_request, nil, "
|
96
|
+
raise error :invalid_request, nil, "unsupported method #{env["REQUEST_METHOD"]} params: #{obj.inspect}"
|
84
97
|
end
|
85
98
|
|
86
99
|
rescue JSON::ParserError => e
|
@@ -114,12 +127,34 @@ module JsonRpc
|
|
114
127
|
include EventMachine::Deferrable
|
115
128
|
|
116
129
|
attr_reader :response
|
117
|
-
attr_accessor :id
|
130
|
+
attr_accessor :id, :status, :header
|
131
|
+
|
132
|
+
def initialize env = nil
|
133
|
+
@env = env
|
134
|
+
@status = 200
|
135
|
+
@header = {}
|
136
|
+
@header_sent = false
|
137
|
+
end
|
138
|
+
|
139
|
+
def send_header
|
140
|
+
raise "You should pass env to AsyncResult.new()" unless @env
|
141
|
+
@env['async.callback'].call([@status, @header, self]) unless @header_sent
|
142
|
+
@header_sent = true
|
143
|
+
end
|
118
144
|
|
119
145
|
def reply obj
|
146
|
+
send_header unless @callback
|
120
147
|
@callback.call(Rpc::forge_response(obj, @id))
|
121
148
|
end
|
122
149
|
|
150
|
+
def failed error_code = :internal_error, msg = nil
|
151
|
+
e = Rpc::error(error_code, id, msg)
|
152
|
+
@status = e.status
|
153
|
+
send_header
|
154
|
+
@callback.call(e.result)
|
155
|
+
succeed
|
156
|
+
end
|
157
|
+
|
123
158
|
#FIXME thin specific
|
124
159
|
def each &blk
|
125
160
|
@callback = blk
|
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
|
+
- 4
|
9
|
+
version: 0.1.4
|
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-27 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|