json-rpc-objects 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.txt +5 -0
  3. data/Gemfile +3 -1
  4. data/Gemfile.lock +63 -12
  5. data/LICENSE.txt +1 -1
  6. data/README.md +40 -41
  7. data/Rakefile +2 -2
  8. data/VERSION +1 -1
  9. data/json-rpc-objects.gemspec +25 -11
  10. data/lib/json-rpc-objects/error.rb +1 -1
  11. data/lib/json-rpc-objects/exceptions/invalid-code.rb +25 -0
  12. data/lib/json-rpc-objects/generic.rb +2 -2
  13. data/lib/json-rpc-objects/generic/error.rb +1 -1
  14. data/lib/json-rpc-objects/generic/object.rb +4 -5
  15. data/lib/json-rpc-objects/generic/request.rb +1 -1
  16. data/lib/json-rpc-objects/generic/response.rb +1 -1
  17. data/lib/json-rpc-objects/request.rb +4 -5
  18. data/lib/json-rpc-objects/response.rb +3 -4
  19. data/lib/json-rpc-objects/serializer.rb +1 -2
  20. data/lib/json-rpc-objects/serializer/marshal.rb +1 -1
  21. data/lib/json-rpc-objects/serializer/none.rb +1 -1
  22. data/lib/json-rpc-objects/utils.rb +6 -0
  23. data/lib/json-rpc-objects/utils/hash.rb +198 -0
  24. data/lib/json-rpc-objects/utils/object.rb +85 -0
  25. data/lib/json-rpc-objects/utils/string.rb +46 -0
  26. data/lib/json-rpc-objects/v10/error.rb +1 -1
  27. data/lib/json-rpc-objects/v10/request.rb +4 -5
  28. data/lib/json-rpc-objects/v10/response.rb +1 -1
  29. data/lib/json-rpc-objects/v10/tests/test.rb +1 -1
  30. data/lib/json-rpc-objects/v11/alt/error.rb +1 -1
  31. data/lib/json-rpc-objects/v11/alt/fakes/request.rb +1 -2
  32. data/lib/json-rpc-objects/v11/alt/fakes/response.rb +1 -2
  33. data/lib/json-rpc-objects/v11/alt/procedure-call.rb +4 -4
  34. data/lib/json-rpc-objects/v11/alt/procedure-parameter-description.rb +1 -1
  35. data/lib/json-rpc-objects/v11/alt/procedure-return.rb +2 -1
  36. data/lib/json-rpc-objects/v11/alt/request.rb +1 -1
  37. data/lib/json-rpc-objects/v11/alt/response.rb +1 -1
  38. data/lib/json-rpc-objects/v11/alt/service-description.rb +1 -1
  39. data/lib/json-rpc-objects/v11/alt/service-procedure-description.rb +2 -4
  40. data/lib/json-rpc-objects/v11/alt/tests/test.rb +1 -1
  41. data/lib/json-rpc-objects/v11/generic-types.rb +1 -1
  42. data/lib/json-rpc-objects/v11/wd/error.rb +5 -6
  43. data/lib/json-rpc-objects/v11/wd/extensions.rb +1 -1
  44. data/lib/json-rpc-objects/v11/wd/fakes/request.rb +1 -2
  45. data/lib/json-rpc-objects/v11/wd/fakes/response.rb +1 -2
  46. data/lib/json-rpc-objects/v11/wd/procedure-call.rb +13 -15
  47. data/lib/json-rpc-objects/v11/wd/procedure-parameter-description.rb +8 -8
  48. data/lib/json-rpc-objects/v11/wd/procedure-return.rb +4 -5
  49. data/lib/json-rpc-objects/v11/wd/request.rb +1 -1
  50. data/lib/json-rpc-objects/v11/wd/response.rb +1 -1
  51. data/lib/json-rpc-objects/v11/wd/service-description.rb +6 -8
  52. data/lib/json-rpc-objects/v11/wd/service-procedure-description.rb +8 -10
  53. data/lib/json-rpc-objects/v11/wd/tests/test.rb +1 -1
  54. data/lib/json-rpc-objects/v20/error.rb +6 -4
  55. data/lib/json-rpc-objects/v20/request.rb +6 -6
  56. data/lib/json-rpc-objects/v20/response.rb +2 -2
  57. data/lib/json-rpc-objects/v20/tests/test.rb +2 -2
  58. data/lib/json-rpc-objects/version.rb +18 -6
  59. data/spec/request_spec.rb +11 -0
  60. data/spec/spec_helper.rb +18 -0
  61. metadata +69 -51
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/generic/object"
5
5
 
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/generic/object"
5
5
 
@@ -1,9 +1,8 @@
1
1
  # encoding: utf-8
2
- # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/v20/request"
5
5
  require "json-rpc-objects/serializer"
6
- require "hash-utils/object"
7
6
 
8
7
  ##
9
8
  # Main JSON-RPC Objects module.
@@ -51,8 +50,8 @@ module JsonRpcObjects
51
50
 
52
51
  def self.parse(string, default_v11 = :wd, serializer = JsonRpcObjects::default_serializer)
53
52
  data = serializer.deserialize(string)
54
-
55
- if not data.hash?
53
+ p data
54
+ if not data.kind_of? Hash
56
55
  raise Exception::new("Data in JSON string aren't object.")
57
56
  end
58
57
 
@@ -76,7 +75,7 @@ module JsonRpcObjects
76
75
  end
77
76
 
78
77
  # Returns
79
- if not file.in? @@files
78
+ if not @@files.include?(file)
80
79
  require file
81
80
  @@files[file] = true
82
81
  end
@@ -1,9 +1,8 @@
1
1
  # encoding: utf-8
2
- # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/v20/response"
5
5
  require "json-rpc-objects/serializer"
6
- require "hash-utils/object"
7
6
 
8
7
  ##
9
8
  # Main JSON-RPC Objects module.
@@ -51,7 +50,7 @@ module JsonRpcObjects
51
50
  def self.parse(string, default_v11 = :wd, serializer = JsonRpcObjects::default_serializer)
52
51
  data = serializer.deserialize(string)
53
52
 
54
- if not data.hash?
53
+ if not data.kind_of? Hash
55
54
  raise Exception::new("Data in JSON string aren't object.")
56
55
  end
57
56
 
@@ -75,7 +74,7 @@ module JsonRpcObjects
75
74
  end
76
75
 
77
76
  # Returns
78
- if not file.in? @@files
77
+ if not @@files.include?(file)
79
78
  require file
80
79
  @@files[file] = true
81
80
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/serializer/json"
5
5
  require "abstract"
@@ -104,4 +104,3 @@ module JsonRpcObjects
104
104
 
105
105
  end
106
106
  end
107
-
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/serializer"
5
5
 
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- # (c) 2012 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/serializer"
5
5
 
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
+
4
+ require 'json-rpc-objects/utils/hash'
5
+ require 'json-rpc-objects/utils/object'
6
+ require 'json-rpc-objects/utils/string'
@@ -0,0 +1,198 @@
1
+ # encoding: utf-8
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
+
4
+ require 'ruby-version'
5
+
6
+ ##
7
+ # Main JSON-RPC Objects module.
8
+ #
9
+
10
+ module JsonRpcObjects
11
+
12
+ ##
13
+ # Module for utility functions. Large part ported from 'hash-utils'.
14
+ # @since 0.4.4
15
+ #
16
+
17
+ module Utils
18
+
19
+ ##
20
+ # Hash utility functions.
21
+ #
22
+
23
+ class Hash
24
+
25
+ ##
26
+ # Returns a new hash with the results of running block once for
27
+ # every key in +hash+.
28
+ #
29
+ # @param [Hash] hash source hash
30
+ # @param [Proc] block evaluating block
31
+ # @return [Hash] new hash
32
+ #
33
+
34
+ def self.map_keys(hash, &block)
35
+ self.map_pairs(hash) do |k, v|
36
+ [block.call(k), v]
37
+ end
38
+ end
39
+
40
+ ##
41
+ # Emulates {#map_keys} on place. In fact, replaces old hash by
42
+ # new one.
43
+ #
44
+ # @param [Hash] hash source hash
45
+ # @param [Proc] block evaluating block
46
+ # @return [Hash] new hash
47
+ #
48
+
49
+ def self.map_keys!(hash, &block)
50
+ hash.replace(self.map_keys(hash, &block))
51
+ end
52
+
53
+ ##
54
+ # Returns a new hash with the results of running block once for
55
+ # every pair in +hash+.
56
+ #
57
+ # @param [Hash] hash source hash
58
+ # @param [Proc] block evaluating block
59
+ # @return [Hash] new hash
60
+ #
61
+
62
+ def self.map_pairs(hash, &block)
63
+ _new = self.recreate(hash)
64
+
65
+ hash.each_pair do |k, v|
66
+ new_k, new_v = block.call(k, v)
67
+ _new[new_k] = new_v
68
+ end
69
+
70
+ return _new
71
+ end
72
+
73
+ ##
74
+ # Recreates the hash, so creates empty one and assigns
75
+ # the same default values.
76
+ #
77
+ # @param [Hash] hash source hash
78
+ # @return [Hash] new hash
79
+ #
80
+
81
+ def self.recreate(hash)
82
+ self.create(hash.default, &hash.default_proc)
83
+ end
84
+
85
+ ##
86
+ # Creates hash by setting default settings in one call.
87
+ #
88
+ # @param [Object] default default value
89
+ # @param [Hash] hash initial values
90
+ # @param [Proc] block default block
91
+ # @return [Hash] new hash
92
+ #
93
+
94
+ def self.create(default = nil, hash = nil, &block)
95
+ if Ruby::Version >= "1.9"
96
+ if not hash.nil?
97
+ hash = hash.dup
98
+ else
99
+ hash = { }
100
+ end
101
+
102
+ hash.default = default
103
+ hash.default_proc = block if not block.nil?
104
+ return hash
105
+ else
106
+ if not hash.nil?
107
+ hash = hash.dup
108
+ else
109
+ hash = { }
110
+ end
111
+
112
+ if not block.nil?
113
+ _new = ::Hash::new(&block)
114
+ _new.update(hash) if not hash.empty?
115
+ hash = _new
116
+ else
117
+ hash.default = default
118
+ end
119
+
120
+ return hash
121
+ end
122
+ end
123
+
124
+ ##
125
+ # Converts all +String+ keys to symbols.
126
+ #
127
+ # @param [Hash] hash source hash
128
+ # @return [Hash] new hash
129
+ #
130
+
131
+ def self.keys_to_sym(hash)
132
+ self.map_keys(hash) do |k|
133
+ if k.kind_of? ::String
134
+ k.to_sym
135
+ else
136
+ k
137
+ end
138
+ end
139
+ end
140
+
141
+ ##
142
+ # Emulates {#keys_to_sym} on place. In fact, replaces old hash by
143
+ # new one.
144
+ #
145
+ # @param [Hash] hash source hash
146
+ # @return [Hash] new hash
147
+ #
148
+
149
+ def self.keys_to_sym!(hash)
150
+ hash.replace(self.keys_to_sym(hash))
151
+ end
152
+
153
+ ##
154
+ # Moves selected pairs outside the hash, so returns them.
155
+ # Output hash has the same default settings.
156
+ #
157
+ # @param [Hash] hash source hash
158
+ # @param [Proc] block selecting block
159
+ # @return [Hash] removed selected pairs
160
+ #
161
+
162
+ def self.remove!(hash, &block)
163
+ result = self.recreate(hash)
164
+ delete = [ ]
165
+
166
+ hash.each_pair do |k, v|
167
+ if block.call(k, v)
168
+ result[k] = v
169
+ delete << k
170
+ end
171
+ end
172
+
173
+ delete.each do |k|
174
+ hash.delete(k)
175
+ end
176
+
177
+ return result
178
+ end
179
+
180
+ ##
181
+ # Defines hash by setting the default value or an +Proc+
182
+ # and content.
183
+ #
184
+ # @param [Hash] values initial values
185
+ # @param [Object] default default value
186
+ # @param [Proc] block default block
187
+ # @return [Hash] new hash
188
+ #
189
+
190
+ def self.define(values = { }, default = nil, &block)
191
+ hash = ::Hash[values]
192
+ self.create(default, hash, &block)
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+ end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
+
4
+ ##
5
+ # Main JSON-RPC Objects module.
6
+ #
7
+
8
+ module JsonRpcObjects
9
+
10
+ ##
11
+ # Module for utility functions. Large part ported from 'hash-utils'.
12
+ # @since 0.4.4
13
+ #
14
+
15
+ module Utils
16
+
17
+ ##
18
+ # Object utility functions.
19
+ #
20
+
21
+ class Object
22
+
23
+ ##
24
+ # Returns +true+ if object is an instance of the given classes.
25
+ #
26
+ # @see #kind_of_any?
27
+ # @param [Object] object source object
28
+ # @param [Array] classes array of class objects
29
+ # @return [Boolean] +true+ if it is, +false+ in otherwise
30
+ #
31
+
32
+ def self.instance_of_any?(object, *classes)
33
+ if classes.first.kind_of? ::Array
34
+ classes = classes.first
35
+ end
36
+
37
+ classes.each do |cls|
38
+ if object.instance_of? cls
39
+ return true
40
+ end
41
+ end
42
+
43
+ return false
44
+ end
45
+
46
+ ##
47
+ # Returns +true+ if one of classes are the class of object,
48
+ # or if one of classes are one of the superclasses of object or
49
+ # modules included in object.
50
+ #
51
+ # @param [Object] object source object
52
+ # @param [Array] classes array of class objects
53
+ # @return [Boolean] +true+ if it is, +false+ in otherwise
54
+ # @since 0.16.0
55
+ #
56
+
57
+ def self.kind_of_any?(object, *classes)
58
+ if classes.first.kind_of? ::Array
59
+ classes = classes.first
60
+ end
61
+
62
+ classes.each do |cls|
63
+ if object.kind_of? cls
64
+ return true
65
+ end
66
+ end
67
+
68
+ return false
69
+ end
70
+
71
+ ##
72
+ # Indicates, object is boolean.
73
+ #
74
+ # @param [Objectt] object source object
75
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
76
+ #
77
+
78
+ def self.boolean?(object)
79
+ self.instance_of_any? object, [TrueClass, FalseClass]
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
+
4
+ ##
5
+ # Main JSON-RPC Objects module.
6
+ #
7
+
8
+ module JsonRpcObjects
9
+
10
+ ##
11
+ # Module for utility functions. Large part ported from 'hash-utils'.
12
+ # @since 0.4.4
13
+ #
14
+
15
+ module Utils
16
+
17
+ ##
18
+ # String utility functions.
19
+ #
20
+
21
+ class String
22
+
23
+ ##
24
+ # Holds numeric matcher.
25
+ #
26
+
27
+ NUMERIC = /^\s*-?\d+(?:\.\d+)?\s*$/
28
+
29
+ ##
30
+ # Indicates, string is numeric, so consists of numbers only.
31
+ #
32
+ # @param [Source] string source string
33
+ # @return [Boolean] +true+ if yes, +false+ in otherwise
34
+ #
35
+
36
+ def self.numeric?(string)
37
+ if string.match(NUMERIC)
38
+ true
39
+ else
40
+ false
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+ # (c) 2011-2015 Martin Poljak (martin@poljak.cz)
3
3
 
4
4
  require "json-rpc-objects/generic/error"
5
5