json-rpc-objects 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.document +5 -0
  2. data/Gemfile +16 -0
  3. data/Gemfile.lock +29 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.md +118 -0
  6. data/Rakefile +37 -0
  7. data/VERSION +1 -0
  8. data/json-rpc-objects.gemspec +106 -0
  9. data/lib/json-rpc-objects/error.rb +29 -0
  10. data/lib/json-rpc-objects/generic.rb +144 -0
  11. data/lib/json-rpc-objects/request.rb +76 -0
  12. data/lib/json-rpc-objects/response.rb +73 -0
  13. data/lib/json-rpc-objects/v10/error.rb +2 -0
  14. data/lib/json-rpc-objects/v10/fakes/error.rb +69 -0
  15. data/lib/json-rpc-objects/v10/request.rb +168 -0
  16. data/lib/json-rpc-objects/v10/response.rb +189 -0
  17. data/lib/json-rpc-objects/v10/tests/test.rb +20 -0
  18. data/lib/json-rpc-objects/v11/alt/error.rb +58 -0
  19. data/lib/json-rpc-objects/v11/alt/fakes/request.rb +33 -0
  20. data/lib/json-rpc-objects/v11/alt/fakes/response.rb +33 -0
  21. data/lib/json-rpc-objects/v11/alt/procedure-call.rb +81 -0
  22. data/lib/json-rpc-objects/v11/alt/procedure-parameter-description.rb +39 -0
  23. data/lib/json-rpc-objects/v11/alt/procedure-return.rb +45 -0
  24. data/lib/json-rpc-objects/v11/alt/request.rb +2 -0
  25. data/lib/json-rpc-objects/v11/alt/response.rb +2 -0
  26. data/lib/json-rpc-objects/v11/alt/service-description.rb +45 -0
  27. data/lib/json-rpc-objects/v11/alt/service-procedure-description.rb +58 -0
  28. data/lib/json-rpc-objects/v11/alt/tests/test.rb +39 -0
  29. data/lib/json-rpc-objects/v11/generic-types.rb +52 -0
  30. data/lib/json-rpc-objects/v11/wd/error.rb +172 -0
  31. data/lib/json-rpc-objects/v11/wd/extensions.rb +76 -0
  32. data/lib/json-rpc-objects/v11/wd/fakes/request.rb +33 -0
  33. data/lib/json-rpc-objects/v11/wd/fakes/response.rb +33 -0
  34. data/lib/json-rpc-objects/v11/wd/procedure-call.rb +205 -0
  35. data/lib/json-rpc-objects/v11/wd/procedure-parameter-description.rb +179 -0
  36. data/lib/json-rpc-objects/v11/wd/procedure-return.rb +155 -0
  37. data/lib/json-rpc-objects/v11/wd/request.rb +2 -0
  38. data/lib/json-rpc-objects/v11/wd/response.rb +2 -0
  39. data/lib/json-rpc-objects/v11/wd/service-description.rb +251 -0
  40. data/lib/json-rpc-objects/v11/wd/service-procedure-description.rb +234 -0
  41. data/lib/json-rpc-objects/v11/wd/tests/test.rb +39 -0
  42. data/lib/json-rpc-objects/v20/error.rb +78 -0
  43. data/lib/json-rpc-objects/v20/request.rb +127 -0
  44. data/lib/json-rpc-objects/v20/response.rb +107 -0
  45. data/lib/json-rpc-objects/v20/tests/test.rb +24 -0
  46. data/lib/json-rpc-objects/version.rb +98 -0
  47. metadata +230 -0
@@ -0,0 +1,234 @@
1
+ # encoding: utf-8
2
+ require "addressable/uri"
3
+ require "multitype-introspection"
4
+ require "types"
5
+ require "hash-utils/array"
6
+ require "json-rpc-objects/v11/wd/procedure-parameter-description"
7
+ require "json-rpc-objects/generic"
8
+
9
+ ##
10
+ # Main JSON-RPC Objects module.
11
+ #
12
+
13
+ module JsonRpcObjects
14
+
15
+ ##
16
+ # General module of JSON-RPC 1.1.
17
+ #
18
+
19
+ module V11
20
+
21
+ ##
22
+ # Module of JSON-RPC 1.1 Working Draft.
23
+ # @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
24
+ #
25
+
26
+ module WD
27
+
28
+ ##
29
+ # Description of one procedure of the service.
30
+ #
31
+
32
+ class ServiceProcedureDescription < JsonRpcObjects::Generic::Object
33
+
34
+ ##
35
+ # Holds link to its version module.
36
+ #
37
+
38
+ VERSION = JsonRpcObjects::V11::WD
39
+
40
+ ##
41
+ # Indicates the procedure parameter description class.
42
+ #
43
+
44
+ PARAMETER_DESCRIPTION_CLASS = JsonRpcObjects::V11::WD::ProcedureParameterDescription
45
+
46
+ ##
47
+ # Holds procedure name.
48
+ #
49
+
50
+ @name
51
+ attr_accessor :name
52
+
53
+ ##
54
+ # Holds procedure summary.
55
+ #
56
+
57
+ @summary
58
+ attr_accessor :summary
59
+
60
+ ##
61
+ # Holds procedure help URL.
62
+ #
63
+
64
+ @help
65
+ attr_accessor :url
66
+
67
+ ##
68
+ # Indicates procedure idempotency.
69
+ #
70
+
71
+ @idempotent
72
+ attr_accessor :idempotent
73
+
74
+ ##
75
+ # Holds procedure params specification.
76
+ #
77
+
78
+ @params
79
+ attr_accessor :params
80
+
81
+ ##
82
+ # Holds procedure return value specification.
83
+ #
84
+
85
+ @return
86
+ attr_accessor :return
87
+
88
+ ##
89
+ # Creates new one.
90
+ #
91
+ # @param [Symbol] name name of the procedure
92
+ # @param [Hash] opts additional options
93
+ # @return [V11:ServiceProcedureDescription] new description object
94
+ #
95
+
96
+ def self.create(name, opts = { })
97
+ data = { :name => name }
98
+ data.merge! opts
99
+ return self::new(data)
100
+ end
101
+
102
+ ##
103
+ # Checks correctness of the data.
104
+ #
105
+
106
+ def check!
107
+ self.normalize!
108
+
109
+ if not @name.kind_of? Symbol
110
+ raise Exception::new("Procedure name must be Symbol or convertable to Symbol.")
111
+ end
112
+
113
+ if not @params.nil?
114
+ if (not @params.kind_of? Array) or (not @params.all? { |v| v.kind_of? self.class::PARAMETER_DESCRIPTION_CLASS })
115
+ raise Exception::new("If params is defined, must be an Array of " << self.class::PARAMETER_DESCRIPTION_CLASS.name << " objects.")
116
+ end
117
+
118
+ if @params.kind_of? Array
119
+ @params.each { |param| param.check! }
120
+ end
121
+ end
122
+
123
+ if not @return.nil?
124
+ if not @return.kind_of? self.class::PARAMETER_DESCRIPTION_CLASS
125
+ raise Exception::new("If return is defined, must be set to " << self.class::PARAMETER_DESCRIPTION_CLASS.name << " object.")
126
+ end
127
+
128
+ @return.check!
129
+ end
130
+
131
+ if (not @idempotent.nil?) and (not @idempotent.type_of? Boolean)
132
+ raise Exception::new("If idempotent is defined, must be boolean.")
133
+ end
134
+ end
135
+
136
+ ##
137
+ # Renders data to output hash.
138
+ # @return [Hash] with data of description
139
+ #
140
+
141
+ def output
142
+ self.check!
143
+ data = { :name => @name.to_s }
144
+
145
+ if not @summary.nil?
146
+ data[:summary] = @summary
147
+ end
148
+
149
+ if not @help.nil?
150
+ data[:help] = @help.to_s
151
+ end
152
+
153
+ if not @idempotent.nil?
154
+ data[:idempotent] = @idempotent
155
+ end
156
+
157
+ if not @params.nil?
158
+ data[:params] = @params
159
+ end
160
+
161
+ if not @return.nil?
162
+ data[:return] = @return
163
+ end
164
+
165
+ return data
166
+ end
167
+
168
+ ##
169
+ # Receives service procedure description objects.
170
+ #
171
+ # @param [ProcedureParameterDescription] value with new
172
+ # service procedure descriptor
173
+ #
174
+
175
+ def <<(value)
176
+ if not value.kind_of? self.class::PARAMETER_DESCRIPTION_CLASS
177
+ raise Exception::new(self.class::PARAMETER_DESCRIPTION_CLASS.name.dup << " object expected.")
178
+ end
179
+
180
+ if @params.nil?
181
+ @params = [ ]
182
+ end
183
+
184
+ @params << value
185
+ end
186
+
187
+
188
+ protected
189
+
190
+ ##
191
+ # Assigns request data.
192
+ #
193
+
194
+ def data=(value, mode = nil)
195
+ data = __convert_data(value, mode)
196
+
197
+ @name = data[:name]
198
+ @summary = data[:summary]
199
+ @help = data[:help]
200
+ @idempotent = data[:idempotent]
201
+ @params = data[:params]
202
+ @return = data[:return]
203
+
204
+ if @params.kind_of? Array
205
+ @params = @params.map { |v| self.class::PARAMETER_DESCRIPTION_CLASS::new(v) }
206
+ end
207
+
208
+ if @return.kind_of? Hash
209
+ @return = self.class::PARAMETER_DESCRIPTION_CLASS::new(@return)
210
+ end
211
+ end
212
+
213
+ ##
214
+ # Converts request data to standard (defined) format.
215
+ #
216
+
217
+ def normalize!
218
+ if @name.kind_of? String
219
+ @name = @name.to_sym
220
+ end
221
+
222
+ if not @summary.nil?
223
+ @summary = @summary.to_s
224
+ end
225
+
226
+ if (not @help.nil?) and (not @help.kind_of? Addressable::URI)
227
+ @help = Addressable::URI::parse(@help.to_s)
228
+ end
229
+ end
230
+
231
+ end
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ $:.push("../../../..")
3
+
4
+ require "../procedure-call"
5
+ req = JsonRpcObjects::V11::WD::ProcedureCall::create(:alfa, {"0" => :beta, "something" => :alfa}, :id => 12345, :"$whatever" => false)
6
+ puts req.to_json
7
+
8
+ require "../error"
9
+ err = JsonRpcObjects::V11::WD::Error::create(200, "some problem")
10
+
11
+ require "../procedure-return"
12
+ res = JsonRpcObjects::V11::WD::ProcedureReturn::create(nil, err, :id => 12345)
13
+ puts res.to_json
14
+ res = JsonRpcObjects::V11::WD::ProcedureReturn::create(true, nil, :id => 12345)
15
+ puts res.to_json
16
+
17
+ require "../service-description"
18
+ sdesc = JsonRpcObjects::V11::WD::ServiceDescription::create(:alfa, 100)
19
+ puts sdesc.to_json
20
+
21
+ require "../service-procedure-description"
22
+ sproc = JsonRpcObjects::V11::WD::ServiceProcedureDescription::create(:some_proc)
23
+ sdesc << sproc
24
+ puts sdesc.to_json
25
+
26
+ require "../procedure-parameter-description"
27
+ sparam1 = JsonRpcObjects::V11::WD::ProcedureParameterDescription::create(:param1, :type => :str)
28
+ sparam2 = JsonRpcObjects::V11::WD::ProcedureParameterDescription::create(:param2, :type => JsonRpcObjects)
29
+ sproc << sparam1
30
+ sproc << sparam2
31
+ puts sdesc.to_json
32
+
33
+
34
+ require "../../../request"
35
+ puts JsonRpcObjects::Request::parse(req.to_json).inspect
36
+ require "../../../response"
37
+ puts JsonRpcObjects::Response::parse(res.to_json).inspect
38
+
39
+ puts req.class::version.response::create(25)
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+ require "hash-utils/numeric"
3
+ require "json-rpc-objects/v11/alt/error"
4
+
5
+ ##
6
+ # Main JSON-RPC Objects module.
7
+ #
8
+
9
+ module JsonRpcObjects
10
+
11
+ ##
12
+ # Module of JSON-RPC 2.0.
13
+ # @see http://groups.google.com/group/json-rpc/web/json-rpc-2-0
14
+ #
15
+
16
+ module V20
17
+
18
+ ##
19
+ # Error description object class for Response.
20
+ #
21
+
22
+ class Error < JsonRpcObjects::V11::Alt::Error
23
+
24
+ ##
25
+ # Holds link to its version module.
26
+ #
27
+
28
+ VERSION = JsonRpcObjects::V20
29
+
30
+ ##
31
+ # Indicates data member name.
32
+ #
33
+
34
+ DATA_MEMBER_NAME = :data
35
+
36
+ ##
37
+ # Checks correctness of the data.
38
+ #
39
+
40
+ def check!
41
+ self.normalize!
42
+
43
+ if (@code.in? -32768..-32000) and not ((@code == -32700) or (@code.in? -32603..-32600) or (@code.in? -32099..-32000))
44
+ raise Exception::new("Code is invalid because of reserved space.")
45
+ end
46
+ end
47
+
48
+ ##
49
+ # Renders data to output hash.
50
+ # @return [Hash] with data of error
51
+ #
52
+
53
+ def output
54
+ result = super()
55
+
56
+ if result.include? :error
57
+ result[:data] = result[:error]
58
+ result.delete(:error)
59
+ end
60
+
61
+ return result
62
+ end
63
+
64
+
65
+ protected
66
+
67
+ ##
68
+ # Assigns error data.
69
+ #
70
+
71
+ def __assign_data(data)
72
+ @data = data[:data]
73
+ data.delete(:data)
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,127 @@
1
+ # encoding: utf-8
2
+ require "multitype-introspection"
3
+ require "hash-utils/hash"
4
+ require "json-rpc-objects/v11/alt/procedure-call"
5
+
6
+ ##
7
+ # Main JSON-RPC Objects module.
8
+ #
9
+
10
+ module JsonRpcObjects
11
+
12
+ ##
13
+ # Module of JSON-RPC 2.0.
14
+ # @see http://groups.google.com/group/json-rpc/web/json-rpc-2-0
15
+ #
16
+
17
+ module V20
18
+
19
+ ##
20
+ # Request object class.
21
+ #
22
+
23
+ class Request < JsonRpcObjects::V11::Alt::ProcedureCall
24
+
25
+ ##
26
+ # Holds link to its version module.
27
+ #
28
+
29
+ VERSION = JsonRpcObjects::V20
30
+
31
+ ##
32
+ # Holds JSON-RPC version specification.
33
+ #
34
+
35
+ VERSION_NUMBER = :"2.0"
36
+
37
+ ##
38
+ # Holds JSON-RPC version member identification.
39
+ #
40
+
41
+ VERSION_MEMBER = :jsonrpc
42
+
43
+ ##
44
+ # Indicates ID has been set.
45
+ #
46
+
47
+ @_id_set
48
+
49
+ ##
50
+ # Checks correctness of the request data.
51
+ #
52
+
53
+ def check!
54
+ super()
55
+
56
+ if not @id.kind_of_any? [String, Integer, NilClass]
57
+ raise Exception::new("ID must contain String, Number or nil if included.")
58
+ end
59
+ end
60
+
61
+ ##
62
+ # Renders data to output hash.
63
+ # @return [Hash] with data of error
64
+ #
65
+
66
+ def output
67
+ result = super()
68
+
69
+ if @_id_set and @id.nil?
70
+ result[:id] = nil
71
+ end
72
+
73
+ return result
74
+ end
75
+
76
+ ##
77
+ # Indicates, it's notification.
78
+ # @return [Boolean] true if it is, otherwise false
79
+ #
80
+
81
+ def notification?
82
+ not @_id_set
83
+ end
84
+
85
+
86
+
87
+ protected
88
+
89
+ ##
90
+ # Assigns request data.
91
+ #
92
+
93
+ def data=(value, mode = nil)
94
+ data = __convert_data(value, mode)
95
+
96
+ # Indicates, ID has been explicitly assigned
97
+ @_id_set = data.include? :id
98
+
99
+ super(data, :converted)
100
+ end
101
+
102
+ ##
103
+ # Gets params from input.
104
+ #
105
+
106
+ def __get_params(data)
107
+ if @params.kind_of? Hash
108
+ @keyword_params = @params.keys_to_sym
109
+ @params = nil
110
+ end
111
+ end
112
+
113
+ ##
114
+ # Assigns the parameters settings.
115
+ #
116
+
117
+ def __assign_params(data, version = nil)
118
+ if not @params.nil? and not @params.empty?
119
+ data[:params] = @params
120
+ elsif not @keyword_params.nil? and not @keyword_params.empty?
121
+ data[:params] = @keyword_params
122
+ end
123
+ end
124
+
125
+ end
126
+ end
127
+ end