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,179 @@
1
+ # encoding: utf-8
2
+ require "multitype-introspection"
3
+ require "json-rpc-objects/v11/generic-types"
4
+ require "json-rpc-objects/generic"
5
+
6
+ ##
7
+ # Main JSON-RPC Objects module.
8
+ #
9
+
10
+ module JsonRpcObjects
11
+
12
+ ##
13
+ # General module of JSON-RPC 1.1.
14
+ #
15
+
16
+ module V11
17
+
18
+ ##
19
+ # Module of Working Draft.
20
+ # @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
21
+ #
22
+
23
+ module WD
24
+
25
+ ##
26
+ # Description of one procedure parameter.
27
+ #
28
+
29
+ class ProcedureParameterDescription < JsonRpcObjects::Generic::Object
30
+
31
+ ##
32
+ # Holds link to its version module.
33
+ #
34
+
35
+ VERSION = JsonRpcObjects::V11::WD
36
+
37
+ ##
38
+ # Maps type to object (class).
39
+ #
40
+
41
+ TYPE_TO_OBJECT = Hash::define({
42
+ :num => Integer,
43
+ :str => String,
44
+ :arr => Array,
45
+ :obj => Object,
46
+ :bit => GenericTypes::Boolean,
47
+ :nil => GenericTypes::Nil,
48
+ :any => GenericTypes::Any
49
+ }, GenericTypes::Any)
50
+
51
+ ##
52
+ # Maps object (class) to type.
53
+ #
54
+
55
+ OBJECT_TO_TYPE = Hash::define({
56
+ Integer => :num,
57
+ String => :str,
58
+ Array => :arr,
59
+ Object => :obj,
60
+ GenericTypes::Boolean => :bit,
61
+ GenericTypes::Nil => :nil,
62
+ GenericTypes::Any => :any
63
+ }, :any)
64
+
65
+ ##
66
+ # Holds parameter name.
67
+ #
68
+
69
+ @name
70
+ attr_accessor :name
71
+
72
+ ##
73
+ # Holds parameter type.
74
+ #
75
+
76
+ @type
77
+ attr_accessor :type
78
+
79
+ ##
80
+ # Creates new one.
81
+ #
82
+ # @param [Symbol] name name of the parameter
83
+ # @return [V11:ProcedureParameterDescription] new description object
84
+ #
85
+
86
+ def self.create(name, opts = { })
87
+ data = { :name => name }
88
+ data.merge! opts
89
+ return self::new(data)
90
+ end
91
+
92
+ ##
93
+ # Checks correctness of the data.
94
+ #
95
+
96
+ def check!
97
+ self.normalize!
98
+
99
+ if not @name.kind_of? Symbol
100
+ raise Exception::new("Parameter name must be Symbol or convertable to Symbol.")
101
+ end
102
+
103
+ if (not @type.nil?) and (not @type.kind_of_any? [GenericTypes::Any, GenericTypes::Nil, GenericTypes::Boolean, Integer, String, Array, Object])
104
+ raise Exception::new("Type if defined can be only Any, Nil, Boolean, Integer, String, Array or Object.")
105
+ end
106
+ end
107
+
108
+ ##
109
+ # Renders data to output hash.
110
+ # @return [Hash] with data of description
111
+ #
112
+
113
+ def output
114
+ self.check!
115
+ data = { :name => @name.to_s }
116
+
117
+ if not @type.nil?
118
+ data[:type] = __object_to_type
119
+ end
120
+
121
+ return data
122
+ end
123
+
124
+
125
+ protected
126
+
127
+ ##
128
+ # Assigns request data.
129
+ #
130
+
131
+ def data=(value, mode = nil)
132
+ data = __convert_data(value, mode)
133
+
134
+ @name = data[:name]
135
+ @type = data[:type]
136
+ end
137
+
138
+ ##
139
+ # Converts request data to standard (defined) format.
140
+ #
141
+
142
+ def normalize!
143
+ if @name.kind_of? String
144
+ @name = @name.to_sym
145
+ end
146
+
147
+ if @type.kind_of_any? [String, Symbol]
148
+ @type = __normalize_type
149
+ end
150
+ end
151
+
152
+ ##
153
+ # Normalizes type definition.
154
+ #
155
+
156
+ def __normalize_type
157
+ __type_to_object
158
+ end
159
+
160
+ ##
161
+ # Converts type definition to appropriate object.
162
+ #
163
+
164
+ def __type_to_object
165
+ self.class::TYPE_TO_OBJECT[@type.to_sym]
166
+ end
167
+
168
+ ##
169
+ # Converts type object to type definition.
170
+ #
171
+
172
+ def __object_to_type
173
+ self.class::OBJECT_TO_TYPE[@type]
174
+ end
175
+
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,155 @@
1
+ # encoding: utf-8
2
+ require "json-rpc-objects/v10/response"
3
+ require "json-rpc-objects/v11/wd/error"
4
+ require "json-rpc-objects/v11/wd/extensions"
5
+
6
+ ##
7
+ # Main JSON-RPC Objects module.
8
+ #
9
+
10
+ module JsonRpcObjects
11
+
12
+ ##
13
+ # General module of JSON-RPC 1.1.
14
+ #
15
+
16
+ module V11
17
+
18
+ ##
19
+ # Module of Working Draft.
20
+ # @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
21
+ #
22
+
23
+ module WD
24
+
25
+ ##
26
+ # Procedure return (response) class.
27
+ #
28
+
29
+ class ProcedureReturn < JsonRpcObjects::V10::Response
30
+
31
+ include Extensions
32
+
33
+ ##
34
+ # Holds link to its version module.
35
+ #
36
+
37
+ VERSION = JsonRpcObjects::V11::WD
38
+
39
+ ##
40
+ # Holds JSON-RPC version specification.
41
+ #
42
+
43
+ VERSION_NUMBER = :"1.1"
44
+
45
+ ##
46
+ # Holds JSON-RPC version member identification.
47
+ #
48
+
49
+ VERSION_MEMBER = :version
50
+
51
+ ##
52
+ # Identifies the error object class.
53
+ #
54
+
55
+ ERROR_CLASS = JsonRpcObjects::V11::WD::Error
56
+
57
+ ##
58
+ # Checks correctness of the request data.
59
+ #
60
+
61
+ def check!
62
+ self.normalize!
63
+
64
+ __check_coherency
65
+ __check_error
66
+ end
67
+
68
+ ##
69
+ # Renders data to output hash.
70
+ # @return [Hash] with data of return
71
+ #
72
+
73
+ def output
74
+ self.check!
75
+
76
+ data = { }
77
+ __assign_version(data)
78
+
79
+ if not @result.nil?
80
+ data[:result] = @result
81
+ end
82
+
83
+ if not @error.nil?
84
+ data[:error] = @error
85
+ end
86
+
87
+ if not @id.nil?
88
+ data[:id] = @id
89
+ end
90
+
91
+ data.merge! @extensions
92
+ return data
93
+ end
94
+
95
+
96
+ protected
97
+
98
+ ##
99
+ # Assigns request data.
100
+ #
101
+
102
+ def data=(value, mode = nil)
103
+ data = __convert_data(value, mode)
104
+ super(data, :converted)
105
+
106
+ data.delete(:result)
107
+ data.delete(:error)
108
+ data.delete(:id)
109
+
110
+ __delete_version(data)
111
+
112
+ # Extensions
113
+ @extensions = data
114
+ end
115
+
116
+ ##
117
+ # Converts request data to standard (defined) format.
118
+ #
119
+
120
+ def normalize!
121
+ if @extensions.nil?
122
+ @extensions = { }
123
+ end
124
+ end
125
+
126
+ ##
127
+ # Creates error object.
128
+ #
129
+
130
+ def __create_error
131
+ if @error.kind_of? Hash
132
+ @error = self.class::ERROR_CLASS::new(@error)
133
+ end
134
+ end
135
+
136
+ ##
137
+ # Assignes the version specification.
138
+ #
139
+
140
+ def __assign_version(data)
141
+ data[self.class::VERSION_MEMBER] = self.class::VERSION_NUMBER
142
+ end
143
+
144
+ ##
145
+ # Removes the version specification.
146
+ #
147
+
148
+ def __delete_version(data)
149
+ data.delete(self.class::VERSION_MEMBER)
150
+ end
151
+
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "json-rpc-objects/v11/wd/fakes/request"
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "json-rpc-objects/v11/wd/fakes/response"
@@ -0,0 +1,251 @@
1
+ # encoding: utf-8
2
+ require "version"
3
+ require "addressable/uri"
4
+ require "json-rpc-objects/v11/wd/service-procedure-description"
5
+ require "json-rpc-objects/generic"
6
+ require "hash-utils/array"
7
+
8
+ ##
9
+ # Main JSON-RPC Objects module.
10
+ #
11
+
12
+ module JsonRpcObjects
13
+
14
+ ##
15
+ # General module of JSON-RPC 1.1.
16
+ #
17
+
18
+ module V11
19
+
20
+ ##
21
+ # Module of Working Draft.
22
+ # @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
23
+ #
24
+
25
+ module WD
26
+
27
+ ##
28
+ # Service description object class.
29
+ #
30
+
31
+ class ServiceDescription < JsonRpcObjects::Generic::Object
32
+
33
+ ##
34
+ # Holds link to its version module.
35
+ #
36
+
37
+ VERSION = JsonRpcObjects::V11::WD
38
+
39
+ ##
40
+ # Indicates the service procedure description class.
41
+ #
42
+
43
+ PROCEDURE_DESCRIPTION_CLASS = JsonRpcObjects::V11::WD::ServiceProcedureDescription
44
+
45
+ ##
46
+ # Holds service name.
47
+ #
48
+
49
+ @name
50
+ attr_accessor :name
51
+
52
+ ##
53
+ # Holds service identifier.
54
+ #
55
+
56
+ @id
57
+ attr_accessor :id
58
+
59
+ ##
60
+ # Holds service version.
61
+ #
62
+
63
+ @version
64
+ attr_accessor :version
65
+
66
+ ##
67
+ # Holds service summary.
68
+ #
69
+
70
+ @summary
71
+ attr_accessor :summary
72
+
73
+ ##
74
+ # Holds service help URL.
75
+ #
76
+
77
+ @help
78
+ attr_accessor :url
79
+
80
+ ##
81
+ # Holds service address.
82
+ #
83
+
84
+ @address
85
+ attr_accessor :address
86
+
87
+ ##
88
+ # Holds procedure descriptions.
89
+ #
90
+
91
+ @procs
92
+ attr_accessor :procs
93
+
94
+ ##
95
+ # Creates new one.
96
+ #
97
+ # @param [Symbol] name name of the service
98
+ # @param [String] id ID of the service (according
99
+ # to specification it should be valid URI)
100
+ # @param [Hash] opts additional options
101
+ # @return [V11:ServiceDescription] new description object
102
+ #
103
+
104
+ def self.create(name, id, opts = { })
105
+ data = {
106
+ :name => name,
107
+ :id => id
108
+ }
109
+
110
+ data.merge! opts
111
+ return self::new(data)
112
+ end
113
+
114
+ ##
115
+ # Checks correctness of the data.
116
+ #
117
+
118
+ def check!
119
+ self.normalize!
120
+
121
+ if not @name.kind_of? Symbol
122
+ raise Exception::new("Service name must be Symbol or convertable to Symbol.")
123
+ end
124
+
125
+ if not (@version.nil?) and ((@version.to_a.length < 2) or @version.prerelease?)
126
+ raise Exception::new("Version must be at least in <major>.<minor> format and must contain numbers only.")
127
+ end
128
+
129
+ if (not @procs.nil?)
130
+ if (not @procs.kind_of? Array) or (not @procs.all? { |v| v.kind_of? self.class::PROCEDURE_DESCRIPTION_CLASS })
131
+ raise Exception::new("If procs is defined, must be an Array of " << self.class::PROCEDURE_DESCRIPTION_CLASS.name << " objects.")
132
+ end
133
+
134
+ if @procs.kind_of? Array
135
+ @procs.each { |proc| proc.check! }
136
+ end
137
+ end
138
+ end
139
+
140
+ ##
141
+ # Renders data to output hash.
142
+ # @return [Hash] with data of description
143
+ #
144
+
145
+ def output
146
+ self.check!
147
+
148
+ data = {
149
+ :sdversion => :"1.0",
150
+ :name => @name.to_s,
151
+ :id => @id.to_s
152
+ }
153
+
154
+ if not @version.nil?
155
+ data[:version] = @version.to_s
156
+ end
157
+
158
+ if not @summary.nil?
159
+ data[:summary] = @summary
160
+ end
161
+
162
+ if not @help.nil?
163
+ data[:help] = @help.to_s
164
+ end
165
+
166
+ if not @address.nil?
167
+ data[:address] = @address.to_s
168
+ end
169
+
170
+ if not @procs.nil?
171
+ data[:procs] = @procs
172
+ end
173
+
174
+ return data
175
+ end
176
+
177
+ ##
178
+ # Receives service procedure description objects.
179
+ # @param [ServiceProcedureDescription] new service procedure description
180
+ #
181
+
182
+ def <<(value)
183
+ if not value.kind_of? self.class::PROCEDURE_DESCRIPTION_CLASS
184
+ raise Exception::new(self.class::PROCEDURE_DESCRIPTION_CLASS.name.dup << " object expected.")
185
+ end
186
+
187
+ if @procs.nil?
188
+ @procs = [ ]
189
+ end
190
+
191
+ @procs << value
192
+ end
193
+
194
+
195
+
196
+ protected
197
+
198
+ ##
199
+ # Assigns request data.
200
+ #
201
+
202
+ def data=(value, mode = nil)
203
+ data = __convert_data(value, mode)
204
+
205
+ @name = data[:name]
206
+ @id = data[:id]
207
+ @version = data[:version]
208
+ @summary = data[:summary]
209
+ @help = data[:help]
210
+ @address = data[:address]
211
+ @procs = data[:procs]
212
+
213
+ if @procs.kind_of? Array
214
+ @procs = @procs.map { |v| self.class::PROCEDURE_DESCRIPTION_CLASS::new(v) }
215
+ end
216
+ end
217
+
218
+ ##
219
+ # Converts request data to standard (defined) format.
220
+ #
221
+
222
+ def normalize!
223
+ if @name.kind_of? String
224
+ @name = @name.to_sym
225
+ end
226
+
227
+ if not @id.kind_of? Addressable::URI
228
+ @id = Addressable::URI::parse(@id.to_s)
229
+ end
230
+
231
+ if (not @version.nil?) and (not @version.kind_of? Version)
232
+ @version = @version.to_s.to_version
233
+ end
234
+
235
+ if not @summary.nil?
236
+ @summary = @summary.to_s
237
+ end
238
+
239
+ if (not @help.nil?) and (not @help.kind_of? Addressable::URI)
240
+ @help = Addressable::URI::parse(@help.to_s)
241
+ end
242
+
243
+ if (not @address.nil?) and (not @address.kind_of? Addressable::URI)
244
+ @address = Addressable::URI::parse(@address.to_s)
245
+ end
246
+ end
247
+
248
+ end
249
+ end
250
+ end
251
+ end