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,52 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # Main JSON-RPC Objects module.
5
+ #
6
+
7
+ module JsonRpcObjects
8
+
9
+ ##
10
+ # General module of JSON-RPC 1.1.
11
+ #
12
+
13
+ module V11
14
+
15
+ ##
16
+ # Module for special generic types as they are defined
17
+ # in JSON-RPC 1.1.
18
+ #
19
+
20
+ module GenericTypes
21
+
22
+ ##
23
+ # Class which means "any type of JSON data", so
24
+ # "Boolean, Number, String, Array or Object".
25
+ #
26
+ # @abstract
27
+ #
28
+
29
+ class Any
30
+ end
31
+
32
+ ##
33
+ # Class which means classical nil. Nil in code means
34
+ # type isn't defined, so this class is necessary.
35
+ #
36
+ # @abstract
37
+ #
38
+
39
+ class Nil
40
+ end
41
+
42
+ ##
43
+ # Class which means Boolean, so true or false.
44
+ # @abstract
45
+ #
46
+
47
+ class Boolean
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,172 @@
1
+ # encoding: utf-8
2
+ require "hash-utils/numeric"
3
+ require "json-rpc-objects/generic"
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
+ # Error description object class for ProcedureReturn.
27
+ #
28
+
29
+ class Error < JsonRpcObjects::Generic::Object
30
+
31
+ include Extensions
32
+
33
+ ##
34
+ # Holds link to its version module.
35
+ #
36
+
37
+ VERSION = JsonRpcObjects::V11::WD
38
+
39
+ ##
40
+ # Indicates data member name.
41
+ #
42
+
43
+ DATA_MEMBER_NAME = :error
44
+
45
+ ##
46
+ # Holds error code.
47
+ #
48
+
49
+ @code
50
+ attr_accessor :code
51
+
52
+ ##
53
+ # Holds error message.
54
+ #
55
+
56
+ @message
57
+ attr_accessor :message
58
+
59
+ ##
60
+ # Holds error data.
61
+ #
62
+
63
+ @data
64
+ attr_accessor :data
65
+
66
+ ##
67
+ # Creates new one.
68
+ #
69
+ # @param [Numeric] code od the error
70
+ # @param [String, Exception] message of the error or
71
+ # exception object
72
+ # @param [Hash] opts additional options
73
+ # @return [V11::Error] new error object
74
+ #
75
+
76
+ def self.create(code, message, opts = { })
77
+ data = {
78
+ :code => code,
79
+ }
80
+
81
+ if message.kind_of? Exception
82
+ data[:message] = message.message
83
+ data[self::DATA_MEMBER_NAME] = message.backtrace
84
+ else
85
+ data[:message] = message
86
+ end
87
+
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 @code.in? 100..999
100
+ raise Exception::new("Code must be between 100 and 999 including them.")
101
+ end
102
+ end
103
+
104
+ ##
105
+ # Renders data to output hash.
106
+ # @return [Hash] with data of error
107
+ #
108
+
109
+ def output
110
+ self.check!
111
+ data = {
112
+ :name => :JSONRPCError,
113
+ :code => @code,
114
+ :message => @message
115
+ }
116
+
117
+ if not @error.nil?
118
+ data[:error] = @data
119
+ end
120
+
121
+ data.merge! @extensions
122
+ return data
123
+ end
124
+
125
+
126
+ protected
127
+
128
+ ##
129
+ # Assigns data.
130
+ #
131
+
132
+ def data=(value, mode = nil)
133
+ data = __convert_data(value, mode)
134
+
135
+ @code = data[:code]
136
+ @message = data[:message]
137
+
138
+ data.delete(:code)
139
+ data.delete(:message)
140
+
141
+ __assign_data(data)
142
+
143
+ # Extensions
144
+ @extensions = data
145
+ end
146
+
147
+ ##
148
+ # Converts request data to standard (defined) format.
149
+ #
150
+
151
+ def normalize!
152
+ @message = @message.to_s
153
+ @code = @code.to_i
154
+
155
+ if @extensions.nil?
156
+ @extensions = { }
157
+ end
158
+ end
159
+
160
+ ##
161
+ # Assigns error data.
162
+ #
163
+
164
+ def __assign_data(data)
165
+ @data = data[:error]
166
+ data.delete(:error)
167
+ end
168
+
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # Main JSON-RPC Objects module.
5
+ #
6
+
7
+ module JsonRpcObjects
8
+
9
+ ##
10
+ # General module of JSON-RPC 1.1.
11
+ #
12
+
13
+ module V11
14
+
15
+ ##
16
+ # Module of Working Draft.
17
+ # @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
18
+ #
19
+
20
+ module WD
21
+
22
+ ##
23
+ # Module for extension support in JSON-RPC 1.1 WD.
24
+ #
25
+
26
+ module Extensions
27
+
28
+ ##
29
+ # Holds extensions.
30
+ #
31
+
32
+ @extensions
33
+ attr_accessor :extensions
34
+
35
+ ##
36
+ # Handles method missing call for extensions.
37
+ #
38
+ # @param [Symbol] name of the method, setter if ends with '='
39
+ # @param [Object] value for set
40
+ # @return [Object] value set or get
41
+ #
42
+
43
+ def method_missing(name, *args)
44
+ if name.to_s[-1].chr == ?=
45
+ self[name.to_s[0..-2]] = args.first
46
+ else
47
+ self[name]
48
+ end
49
+ end
50
+
51
+ ##
52
+ # Handles array access as access for extensions too.
53
+ #
54
+ # @param [String] name of extension for return
55
+ # @return [Object] value of extension member
56
+ #
57
+
58
+ def [](name)
59
+ @extensions[name.to_sym]
60
+ end
61
+
62
+ ##
63
+ # Handles array set to extensions.
64
+ #
65
+ # @param [String] name of extension for set
66
+ # @param[Object] value of extension for set
67
+ #
68
+
69
+ def []=(name, value)
70
+ @extensions[name.to_sym] = value
71
+ end
72
+
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ require "json-rpc-objects/v11/wd/procedure-call"
3
+
4
+ ##
5
+ # Main JSON-RPC Objects module.
6
+ #
7
+
8
+ module JsonRpcObjects
9
+
10
+ ##
11
+ # General module of JSON-RPC 1.1.
12
+ #
13
+
14
+ module V11
15
+
16
+ ##
17
+ # Module of Working Draft.
18
+ # @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
19
+ #
20
+
21
+ module WD
22
+
23
+ ##
24
+ # Fake procedure call (request) class.
25
+ #
26
+
27
+ class Request < JsonRpcObjects::V11::WD::ProcedureCall
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ require "json-rpc-objects/v11/wd/procedure-return"
3
+
4
+ ##
5
+ # Main JSON-RPC Objects module.
6
+ #
7
+
8
+ module JsonRpcObjects
9
+
10
+ ##
11
+ # General module of JSON-RPC 1.1.
12
+ #
13
+
14
+ module V11
15
+
16
+ ##
17
+ # Module of Working Draft.
18
+ # @see http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
19
+ #
20
+
21
+ module WD
22
+
23
+ ##
24
+ # Fake procedure return (response) class.
25
+ #
26
+
27
+ class Response < JsonRpcObjects::V11::WD::ProcedureReturn
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,205 @@
1
+ # encoding: utf-8
2
+ require "hash-utils/hash"
3
+ require "json-rpc-objects/v10/request"
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 call (request) class.
27
+ #
28
+
29
+ class ProcedureCall < JsonRpcObjects::V10::Request
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
+ # Holds keyword parameters.
53
+ #
54
+
55
+ @keyword_parameters
56
+ attr_accessor :keyword_parameters
57
+
58
+ ##
59
+ # Checks correctness of the request data.
60
+ #
61
+
62
+ def check!
63
+ super()
64
+
65
+ if not @keyword_params.nil? and not @keyword_params.kind_of? Hash
66
+ raise Exception::new("Keyword params must be Hash.")
67
+ end
68
+ end
69
+
70
+
71
+ ##
72
+ # Renders data to output hash.
73
+ # @return [Hash] with data of call
74
+ #
75
+
76
+ def output
77
+ self.check!
78
+
79
+ data = { }
80
+
81
+ # Version
82
+ __assign_version(data)
83
+
84
+ # Method
85
+ data[:method] = @method.to_s
86
+
87
+ # Params
88
+ __assign_params(data)
89
+
90
+ # ID
91
+ if not @id.nil?
92
+ data[:id] = @id
93
+ end
94
+
95
+ data.merge! @extensions
96
+ return data
97
+ end
98
+
99
+
100
+ protected
101
+
102
+ ##
103
+ # Assigns request data.
104
+ #
105
+
106
+ def data=(value, mode = nil)
107
+ data = __convert_data(value, mode)
108
+ super(data, :converted)
109
+
110
+ # Params
111
+ __get_params(data)
112
+
113
+ data.delete(:method)
114
+ data.delete(:params)
115
+ data.delete(:id)
116
+
117
+ __delete_version(data)
118
+
119
+ # Extensions
120
+ @extensions = data
121
+ end
122
+
123
+ ##
124
+ # Converts request data to standard (defined) format.
125
+ #
126
+
127
+ def normalize!
128
+ __normalize_method
129
+
130
+ if @extensions.nil?
131
+ @extensions = { }
132
+ end
133
+ end
134
+
135
+ ##
136
+ # Gets params from input.
137
+ #
138
+
139
+ def __get_params(data)
140
+
141
+ # If named arguments used, assigns keys as symbols
142
+ # but keeps numeric arguments as integers
143
+
144
+ if @params.kind_of? Hash
145
+ @params = @params.dup
146
+ @keyword_params = @params.remove! { |k, v| not k.numeric? }
147
+ @params = @params.sort_by { |i| i[0].to_i }.map { |i| i[1] }
148
+ else
149
+ @keyword_params = { }
150
+ end
151
+
152
+ @keyword_params.map_keys! { |k| k.to_sym }
153
+
154
+ end
155
+
156
+ ##
157
+ # Assigns the parameters settings.
158
+ #
159
+
160
+ def __assign_params(data)
161
+ params = { }
162
+
163
+ if not @params.nil?
164
+ @params.each_index do |i|
165
+ params[i.to_s.to_sym] = @params[i]
166
+ end
167
+ end
168
+ if not @keyword_params.nil?
169
+ params.merge! @keyword_params
170
+ end
171
+
172
+ data[:params] = params
173
+ end
174
+
175
+ ##
176
+ # Checks params data.
177
+ #
178
+
179
+ def __check_params
180
+ if not @params.nil? and not @params.kind_of? Array
181
+ raise Exception::new("Params must be Array.")
182
+ end
183
+ end
184
+
185
+ ##
186
+ # Assignes the version specification.
187
+ #
188
+
189
+ def __assign_version(data)
190
+ data[self.class::VERSION_MEMBER] = self.class::VERSION_NUMBER
191
+ end
192
+
193
+ ##
194
+ # Removes the version specification.
195
+ #
196
+
197
+ def __delete_version(data)
198
+ data.delete(self.class::VERSION_MEMBER)
199
+ end
200
+
201
+ end
202
+ end
203
+ end
204
+ end
205
+