aws-sdk 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. data/ca-bundle.crt +2 -348
  2. data/lib/aws.rb +24 -49
  3. data/lib/aws/{common.rb → core.rb} +120 -9
  4. data/lib/aws/{api_config.rb → core/api_config.rb} +19 -20
  5. data/lib/aws/core/api_config_transform.rb +36 -0
  6. data/lib/aws/core/async_handle.rb +91 -0
  7. data/lib/aws/core/authorize_v2.rb +39 -0
  8. data/lib/aws/{authorize_v3.rb → core/authorize_v3.rb} +16 -15
  9. data/lib/aws/{api_config_transform.rb → core/authorize_with_session_token.rb} +9 -12
  10. data/lib/aws/core/autoloader.rb +64 -0
  11. data/lib/aws/core/cacheable.rb +78 -0
  12. data/lib/aws/core/client.rb +471 -0
  13. data/lib/aws/core/client_logging.rb +125 -0
  14. data/lib/aws/core/collections.rb +229 -0
  15. data/lib/aws/core/configuration.rb +358 -0
  16. data/lib/aws/core/configured_client_methods.rb +76 -0
  17. data/lib/aws/core/configured_grammars.rb +63 -0
  18. data/lib/aws/{configured_option_grammars.rb → core/configured_option_grammars.rb} +18 -20
  19. data/lib/aws/{configured_xml_grammars.rb → core/configured_xml_grammars.rb} +19 -21
  20. data/lib/aws/core/default_signer.rb +67 -0
  21. data/lib/aws/core/http/curb_handler.rb +129 -0
  22. data/lib/aws/core/http/handler.rb +77 -0
  23. data/lib/aws/core/http/httparty_handler.rb +113 -0
  24. data/lib/aws/core/http/net_http_handler.rb +124 -0
  25. data/lib/aws/core/http/request.rb +207 -0
  26. data/lib/aws/core/http/response.rb +73 -0
  27. data/lib/aws/{ignore_result_element.rb → core/ignore_result_element.rb} +14 -18
  28. data/lib/aws/core/indifferent_hash.rb +88 -0
  29. data/lib/aws/core/inflection.rb +47 -0
  30. data/lib/aws/core/lazy_error_classes.rb +62 -0
  31. data/lib/aws/{uri_escape.rb → core/meta_utils.rb} +24 -23
  32. data/lib/aws/core/model.rb +57 -0
  33. data/lib/aws/{authorize_with_session_token.rb → core/naming.rb} +9 -8
  34. data/lib/aws/core/option_grammar.rb +562 -0
  35. data/lib/aws/core/policy.rb +914 -0
  36. data/lib/aws/core/resource.rb +380 -0
  37. data/lib/aws/core/resource_cache.rb +40 -0
  38. data/lib/aws/core/response.rb +125 -0
  39. data/lib/aws/core/response_cache.rb +50 -0
  40. data/lib/aws/core/service_interface.rb +60 -0
  41. data/lib/aws/core/uri_escape.rb +46 -0
  42. data/lib/aws/core/xml_grammar.rb +926 -0
  43. data/lib/aws/ec2.rb +47 -16
  44. data/lib/aws/ec2/attachment.rb +0 -2
  45. data/lib/aws/ec2/attachment_collection.rb +1 -6
  46. data/lib/aws/ec2/availability_zone.rb +0 -2
  47. data/lib/aws/ec2/availability_zone_collection.rb +0 -3
  48. data/lib/aws/ec2/client.rb +6 -9
  49. data/lib/aws/ec2/client/xml.rb +3 -6
  50. data/lib/aws/ec2/collection.rb +1 -4
  51. data/lib/aws/ec2/config.rb +18 -0
  52. data/lib/aws/ec2/elastic_ip.rb +0 -2
  53. data/lib/aws/ec2/elastic_ip_collection.rb +0 -4
  54. data/lib/aws/ec2/errors.rb +1 -4
  55. data/lib/aws/ec2/has_permissions.rb +0 -2
  56. data/lib/aws/ec2/image.rb +0 -5
  57. data/lib/aws/ec2/image_collection.rb +0 -6
  58. data/lib/aws/ec2/instance.rb +23 -9
  59. data/lib/aws/ec2/instance_collection.rb +85 -22
  60. data/lib/aws/ec2/key_pair.rb +0 -2
  61. data/lib/aws/ec2/key_pair_collection.rb +1 -4
  62. data/lib/aws/ec2/permission_collection.rb +2 -5
  63. data/lib/aws/ec2/region.rb +0 -2
  64. data/lib/aws/ec2/region_collection.rb +0 -4
  65. data/lib/aws/ec2/request.rb +4 -9
  66. data/lib/aws/ec2/reserved_instances.rb +0 -3
  67. data/lib/aws/ec2/reserved_instances_collection.rb +0 -4
  68. data/lib/aws/ec2/reserved_instances_offering.rb +0 -3
  69. data/lib/aws/ec2/reserved_instances_offering_collection.rb +0 -4
  70. data/lib/aws/ec2/resource.rb +3 -5
  71. data/lib/aws/ec2/resource_tag_collection.rb +1 -6
  72. data/lib/aws/ec2/security_group.rb +204 -72
  73. data/lib/aws/ec2/security_group/egress_ip_permission_collection.rb +53 -0
  74. data/lib/aws/ec2/security_group/ip_permission.rb +92 -10
  75. data/lib/aws/ec2/security_group/ip_permission_collection.rb +14 -15
  76. data/lib/aws/ec2/security_group_collection.rb +11 -8
  77. data/lib/aws/ec2/snapshot.rb +0 -4
  78. data/lib/aws/ec2/snapshot_collection.rb +0 -4
  79. data/lib/aws/ec2/tag.rb +0 -3
  80. data/lib/aws/ec2/tag_collection.rb +1 -8
  81. data/lib/aws/ec2/tagged_item.rb +1 -3
  82. data/lib/aws/ec2/volume.rb +0 -6
  83. data/lib/aws/ec2/volume_collection.rb +0 -4
  84. data/lib/aws/errors.rb +0 -3
  85. data/lib/aws/iam.rb +33 -12
  86. data/lib/aws/iam/access_key.rb +0 -3
  87. data/lib/aws/iam/access_key_collection.rb +0 -3
  88. data/lib/aws/iam/account_alias_collection.rb +0 -2
  89. data/lib/aws/iam/client.rb +6 -8
  90. data/lib/aws/iam/client/xml.rb +4 -8
  91. data/lib/aws/iam/collection.rb +2 -5
  92. data/lib/aws/iam/config.rb +18 -0
  93. data/lib/aws/iam/errors.rb +2 -4
  94. data/lib/aws/iam/group.rb +0 -5
  95. data/lib/aws/iam/group_collection.rb +0 -3
  96. data/lib/aws/iam/group_policy_collection.rb +0 -2
  97. data/lib/aws/iam/group_user_collection.rb +1 -4
  98. data/lib/aws/iam/login_profile.rb +0 -2
  99. data/lib/aws/iam/mfa_device.rb +1 -3
  100. data/lib/aws/iam/mfa_device_collection.rb +0 -3
  101. data/lib/aws/iam/policy.rb +2 -4
  102. data/lib/aws/iam/policy_collection.rb +0 -3
  103. data/lib/aws/iam/request.rb +3 -7
  104. data/lib/aws/iam/resource.rb +1 -4
  105. data/lib/aws/iam/server_certificate.rb +0 -2
  106. data/lib/aws/iam/server_certificate_collection.rb +0 -3
  107. data/lib/aws/iam/signing_certificate.rb +0 -2
  108. data/lib/aws/iam/signing_certificate_collection.rb +0 -3
  109. data/lib/aws/iam/user.rb +0 -7
  110. data/lib/aws/iam/user_collection.rb +0 -3
  111. data/lib/aws/iam/user_group_collection.rb +0 -3
  112. data/lib/aws/iam/user_policy.rb +0 -2
  113. data/lib/aws/iam/user_policy_collection.rb +0 -3
  114. data/lib/aws/rails.rb +5 -4
  115. data/lib/aws/record.rb +4 -1
  116. data/lib/aws/record/base.rb +3 -2
  117. data/lib/aws/record/errors.rb +1 -3
  118. data/lib/aws/s3.rb +31 -6
  119. data/lib/aws/s3/access_control_list.rb +0 -2
  120. data/lib/aws/s3/acl_object.rb +10 -12
  121. data/lib/aws/s3/bucket.rb +1 -9
  122. data/lib/aws/s3/bucket_collection.rb +2 -5
  123. data/lib/aws/s3/bucket_version_collection.rb +1 -5
  124. data/lib/aws/s3/client.rb +34 -37
  125. data/lib/aws/s3/client/xml.rb +11 -14
  126. data/lib/aws/{resource_cache.rb → s3/config.rb} +6 -20
  127. data/lib/aws/s3/errors.rb +6 -7
  128. data/lib/aws/s3/multipart_upload.rb +1 -4
  129. data/lib/aws/s3/multipart_upload_collection.rb +1 -6
  130. data/lib/aws/s3/object_collection.rb +1 -5
  131. data/lib/aws/s3/object_metadata.rb +1 -3
  132. data/lib/aws/s3/object_upload_collection.rb +1 -7
  133. data/lib/aws/s3/object_version.rb +1 -4
  134. data/lib/aws/s3/object_version_collection.rb +1 -4
  135. data/lib/aws/s3/policy.rb +3 -5
  136. data/lib/aws/s3/prefix_and_delimiter_collection.rb +0 -2
  137. data/lib/aws/s3/prefixed_collection.rb +0 -2
  138. data/lib/aws/s3/presigned_post.rb +1 -3
  139. data/lib/aws/s3/request.rb +2 -5
  140. data/lib/aws/s3/s3_object.rb +1 -8
  141. data/lib/aws/s3/tree.rb +8 -6
  142. data/lib/aws/s3/tree/branch_node.rb +0 -3
  143. data/lib/aws/s3/tree/child_collection.rb +1 -5
  144. data/lib/aws/s3/tree/leaf_node.rb +0 -5
  145. data/lib/aws/s3/tree/parent.rb +1 -4
  146. data/lib/aws/s3/uploaded_part.rb +1 -3
  147. data/lib/aws/s3/uploaded_part_collection.rb +1 -5
  148. data/lib/aws/simple_db.rb +21 -6
  149. data/lib/aws/simple_db/attribute.rb +1 -6
  150. data/lib/aws/simple_db/attribute_collection.rb +1 -7
  151. data/lib/aws/simple_db/client.rb +7 -9
  152. data/lib/aws/simple_db/client/options.rb +2 -4
  153. data/lib/aws/simple_db/client/xml.rb +5 -10
  154. data/lib/aws/{naming.rb → simple_db/config.rb} +3 -14
  155. data/lib/aws/simple_db/delete_attributes.rb +0 -2
  156. data/lib/aws/simple_db/domain.rb +1 -6
  157. data/lib/aws/simple_db/domain_collection.rb +1 -4
  158. data/lib/aws/simple_db/domain_metadata.rb +1 -3
  159. data/lib/aws/simple_db/errors.rb +1 -4
  160. data/lib/aws/simple_db/item.rb +1 -7
  161. data/lib/aws/simple_db/item_collection.rb +22 -23
  162. data/lib/aws/simple_db/put_attributes.rb +0 -2
  163. data/lib/aws/simple_db/request.rb +3 -7
  164. data/lib/aws/simple_email_service.rb +11 -6
  165. data/lib/aws/simple_email_service/client.rb +7 -9
  166. data/lib/aws/simple_email_service/client/options.rb +3 -6
  167. data/lib/aws/simple_email_service/client/xml.rb +4 -8
  168. data/lib/aws/simple_email_service/config.rb +19 -0
  169. data/lib/aws/simple_email_service/email_address_collection.rb +1 -3
  170. data/lib/aws/simple_email_service/errors.rb +1 -4
  171. data/lib/aws/simple_email_service/quotas.rb +1 -3
  172. data/lib/aws/simple_email_service/request.rb +3 -7
  173. data/lib/aws/sns.rb +15 -8
  174. data/lib/aws/sns/client.rb +7 -8
  175. data/lib/aws/sns/client/options.rb +3 -6
  176. data/lib/aws/sns/client/xml.rb +4 -8
  177. data/lib/aws/sns/config.rb +18 -0
  178. data/lib/aws/sns/errors.rb +1 -4
  179. data/lib/aws/sns/policy.rb +3 -5
  180. data/lib/aws/sns/request.rb +4 -9
  181. data/lib/aws/sns/subscription.rb +1 -5
  182. data/lib/aws/sns/subscription_collection.rb +1 -5
  183. data/lib/aws/sns/topic.rb +1 -6
  184. data/lib/aws/sns/topic_collection.rb +1 -4
  185. data/lib/aws/sns/topic_subscription_collection.rb +1 -4
  186. data/lib/aws/sqs.rb +14 -5
  187. data/lib/aws/sqs/client.rb +6 -9
  188. data/lib/aws/sqs/client/xml.rb +4 -7
  189. data/lib/aws/sqs/config.rb +18 -0
  190. data/lib/aws/sqs/errors.rb +1 -4
  191. data/lib/aws/sqs/policy.rb +3 -5
  192. data/lib/aws/sqs/queue.rb +2 -7
  193. data/lib/aws/sqs/queue_collection.rb +4 -5
  194. data/lib/aws/sqs/received_message.rb +1 -3
  195. data/lib/aws/sqs/received_sns_message.rb +2 -2
  196. data/lib/aws/sqs/request.rb +4 -7
  197. data/lib/aws/sts.rb +12 -6
  198. data/lib/aws/sts/client.rb +6 -8
  199. data/lib/aws/sts/client/xml.rb +4 -8
  200. data/lib/aws/sts/config.rb +18 -0
  201. data/lib/aws/sts/errors.rb +1 -4
  202. data/lib/aws/sts/federated_session.rb +0 -2
  203. data/lib/aws/sts/policy.rb +3 -5
  204. data/lib/aws/sts/request.rb +3 -7
  205. metadata +60 -50
  206. data/lib/aws/async_handle.rb +0 -90
  207. data/lib/aws/authorize_v2.rb +0 -37
  208. data/lib/aws/base_client.rb +0 -488
  209. data/lib/aws/cacheable.rb +0 -79
  210. data/lib/aws/client_logging.rb +0 -122
  211. data/lib/aws/collections.rb +0 -230
  212. data/lib/aws/configuration.rb +0 -357
  213. data/lib/aws/configured_client_methods.rb +0 -81
  214. data/lib/aws/configured_grammars.rb +0 -65
  215. data/lib/aws/default_signer.rb +0 -65
  216. data/lib/aws/http/curb_handler.rb +0 -127
  217. data/lib/aws/http/handler.rb +0 -77
  218. data/lib/aws/http/httparty_handler.rb +0 -103
  219. data/lib/aws/http/request.rb +0 -165
  220. data/lib/aws/http/request_param.rb +0 -64
  221. data/lib/aws/http/response.rb +0 -72
  222. data/lib/aws/indifferent_hash.rb +0 -86
  223. data/lib/aws/inflection.rb +0 -46
  224. data/lib/aws/lazy_error_classes.rb +0 -64
  225. data/lib/aws/meta_utils.rb +0 -43
  226. data/lib/aws/model.rb +0 -57
  227. data/lib/aws/option_grammar.rb +0 -565
  228. data/lib/aws/policy.rb +0 -914
  229. data/lib/aws/resource.rb +0 -381
  230. data/lib/aws/response.rb +0 -125
  231. data/lib/aws/response_cache.rb +0 -50
  232. data/lib/aws/service_interface.rb +0 -60
  233. data/lib/aws/xml_grammar.rb +0 -925
@@ -0,0 +1,562 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'base64'
15
+
16
+ module AWS
17
+ module Core
18
+
19
+ # @private
20
+ class OptionGrammar
21
+
22
+ # @private
23
+ class DefaultOption; end
24
+
25
+ # @private
26
+ class FormatError < ArgumentError
27
+ attr_accessor :expectation
28
+ attr_accessor :context_description
29
+
30
+ def initialize(expectation, context)
31
+ @expectation = expectation
32
+ @context_description = context
33
+ end
34
+
35
+ def to_s
36
+ "expected #{expectation} for #{context_description}"
37
+ end
38
+ end
39
+
40
+ # @private
41
+ module Descriptors
42
+
43
+ # @private
44
+ module NoArgs
45
+ def apply(option)
46
+ option.extend self
47
+ end
48
+ end
49
+
50
+ module Timestamp
51
+
52
+ extend NoArgs
53
+
54
+ def validate(value, context = nil)
55
+ true
56
+ # raise format_error("timestamp value", context) unless
57
+ # case value
58
+ # when String
59
+ # value =~ /^\d+$/ or value =~ /^\d{4}-\d{2}-d{2}T\d{2}:\d{2}:\d{2}Z$/
60
+ # when String then value =~ /^2009-12-04T20:56:05.000Z\d+$/
61
+ # when Integer then true
62
+ # when DateTime then true
63
+ # when Timestamp then true
64
+ # when Date then true
65
+ # else false
66
+ # end
67
+ # end
68
+ # value.respond_to? :to_str
69
+ end
70
+
71
+ def encode_value(value)
72
+ value.to_s
73
+ # value.to_s
74
+ # case value
75
+ # when Integer
76
+ # when
77
+ # case value
78
+ # when nil, '' then nil
79
+ # when DateTime then raw
80
+ # when Integer then DateTime.parse(Time.at(raw).to_s) # timestamp
81
+ # else DateTime.parse(raw.to_s) # work with Time, Date and String objects
82
+ # end
83
+ end
84
+ end
85
+
86
+ # @private
87
+ module String
88
+
89
+ extend NoArgs
90
+
91
+ def validate(value, context = nil)
92
+ raise format_error("string value", context) unless
93
+ value.respond_to? :to_str
94
+ end
95
+
96
+ def encode_value(value)
97
+ value.to_s
98
+ end
99
+
100
+ end
101
+
102
+ # @private
103
+ module Blob
104
+
105
+ extend NoArgs
106
+
107
+ def validate(value, context = nil)
108
+ raise format_error("string value", context) unless
109
+ value.respond_to? :to_str
110
+ end
111
+
112
+ def encode_value(value)
113
+ Base64.encode64(value.to_s)
114
+ end
115
+
116
+ end
117
+
118
+ # @private
119
+ module Integer
120
+
121
+ extend NoArgs
122
+
123
+ def validate(value, context = nil)
124
+ raise format_error("integer value", context) unless
125
+ value.respond_to? :to_int
126
+ end
127
+
128
+ def encode_value(value)
129
+ value.to_s
130
+ end
131
+
132
+ end
133
+
134
+ # @private
135
+ module Boolean
136
+
137
+ extend NoArgs
138
+
139
+ def validate(value, context = nil)
140
+ raise format_error("boolean value", context) unless
141
+ value == true || value == false
142
+ end
143
+
144
+ def encode_value(value)
145
+ value.to_s
146
+ end
147
+
148
+ end
149
+
150
+ # @private
151
+ module Required
152
+ extend NoArgs
153
+ def required?; true; end
154
+ end
155
+
156
+ # @private
157
+ module Rename
158
+ def self.apply(option, new_name)
159
+ new_name = Inflection.ruby_name(new_name)
160
+ MetaUtils.extend_method(option, :ruby_name) { new_name }
161
+ end
162
+ end
163
+
164
+ # @private
165
+ module Pattern
166
+
167
+ # def validate value, context = nil
168
+ # unless value =~ regex
169
+ # raise format_error("value to match #{regex}", context)
170
+ # end
171
+ # end
172
+ #
173
+ # def self.apply option, regex
174
+ # option.extend(self)
175
+ # MetaUtils.extend_method(option, :regex) { regex }
176
+ # end
177
+
178
+ def self.apply *args
179
+ end
180
+
181
+ end
182
+
183
+ # @private
184
+ module ListMethods
185
+
186
+ module ClassMethods
187
+
188
+ def apply(option, member_descriptors)
189
+ super(option)
190
+ member_option = option.member_option if option.respond_to?(:member_option)
191
+ member_option ||= ListMember.new
192
+ member_option = member_option.extend_with_config(*member_descriptors)
193
+ MetaUtils.extend_method(option, :member_option) { member_option }
194
+ end
195
+
196
+ end
197
+
198
+ module InstanceMethods
199
+
200
+ def validate(value, context = nil)
201
+ raise format_error("enumerable value", context) unless
202
+ value.respond_to? :each
203
+ i = 0
204
+ value.each do |member|
205
+ i += 1
206
+ member_option.validate(member,
207
+ "member #{i} of #{context_description(context)}")
208
+ end
209
+ end
210
+
211
+ def request_params(value, prefix = nil)
212
+ params = []
213
+ value.each do |v|
214
+ name = prefixed_name(prefix) + join + (params.size + 1).to_s
215
+ params << member_option.request_params(v, name)
216
+ end
217
+ return [Http::Request::Param.new(prefixed_name(prefix), "")] if params.empty?
218
+ params
219
+ end
220
+
221
+ def join
222
+ '.'
223
+ end
224
+
225
+ end
226
+
227
+ end
228
+
229
+ module List
230
+
231
+ extend NoArgs
232
+ extend ListMethods::ClassMethods
233
+ include ListMethods::InstanceMethods
234
+
235
+ end
236
+
237
+ module MemberedList
238
+
239
+ extend NoArgs
240
+ extend ListMethods::ClassMethods
241
+ include ListMethods::InstanceMethods
242
+
243
+ def join
244
+ '.member.'
245
+ end
246
+
247
+ end
248
+
249
+ class ListMember < DefaultOption
250
+
251
+ def initialize options = {}
252
+ super("##list-member##")
253
+ @prefix = options[:prefix] || ''
254
+ end
255
+
256
+ def prefixed_name(prefix)
257
+ "#{prefix}#{@prefix}"
258
+ end
259
+
260
+ end
261
+
262
+ # @private
263
+ module Structure
264
+
265
+ extend NoArgs
266
+
267
+ def self.apply(option, members)
268
+ options = {}
269
+ options = option.member_options.inject({}) do |memo, member_option|
270
+ memo[member_option.name] = member_option
271
+ memo
272
+ end if option.respond_to?(:member_options)
273
+
274
+ super(option)
275
+
276
+ members.each do |(name, descriptors)|
277
+ member_option = options[name] || DefaultOption.new(name)
278
+ member_option = member_option.extend_with_config(*descriptors)
279
+ options[name] = member_option
280
+ end
281
+
282
+ MetaUtils.extend_method(option, :member_options) { options.values }
283
+ by_ruby_name = options.values.inject({}) do |memo, member_option|
284
+ memo[member_option.ruby_name] = member_option
285
+ memo
286
+ end
287
+ MetaUtils.extend_method(option, :member_option) { |n| by_ruby_name[n] }
288
+ end
289
+
290
+ def validate(value, context = nil)
291
+ raise format_error("hash value", context) unless
292
+ value.respond_to?(:to_hash)
293
+
294
+ context = context_description(context)
295
+
296
+ value.each do |name, v|
297
+ name = name.to_s
298
+ raise ArgumentError.new("unexpected key #{name} for #{context}") unless
299
+ member_option(name)
300
+ member_option(name).validate(v, "key #{name} of #{context}")
301
+ end
302
+
303
+ member_options.each do |option|
304
+ raise ArgumentError.new("missing required key #{option.ruby_name} for #{context}") if
305
+ option.required? and
306
+ !value.has_key?(option.ruby_name) and
307
+ !value.has_key?(option.ruby_name.to_sym)
308
+ end
309
+ end
310
+
311
+ def request_params(values, prefix = nil)
312
+ values.map do |name, value|
313
+ name = name.to_s
314
+ member_option(name).request_params(value, prefixed_name(prefix))
315
+ end.flatten
316
+ end
317
+
318
+ end
319
+
320
+ # @private
321
+ module Boolean
322
+ extend NoArgs
323
+ end
324
+
325
+ end
326
+
327
+ class DefaultOption
328
+
329
+ attr_reader :name
330
+
331
+ def initialize(name)
332
+ @name = name
333
+ end
334
+
335
+ def ruby_name
336
+ Inflection.ruby_name(name)
337
+ end
338
+
339
+ def request_params(value, prefix = nil)
340
+ [Http::Request::Param.new(prefixed_name(prefix), encode_value(value))]
341
+ end
342
+
343
+ def prefixed_name(prefix)
344
+ return "#{prefix}.#{name}" if prefix
345
+ name
346
+ end
347
+
348
+ def encode_value(value)
349
+ value
350
+ end
351
+
352
+ def required?
353
+ false
354
+ end
355
+
356
+ def format_error(expected, context = nil)
357
+ context = context_description(context)
358
+ FormatError.new(expected, context)
359
+ end
360
+
361
+ def context_description(context)
362
+ context or "option #{ruby_name}"
363
+ end
364
+
365
+ def extend_with_config(*descriptors)
366
+ option = clone
367
+ descriptors.each do |desc|
368
+ if desc.kind_of?(Hash)
369
+ (name, arg) = desc.to_a.first
370
+ else
371
+ name = desc
372
+ arg = nil
373
+ end
374
+ class_name = Inflection.class_name(name.to_s)
375
+ mod = Descriptors::const_get(class_name)
376
+ if arg
377
+ mod.apply(option, arg)
378
+ else
379
+ mod.apply(option)
380
+ end
381
+ end
382
+ option
383
+ end
384
+
385
+ include Descriptors::String
386
+
387
+ end
388
+
389
+ # @private
390
+ module ModuleMethods
391
+
392
+ include Inflection
393
+
394
+ def customize(config = [])
395
+ m = Class.new(self)
396
+ supported_options = m.supported_options.inject({}) do |memo, opt|
397
+ memo[opt.name] = opt
398
+ memo
399
+ end
400
+ config.each do |option_config|
401
+ if config.kind_of?(Hash)
402
+ (name, value_desc) = option_config
403
+ else
404
+ (name, value_desc) = parse_option(option_config)
405
+ end
406
+ option = supported_options[name] || DefaultOption.new(name)
407
+ option = option.extend_with_config(*value_desc)
408
+ supported_options[option.name] = option
409
+ end
410
+
411
+ supported_ary = supported_options.values
412
+ MetaUtils.extend_method(m, :supported_options) { supported_ary }
413
+ supported_ruby_names = supported_ary.inject({}) do |memo, opt|
414
+ memo[opt.ruby_name] = opt
415
+ memo
416
+ end
417
+ MetaUtils.extend_method(m, :option) { |n| supported_ruby_names[n] }
418
+ supported_ary.each do |opt|
419
+ MetaUtils.extend_method(m, "validate_#{opt.ruby_name}") do |value|
420
+ opt.validate(value)
421
+ end
422
+ end
423
+
424
+ m
425
+ end
426
+
427
+ def option(name)
428
+ nil
429
+ end
430
+
431
+ def supported_options
432
+ []
433
+ end
434
+
435
+ def validate(options)
436
+ options.each do |name, value|
437
+ name = name.to_s
438
+ raise ArgumentError.new("unexpected option #{name}") unless
439
+ option(name)
440
+ option(name).validate(value)
441
+ end
442
+ supported_options.each do |option|
443
+ raise ArgumentError.new("missing required option #{option.ruby_name}") unless
444
+ !option.required? ||
445
+ options.has_key?(option.ruby_name) || options.has_key?(option.ruby_name.to_sym)
446
+ end
447
+ end
448
+
449
+ def request_params(options)
450
+ validate(options)
451
+ options.map do |(name, value)|
452
+ name = name.to_s
453
+ option(name).request_params(value)
454
+ end.flatten
455
+ end
456
+
457
+ def included(m)
458
+ m.extend(self::ModuleMethods)
459
+ end
460
+
461
+ protected
462
+ def parse_option(option)
463
+ value_desc = nil
464
+ if option.kind_of? Hash
465
+ raise ArgumentError.new("passed empty hash where an option was expected") if
466
+ option.empty?
467
+
468
+ raise ArgumentError.new("too many entries in option description") if
469
+ option.size > 1
470
+
471
+ (name, value_desc) = option.to_a.first
472
+ name = name.to_s
473
+
474
+ raise ArgumentError.new("expected an array for "+
475
+ "value description of option #{name},"+
476
+ "got #{value_desc.inspect}") unless
477
+ value_desc.nil? or value_desc.kind_of?(Array)
478
+ else
479
+ name = option
480
+ end
481
+
482
+ value_desc ||= []
483
+
484
+ [name, value_desc]
485
+ end
486
+
487
+ protected
488
+ def apply_required_descriptor(m, name)
489
+ name = ruby_name(name)
490
+ MetaUtils.extend_method(m, :validate) do |opts|
491
+ raise ArgumentError.new("missing required option #{name}") unless
492
+ opts.key? name or opts.key? name.to_sym
493
+ end
494
+ end
495
+
496
+ protected
497
+ def apply_integer_descriptor(m, name)
498
+ MetaUtils.extend_method(m, "validate_#{ruby_name(name)}") do |value|
499
+ raise ArgumentError.new("expected integer value for option #{ruby_name(name)}") unless
500
+ value.respond_to? :to_int
501
+ end
502
+ end
503
+
504
+ protected
505
+ def apply_string_descriptor(m, name)
506
+ MetaUtils.extend_method(m, "validate_#{ruby_name(name)}") do |value|
507
+ raise ArgumentError.new("expected string value for option #{ruby_name(name)}") unless
508
+ value.respond_to? :to_str
509
+ end
510
+ end
511
+
512
+ protected
513
+ def apply_list_descriptor(m, name, arg)
514
+ MetaUtils.extend_method(m, "validate_#{ruby_name(name)}") do |value|
515
+ raise ArgumentError.new("expected value for option #{ruby_name(name)} "+
516
+ "to respond to #each") unless
517
+ value.respond_to? :each
518
+ end
519
+ MetaUtils.extend_method(m, "params_for_#{ruby_name(name)}") do |value|
520
+ i = 0
521
+ values = []
522
+ value.each do |member|
523
+ i += 1
524
+ values << Http::Request::Param.new(name+"."+i.to_s, member.to_s)
525
+ end
526
+ if i > 0
527
+ values
528
+ else
529
+ Http::Request::Param.new(name, "")
530
+ end
531
+ end
532
+ end
533
+
534
+ protected
535
+ def apply_rename_descriptor(m, name, new_name)
536
+ name = ruby_name(name)
537
+ MetaUtils.extend_method(m, :validate) do |opts|
538
+ raise ArgumentError.new("unexpected option foo") if
539
+ opts.key?(name) or opts.key?(name.to_sym)
540
+
541
+ opts = opts.dup
542
+ opts[name] = opts[new_name] if opts.key?(new_name)
543
+ opts[name.to_sym] = opts[new_name.to_sym] if opts.key?(new_name.to_sym)
544
+ opts.delete(new_name)
545
+ opts.delete(new_name.to_sym)
546
+ super(opts)
547
+ end
548
+
549
+ # couldn't find a better way to alias a class method
550
+ method = m.method("params_for_#{name}")
551
+ MetaUtils.extend_method(m, "params_for_#{new_name}") do |value|
552
+ method.call(value)
553
+ end
554
+ end
555
+
556
+ end
557
+
558
+ extend ModuleMethods
559
+
560
+ end
561
+ end
562
+ end