json-rpc 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -0
- data/lib/json-rpc.rb +15 -12
- metadata +2 -2
data/README.md
CHANGED
@@ -53,6 +53,13 @@ class AsyncApp
|
|
53
53
|
end
|
54
54
|
~~~~~~
|
55
55
|
|
56
|
+
Test it works:
|
57
|
+
|
58
|
+
~~~~~~ {sh}
|
59
|
+
$ curl "http://localhost:3000/rpc?jsonrpc=2.0&method=sum¶ms=%5B21%2C21%5D"
|
60
|
+
{"jsonrpc":"2.0","id":0,"result":42}
|
61
|
+
~~~~~~
|
62
|
+
|
56
63
|
### License
|
57
64
|
Copyright 2011 [Helios Technologies Ltd.](http://www.heliostech.hk)
|
58
65
|
|
data/lib/json-rpc.rb
CHANGED
@@ -38,9 +38,9 @@ module JsonRpc
|
|
38
38
|
|
39
39
|
class Error < RuntimeError
|
40
40
|
attr_reader :status, :code, :msg
|
41
|
-
attr_accessor :id
|
42
|
-
def initialize status, code, msg
|
43
|
-
@status, @code, @msg = status, code, msg
|
41
|
+
attr_accessor :id, :msg_debug
|
42
|
+
def initialize status, code, msg, msg_debug = nil
|
43
|
+
@status, @code, @msg, @msg_debug = status, code, msg, msg_debug
|
44
44
|
end
|
45
45
|
def result
|
46
46
|
res = {"jsonrpc" => "2.0", "id" => id,
|
@@ -49,11 +49,14 @@ module JsonRpc
|
|
49
49
|
res.delete_if { |k, v| v == nil}
|
50
50
|
res.to_json
|
51
51
|
end
|
52
|
+
def to_s
|
53
|
+
msg_debug || super
|
54
|
+
end
|
52
55
|
end
|
53
56
|
|
54
|
-
def self.error index, id = nil
|
57
|
+
def self.error index, id = nil, debug_msg = nil
|
55
58
|
id = nil unless id.is_a? Fixnum
|
56
|
-
ex = Rpc::Error.new *ErrorProtocol[index]
|
59
|
+
ex = Rpc::Error.new *ErrorProtocol[index], debug_msg
|
57
60
|
ex.id = id
|
58
61
|
ex
|
59
62
|
end
|
@@ -62,7 +65,7 @@ module JsonRpc
|
|
62
65
|
return 200 if request["jsonrpc"] == Version and
|
63
66
|
request["method"].kind_of?(String) and
|
64
67
|
request["method"] != ""
|
65
|
-
raise error :invalid_request, request["id"]
|
68
|
+
raise error :invalid_request, request["id"], "invalid request: #{request.inspect}"
|
66
69
|
end
|
67
70
|
|
68
71
|
def self.parse env
|
@@ -73,15 +76,15 @@ module JsonRpc
|
|
73
76
|
when "GET"
|
74
77
|
req = Rack::Request.new(env)
|
75
78
|
obj = req.params
|
76
|
-
obj["id"] = obj["id"]
|
77
|
-
obj["params"] = JSON::parse(obj["params"])
|
79
|
+
obj["id"] = obj["id"].to_i
|
80
|
+
obj["params"] = obj["params"] ? JSON::parse(obj["params"]) : []
|
78
81
|
obj
|
79
82
|
else
|
80
|
-
raise error :invalid_request
|
83
|
+
raise error :invalid_request, nil, "invalid params: #{obj.inspect}"
|
81
84
|
end
|
82
85
|
|
83
|
-
rescue JSON::ParserError
|
84
|
-
raise error :parse_error
|
86
|
+
rescue JSON::ParserError => e
|
87
|
+
raise error :parse_error, nil, "JSON parsing error: #{e}"
|
85
88
|
end
|
86
89
|
end
|
87
90
|
|
@@ -89,7 +92,7 @@ module JsonRpc
|
|
89
92
|
method, params = Prefix + request["method"], request["params"]
|
90
93
|
|
91
94
|
unless ctrl.respond_to? method
|
92
|
-
raise error :method_not_found, request["id"]
|
95
|
+
raise error :method_not_found, request["id"], "method `#{method}` not found"
|
93
96
|
end
|
94
97
|
|
95
98
|
result = ctrl.send(method, *params)
|