json-rpc-objects 0.1.0
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/.document +5 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +29 -0
- data/LICENSE.txt +20 -0
- data/README.md +118 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/json-rpc-objects.gemspec +106 -0
- data/lib/json-rpc-objects/error.rb +29 -0
- data/lib/json-rpc-objects/generic.rb +144 -0
- data/lib/json-rpc-objects/request.rb +76 -0
- data/lib/json-rpc-objects/response.rb +73 -0
- data/lib/json-rpc-objects/v10/error.rb +2 -0
- data/lib/json-rpc-objects/v10/fakes/error.rb +69 -0
- data/lib/json-rpc-objects/v10/request.rb +168 -0
- data/lib/json-rpc-objects/v10/response.rb +189 -0
- data/lib/json-rpc-objects/v10/tests/test.rb +20 -0
- data/lib/json-rpc-objects/v11/alt/error.rb +58 -0
- data/lib/json-rpc-objects/v11/alt/fakes/request.rb +33 -0
- data/lib/json-rpc-objects/v11/alt/fakes/response.rb +33 -0
- data/lib/json-rpc-objects/v11/alt/procedure-call.rb +81 -0
- data/lib/json-rpc-objects/v11/alt/procedure-parameter-description.rb +39 -0
- data/lib/json-rpc-objects/v11/alt/procedure-return.rb +45 -0
- data/lib/json-rpc-objects/v11/alt/request.rb +2 -0
- data/lib/json-rpc-objects/v11/alt/response.rb +2 -0
- data/lib/json-rpc-objects/v11/alt/service-description.rb +45 -0
- data/lib/json-rpc-objects/v11/alt/service-procedure-description.rb +58 -0
- data/lib/json-rpc-objects/v11/alt/tests/test.rb +39 -0
- data/lib/json-rpc-objects/v11/generic-types.rb +52 -0
- data/lib/json-rpc-objects/v11/wd/error.rb +172 -0
- data/lib/json-rpc-objects/v11/wd/extensions.rb +76 -0
- data/lib/json-rpc-objects/v11/wd/fakes/request.rb +33 -0
- data/lib/json-rpc-objects/v11/wd/fakes/response.rb +33 -0
- data/lib/json-rpc-objects/v11/wd/procedure-call.rb +205 -0
- data/lib/json-rpc-objects/v11/wd/procedure-parameter-description.rb +179 -0
- data/lib/json-rpc-objects/v11/wd/procedure-return.rb +155 -0
- data/lib/json-rpc-objects/v11/wd/request.rb +2 -0
- data/lib/json-rpc-objects/v11/wd/response.rb +2 -0
- data/lib/json-rpc-objects/v11/wd/service-description.rb +251 -0
- data/lib/json-rpc-objects/v11/wd/service-procedure-description.rb +234 -0
- data/lib/json-rpc-objects/v11/wd/tests/test.rb +39 -0
- data/lib/json-rpc-objects/v20/error.rb +78 -0
- data/lib/json-rpc-objects/v20/request.rb +127 -0
- data/lib/json-rpc-objects/v20/response.rb +107 -0
- data/lib/json-rpc-objects/v20/tests/test.rb +24 -0
- data/lib/json-rpc-objects/version.rb +98 -0
- metadata +230 -0
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "json-rpc-objects/v20/request"
|
3
|
+
require "yajl/json_gem"
|
4
|
+
|
5
|
+
##
|
6
|
+
# Main JSON-RPC Objects module.
|
7
|
+
#
|
8
|
+
|
9
|
+
module JsonRpcObjects
|
10
|
+
|
11
|
+
##
|
12
|
+
# Request module for version detection and universal API.
|
13
|
+
#
|
14
|
+
|
15
|
+
module Request
|
16
|
+
|
17
|
+
##
|
18
|
+
# Parses JSON-RPC string for request and uses differential
|
19
|
+
# heuristic for detecting the right class.
|
20
|
+
#
|
21
|
+
# Be warn, it's impossible to distinguish JSON-RPC 1.1 Alt
|
22
|
+
# and WD if there aren't keyword parameters in request. So
|
23
|
+
# requests without them are detected as WD. Set the default 1.1
|
24
|
+
# version by second argument.
|
25
|
+
#
|
26
|
+
# @param [String] string JSON string for parse
|
27
|
+
# @param [:wd, :alt] default_v11 type of the eventually returned
|
28
|
+
# 1.1 object
|
29
|
+
#
|
30
|
+
|
31
|
+
def self.parse(string, default_v11 = :wd)
|
32
|
+
data = JSON.load(string)
|
33
|
+
|
34
|
+
if not data.kind_of? Hash
|
35
|
+
raise Exception::new("Data in JSON string aren't object.")
|
36
|
+
end
|
37
|
+
|
38
|
+
data.keys_to_sym!
|
39
|
+
|
40
|
+
# Detects
|
41
|
+
if data.include? :jsonrpc
|
42
|
+
file = "json-rpc-objects/v20/request"
|
43
|
+
cls = V20::Request
|
44
|
+
elsif data.include? :version
|
45
|
+
if (default_v11 == :alt) or (data.include? :kwparams)
|
46
|
+
file = "json-rpc-objects/v11/alt/procedure-call"
|
47
|
+
cls = V11::Alt::ProcedureCall
|
48
|
+
else
|
49
|
+
file = "json-rpc-objects/v11/wd/procedure-call"
|
50
|
+
cls = V11::WD::ProcedureCall
|
51
|
+
end
|
52
|
+
else
|
53
|
+
file = "json-rpc-objects/v10/request"
|
54
|
+
cls = V10::Request
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns
|
58
|
+
require file
|
59
|
+
return cls::new(data)
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
##
|
64
|
+
# Returns request of the latest standard.
|
65
|
+
#
|
66
|
+
# @param [Array] args for target constructor
|
67
|
+
# @return [JsonRpcObjects::V20::Request] request object
|
68
|
+
#
|
69
|
+
|
70
|
+
def self.create(*args)
|
71
|
+
JsonRpcObjects::V20::Request::create(*args)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "json-rpc-objects/v20/response"
|
3
|
+
|
4
|
+
##
|
5
|
+
# Main JSON-RPC Objects module.
|
6
|
+
#
|
7
|
+
|
8
|
+
module JsonRpcObjects
|
9
|
+
|
10
|
+
##
|
11
|
+
# Request module for version detection and universal API.
|
12
|
+
#
|
13
|
+
|
14
|
+
module Response
|
15
|
+
|
16
|
+
##
|
17
|
+
# Parses JSON-RPC string for response and uses differential
|
18
|
+
# heuristic for detecting the right class.
|
19
|
+
#
|
20
|
+
# Be warn, it's impossible to distinguish JSON-RPC 1.1 Alt
|
21
|
+
# and WD if there aren't error. So responsens without it are
|
22
|
+
# detected as WD. Set the default 1.1 version by second argument.
|
23
|
+
#
|
24
|
+
# @param [String] string JSON string for parse
|
25
|
+
# @param [:wd, :alt] default_v11 type of the eventually returned
|
26
|
+
# 1.1 object
|
27
|
+
#
|
28
|
+
|
29
|
+
def self.parse(string, default_v11 = :wd)
|
30
|
+
data = JSON.load(string)
|
31
|
+
|
32
|
+
if not data.kind_of? Hash
|
33
|
+
raise Exception::new("Data in JSON string aren't object.")
|
34
|
+
end
|
35
|
+
|
36
|
+
data.keys_to_sym!
|
37
|
+
|
38
|
+
# Detects
|
39
|
+
if data.include? :jsonrpc
|
40
|
+
file = "json-rpc-objects/v20/response"
|
41
|
+
cls = V20::Response
|
42
|
+
elsif data.include? :version
|
43
|
+
if (default_v11 == :wd) or ((data.include? :error) and (data[:error].kind_of? Hash) and (data[:error].include? "name"))
|
44
|
+
file = "json-rpc-objects/v11/wd/procedure-return"
|
45
|
+
cls = V11::WD::ProcedureReturn
|
46
|
+
else
|
47
|
+
file = "json-rpc-objects/v11/alt/procedure-return"
|
48
|
+
cls = V11::Alt::ProcedureReturn
|
49
|
+
end
|
50
|
+
else
|
51
|
+
file = "json-rpc-objects/v10/response"
|
52
|
+
cls = V10::Response
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns
|
56
|
+
require file
|
57
|
+
return cls::new(data)
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# Returns request of the latest standard.
|
62
|
+
#
|
63
|
+
# @param [Array] args for target constructor
|
64
|
+
# @return [JsonRpcObjects::V20::Response] response object
|
65
|
+
#
|
66
|
+
|
67
|
+
def self.create(*args)
|
68
|
+
JsonRpcObjects::V20::Response::create(*args)
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "json-rpc-objects/generic"
|
3
|
+
|
4
|
+
##
|
5
|
+
# Main JSON-RPC Objects module.
|
6
|
+
#
|
7
|
+
|
8
|
+
module JsonRpcObjects
|
9
|
+
|
10
|
+
##
|
11
|
+
# Module of JSON-RPC 1.0.
|
12
|
+
# @see http://json-rpc.org/wiki/specification
|
13
|
+
#
|
14
|
+
|
15
|
+
module V10
|
16
|
+
|
17
|
+
##
|
18
|
+
# In fact, fake error class.
|
19
|
+
#
|
20
|
+
|
21
|
+
class Error < JsonRpcObjects::Generic::Object
|
22
|
+
|
23
|
+
##
|
24
|
+
# Holds link to its version module.
|
25
|
+
#
|
26
|
+
|
27
|
+
VERSION = JsonRpcObjects::V10
|
28
|
+
|
29
|
+
##
|
30
|
+
# Creates new one.
|
31
|
+
#
|
32
|
+
# @param [Object] data error data
|
33
|
+
# @return [JsonRpcObjects::V10::Error] new object
|
34
|
+
#
|
35
|
+
|
36
|
+
def self.create(data)
|
37
|
+
self::new(data)
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
# Holds request method name.
|
42
|
+
#
|
43
|
+
|
44
|
+
@data
|
45
|
+
attr_accessor :data
|
46
|
+
|
47
|
+
##
|
48
|
+
# Renders data to output form.
|
49
|
+
# @return [Object] with data of object
|
50
|
+
#
|
51
|
+
|
52
|
+
def output
|
53
|
+
@data
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
protected
|
58
|
+
|
59
|
+
##
|
60
|
+
# Assigns request data.
|
61
|
+
#
|
62
|
+
|
63
|
+
def data=(value, mode = nil)
|
64
|
+
@data = value
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "json-rpc-objects/generic"
|
3
|
+
|
4
|
+
##
|
5
|
+
# Main JSON-RPC Objects module.
|
6
|
+
#
|
7
|
+
|
8
|
+
module JsonRpcObjects
|
9
|
+
|
10
|
+
##
|
11
|
+
# Module of JSON-RPC 1.0.
|
12
|
+
# @see http://json-rpc.org/wiki/specification
|
13
|
+
#
|
14
|
+
|
15
|
+
module V10
|
16
|
+
|
17
|
+
##
|
18
|
+
# Request object class.
|
19
|
+
#
|
20
|
+
|
21
|
+
class Request < JsonRpcObjects::Generic::Object
|
22
|
+
|
23
|
+
##
|
24
|
+
# Holds link to its version module.
|
25
|
+
#
|
26
|
+
|
27
|
+
VERSION = JsonRpcObjects::V10
|
28
|
+
|
29
|
+
##
|
30
|
+
# Holds request method name.
|
31
|
+
#
|
32
|
+
|
33
|
+
@method
|
34
|
+
attr_accessor :method
|
35
|
+
|
36
|
+
##
|
37
|
+
# Holds params for requested method.
|
38
|
+
#
|
39
|
+
|
40
|
+
@params
|
41
|
+
attr_accessor :params
|
42
|
+
|
43
|
+
##
|
44
|
+
# Call ID.
|
45
|
+
#
|
46
|
+
|
47
|
+
@id
|
48
|
+
attr_accessor :id
|
49
|
+
|
50
|
+
##
|
51
|
+
# Creates new one.
|
52
|
+
#
|
53
|
+
# @param [Symbol] method of the request
|
54
|
+
# @param [Array] params array of arguments for the request
|
55
|
+
# @param [Hash] opts additional options
|
56
|
+
# @return [V10::Request] new request
|
57
|
+
#
|
58
|
+
|
59
|
+
def self.create(method, params = [ ], opts = { })
|
60
|
+
data = {
|
61
|
+
:method => method,
|
62
|
+
:params => params
|
63
|
+
}
|
64
|
+
|
65
|
+
data.merge! opts
|
66
|
+
return self::new(data)
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Checks correctness of the request data.
|
71
|
+
#
|
72
|
+
|
73
|
+
def check!
|
74
|
+
self.normalize!
|
75
|
+
__check_method
|
76
|
+
__check_params
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Renders data to output hash.
|
81
|
+
# @return [Hash] with data of request
|
82
|
+
#
|
83
|
+
|
84
|
+
def output
|
85
|
+
self.check!
|
86
|
+
data = {
|
87
|
+
:method => @method.to_s,
|
88
|
+
:params => @params,
|
89
|
+
:id => @id
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
##
|
94
|
+
# Indicates, it's notification.
|
95
|
+
# @return [Boolean] true if it is, otherwise false
|
96
|
+
#
|
97
|
+
|
98
|
+
def notification?
|
99
|
+
@id.nil?
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
protected
|
104
|
+
|
105
|
+
##
|
106
|
+
# Assigns request data.
|
107
|
+
#
|
108
|
+
|
109
|
+
def data=(value, mode = nil)
|
110
|
+
data = __convert_data(value, mode)
|
111
|
+
|
112
|
+
@method = data[:method]
|
113
|
+
@params = data[:params]
|
114
|
+
@id = data[:id]
|
115
|
+
end
|
116
|
+
|
117
|
+
##
|
118
|
+
# Converts request data to standard (defined) format.
|
119
|
+
#
|
120
|
+
|
121
|
+
def normalize!
|
122
|
+
__normalize_method
|
123
|
+
__normalize_params
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# Checks method data.
|
128
|
+
#
|
129
|
+
|
130
|
+
def __check_method
|
131
|
+
if not @method.kind_of? Symbol
|
132
|
+
raise Exception::new("Service name must be Symbol or convertable to Symbol.")
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
##
|
137
|
+
# Checks params data.
|
138
|
+
#
|
139
|
+
|
140
|
+
def __check_params
|
141
|
+
if not @params.kind_of? Array
|
142
|
+
raise Exception::new("Params must be Array.")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
##
|
147
|
+
# Normalizes method.
|
148
|
+
#
|
149
|
+
|
150
|
+
def __normalize_method
|
151
|
+
if @method.kind_of? String
|
152
|
+
@method = @method.to_sym
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
##
|
157
|
+
# Normalizes params.
|
158
|
+
#
|
159
|
+
|
160
|
+
def __normalize_params
|
161
|
+
if @params.nil?
|
162
|
+
@params = [ ]
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,189 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "json-rpc-objects/generic"
|
3
|
+
require "json-rpc-objects/v10/error"
|
4
|
+
|
5
|
+
##
|
6
|
+
# Main JSON-RPC Objects module.
|
7
|
+
#
|
8
|
+
|
9
|
+
module JsonRpcObjects
|
10
|
+
|
11
|
+
##
|
12
|
+
# Module of JSON-RPC 1.0.
|
13
|
+
# @see http://json-rpc.org/wiki/specification
|
14
|
+
#
|
15
|
+
|
16
|
+
module V10
|
17
|
+
|
18
|
+
##
|
19
|
+
# Response object class.
|
20
|
+
#
|
21
|
+
|
22
|
+
class Response < JsonRpcObjects::Generic::Object
|
23
|
+
|
24
|
+
##
|
25
|
+
# Holds link to its version module.
|
26
|
+
#
|
27
|
+
|
28
|
+
VERSION = JsonRpcObjects::V10
|
29
|
+
|
30
|
+
##
|
31
|
+
# Identifies the error object class.
|
32
|
+
#
|
33
|
+
|
34
|
+
ERROR_CLASS = JsonRpcObjects::V10::Error
|
35
|
+
|
36
|
+
##
|
37
|
+
# Holds result data.
|
38
|
+
#
|
39
|
+
|
40
|
+
@result
|
41
|
+
attr_accessor :result
|
42
|
+
|
43
|
+
##
|
44
|
+
# Holds error data.
|
45
|
+
#
|
46
|
+
|
47
|
+
@error
|
48
|
+
attr_accessor :error
|
49
|
+
|
50
|
+
##
|
51
|
+
# Call ID.
|
52
|
+
#
|
53
|
+
|
54
|
+
@id
|
55
|
+
attr_accessor :id
|
56
|
+
|
57
|
+
##
|
58
|
+
# Creates new one.
|
59
|
+
#
|
60
|
+
# @param [Object] result of the call for response
|
61
|
+
# @param [Object] error of the call for response
|
62
|
+
# @param [Hash] opts additional options
|
63
|
+
# @return [V10::Response] new response
|
64
|
+
#
|
65
|
+
|
66
|
+
def self.create(result = nil, error = nil, opts = { })
|
67
|
+
data = {
|
68
|
+
:result => result,
|
69
|
+
:error => error
|
70
|
+
}
|
71
|
+
|
72
|
+
data.merge! opts
|
73
|
+
return self::new(data)
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Checks correctness of the request data.
|
78
|
+
#
|
79
|
+
|
80
|
+
def check!
|
81
|
+
self.normalize!
|
82
|
+
|
83
|
+
__check_coherency
|
84
|
+
__check_id
|
85
|
+
__check_error
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Renders data to output hash.
|
90
|
+
# @return [Hash] with data of response
|
91
|
+
#
|
92
|
+
|
93
|
+
def output
|
94
|
+
self.check!
|
95
|
+
data = {
|
96
|
+
:result => @result,
|
97
|
+
:error => @error,
|
98
|
+
:id => @id
|
99
|
+
}
|
100
|
+
|
101
|
+
return data
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
# Receives result by array fill operator.
|
106
|
+
# @param [Object] result for the response
|
107
|
+
#
|
108
|
+
|
109
|
+
def <<(result)
|
110
|
+
@result = result
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
protected
|
115
|
+
|
116
|
+
##
|
117
|
+
# Assigns request data.
|
118
|
+
#
|
119
|
+
|
120
|
+
def data=(value, mode = nil)
|
121
|
+
data = __convert_data(value, mode)
|
122
|
+
|
123
|
+
@result = data[:result]
|
124
|
+
@error = data[:error]
|
125
|
+
@id = data[:id]
|
126
|
+
|
127
|
+
__create_error
|
128
|
+
end
|
129
|
+
|
130
|
+
##
|
131
|
+
# Converts request data to standard (defined) format.
|
132
|
+
#
|
133
|
+
|
134
|
+
def normalize!
|
135
|
+
__normalize_error
|
136
|
+
end
|
137
|
+
|
138
|
+
##
|
139
|
+
# Checks coherency of result and error.
|
140
|
+
#
|
141
|
+
|
142
|
+
def __check_coherency
|
143
|
+
if not @result.nil? and not @error.nil?
|
144
|
+
raise Exception::new("Either result or error must be nil.")
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
##
|
149
|
+
# Checks ID.
|
150
|
+
#
|
151
|
+
def __check_id
|
152
|
+
if @id.nil?
|
153
|
+
raise Exception::new("ID is required for 1.0 responses.")
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
##
|
158
|
+
# Creates error object.
|
159
|
+
#
|
160
|
+
|
161
|
+
def __create_error
|
162
|
+
if not @error.nil? and not @error.kind_of? self.class::ERROR_CLASS
|
163
|
+
@error = self.class::ERROR_CLASS::new(@error)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Checks error settings.
|
169
|
+
#
|
170
|
+
|
171
|
+
def __check_error
|
172
|
+
if not @error.nil?
|
173
|
+
if not @error.kind_of? self.class::ERROR_CLASS
|
174
|
+
raise Exception::new("Error object must be of type " << self.class::ERROR_CLASS.name << ".")
|
175
|
+
end
|
176
|
+
|
177
|
+
@error.check!
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
##
|
182
|
+
# Normalizes error.
|
183
|
+
#
|
184
|
+
|
185
|
+
alias :__normalize_error :__create_error
|
186
|
+
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|