aws-sdk-core 2.0.48 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. checksums.yaml +4 -4
  2. data/apis/autoscaling/2011-01-01/api-2.json +636 -33
  3. data/apis/cloudhsm/2014-05-30/api-2.json +3 -6
  4. data/apis/ec2/2014-10-01/api-2.json +10267 -0
  5. data/apis/ec2/2014-10-01/paginators-1.json +125 -0
  6. data/apis/ec2/2014-10-01/resources-1.json +2289 -0
  7. data/apis/ec2/2014-10-01/waiters-2.json +453 -0
  8. data/apis/ec2/2015-03-01/api-2.json +11245 -0
  9. data/apis/ec2/2015-03-01/paginators-1.json +125 -0
  10. data/apis/ec2/2015-03-01/resources-1.json +2479 -0
  11. data/apis/ec2/2015-03-01/waiters-2.json +458 -0
  12. data/apis/ec2/2015-04-15/api-2.json +182 -2
  13. data/apis/ecs/2014-11-13/api-2.json +316 -7
  14. data/apis/ecs/2014-11-13/paginators-1.json +40 -0
  15. data/apis/ecs/2014-11-13/waiters-2.json +93 -0
  16. data/apis/redshift/2012-12-01/waiters-2.json +19 -0
  17. data/apis/s3/2006-03-01/api-2.json +1 -2
  18. data/apis/sqs/2012-11-05/api-2.json +5 -6
  19. data/ca-bundle.crt +3554 -0
  20. data/lib/aws-sdk-core.rb +127 -44
  21. data/lib/aws-sdk-core/api/builder.rb +95 -0
  22. data/lib/aws-sdk-core/api/customizations.rb +152 -0
  23. data/lib/aws-sdk-core/api/docs/builder.rb +220 -0
  24. data/lib/aws-sdk-core/api/docs/client_type_documenter.rb +109 -0
  25. data/lib/aws-sdk-core/api/docs/docstring_provider.rb +66 -0
  26. data/lib/aws-sdk-core/api/docs/operation_documenter.rb +107 -0
  27. data/lib/aws-sdk-core/api/docs/param_formatter.rb +163 -0
  28. data/lib/aws-sdk-core/api/docs/request_syntax_example.rb +22 -0
  29. data/lib/aws-sdk-core/api/docs/response_structure_example.rb +91 -0
  30. data/lib/aws-sdk-core/api/docs/utils.rb +133 -0
  31. data/lib/aws-sdk-core/api/shape_map.rb +140 -0
  32. data/lib/aws-sdk-core/assume_role_credentials.rb +9 -6
  33. data/lib/aws-sdk-core/client.rb +9 -6
  34. data/lib/aws-sdk-core/client_stubs.rb +127 -133
  35. data/lib/aws-sdk-core/client_waiters.rb +1 -1
  36. data/lib/aws-sdk-core/credential_provider.rb +44 -0
  37. data/lib/aws-sdk-core/credential_provider_chain.rb +3 -2
  38. data/lib/aws-sdk-core/credentials.rb +5 -0
  39. data/lib/aws-sdk-core/deprecations.rb +69 -0
  40. data/lib/aws-sdk-core/dynamodb.rb +12 -0
  41. data/lib/aws-sdk-core/eager_loader.rb +31 -0
  42. data/lib/aws-sdk-core/ecs.rb +2 -0
  43. data/lib/aws-sdk-core/empty_structure.rb +1 -82
  44. data/lib/aws-sdk-core/endpoint_provider.rb +1 -1
  45. data/lib/aws-sdk-core/instance_profile_credentials.rb +9 -10
  46. data/lib/aws-sdk-core/json.rb +56 -0
  47. data/lib/aws-sdk-core/json/builder.rb +33 -34
  48. data/lib/aws-sdk-core/json/error_handler.rb +2 -2
  49. data/lib/aws-sdk-core/json/handler.rb +67 -0
  50. data/lib/aws-sdk-core/json/json_engine.rb +15 -0
  51. data/lib/aws-sdk-core/json/oj_engine.rb +15 -0
  52. data/lib/aws-sdk-core/json/parser.rb +30 -38
  53. data/lib/aws-sdk-core/pageable_response.rb +9 -12
  54. data/lib/aws-sdk-core/pager.rb +69 -0
  55. data/lib/aws-sdk-core/param_converter.rb +203 -0
  56. data/lib/aws-sdk-core/param_validator.rb +148 -0
  57. data/lib/aws-sdk-core/plugins/dynamodb_simple_attributes.rb +21 -18
  58. data/lib/aws-sdk-core/plugins/glacier_account_id.rb +9 -1
  59. data/lib/aws-sdk-core/plugins/glacier_checksums.rb +2 -3
  60. data/lib/aws-sdk-core/plugins/param_converter.rb +27 -0
  61. data/lib/aws-sdk-core/plugins/param_validator.rb +28 -0
  62. data/lib/aws-sdk-core/plugins/protocols/json_rpc.rb +3 -7
  63. data/lib/aws-sdk-core/plugins/protocols/rest_json.rb +1 -2
  64. data/lib/aws-sdk-core/plugins/protocols/rest_xml.rb +1 -1
  65. data/lib/aws-sdk-core/plugins/regional_endpoint.rb +1 -1
  66. data/lib/aws-sdk-core/plugins/request_signer.rb +5 -5
  67. data/lib/aws-sdk-core/plugins/response_paging.rb +20 -11
  68. data/lib/aws-sdk-core/plugins/s3_get_bucket_location_fix.rb +1 -1
  69. data/lib/aws-sdk-core/plugins/s3_request_signer.rb +1 -2
  70. data/lib/aws-sdk-core/plugins/stub_responses.rb +25 -27
  71. data/lib/aws-sdk-core/query/ec2_param_builder.rb +29 -34
  72. data/lib/aws-sdk-core/query/handler.rb +32 -18
  73. data/lib/aws-sdk-core/query/param_builder.rb +45 -53
  74. data/lib/aws-sdk-core/refreshing_credentials.rb +3 -15
  75. data/lib/aws-sdk-core/rest/handler.rb +22 -0
  76. data/lib/aws-sdk-core/rest/request/body.rb +58 -0
  77. data/lib/aws-sdk-core/rest/request/builder.rb +50 -0
  78. data/lib/aws-sdk-core/rest/request/endpoint.rb +70 -0
  79. data/lib/aws-sdk-core/rest/request/headers.rb +48 -0
  80. data/lib/aws-sdk-core/rest/response/body.rb +43 -0
  81. data/lib/aws-sdk-core/rest/response/headers.rb +60 -0
  82. data/lib/aws-sdk-core/rest/response/parser.rb +47 -0
  83. data/lib/aws-sdk-core/rest/response/status_code.rb +24 -0
  84. data/lib/aws-sdk-core/shared_credentials.rb +11 -6
  85. data/lib/aws-sdk-core/signers/base.rb +1 -1
  86. data/lib/aws-sdk-core/signers/s3.rb +2 -3
  87. data/lib/aws-sdk-core/signers/v2.rb +7 -4
  88. data/lib/aws-sdk-core/signers/v4.rb +11 -22
  89. data/lib/aws-sdk-core/structure.rb +26 -145
  90. data/lib/aws-sdk-core/stubbing/data_applicator.rb +46 -0
  91. data/lib/aws-sdk-core/stubbing/empty_stub.rb +53 -0
  92. data/lib/aws-sdk-core/stubbing/protocols/ec2.rb +49 -0
  93. data/lib/aws-sdk-core/stubbing/protocols/json.rb +40 -0
  94. data/lib/aws-sdk-core/stubbing/protocols/query.rb +40 -0
  95. data/lib/aws-sdk-core/stubbing/protocols/rest.rb +67 -0
  96. data/lib/aws-sdk-core/stubbing/protocols/rest_json.rb +25 -0
  97. data/lib/aws-sdk-core/stubbing/protocols/rest_xml.rb +38 -0
  98. data/lib/aws-sdk-core/stubbing/stub_data.rb +34 -0
  99. data/lib/aws-sdk-core/version.rb +1 -1
  100. data/lib/aws-sdk-core/xml/builder.rb +58 -52
  101. data/lib/aws-sdk-core/xml/default_map.rb +10 -0
  102. data/lib/aws-sdk-core/xml/doc_builder.rb +86 -0
  103. data/lib/aws-sdk-core/xml/error_handler.rb +7 -2
  104. data/lib/aws-sdk-core/xml/parser.rb +4 -4
  105. data/lib/aws-sdk-core/xml/parser/frame.rb +75 -70
  106. data/lib/aws-sdk-core/xml/parser/stack.rb +4 -4
  107. data/lib/seahorse.rb +0 -11
  108. data/lib/seahorse/client/base.rb +3 -12
  109. data/lib/seahorse/client/http/request.rb +9 -5
  110. data/lib/seahorse/client/plugins/endpoint.rb +5 -56
  111. data/lib/seahorse/client/plugins/restful_bindings.rb +38 -31
  112. data/lib/seahorse/client/request_context.rb +2 -2
  113. data/lib/seahorse/client/response.rb +6 -5
  114. data/lib/seahorse/model/api.rb +18 -61
  115. data/lib/seahorse/model/operation.rb +24 -54
  116. data/lib/seahorse/model/shapes.rb +120 -364
  117. data/lib/seahorse/util.rb +2 -6
  118. metadata +56 -109
  119. data/apis/autoscaling/2011-01-01/docs-2.json +0 -1246
  120. data/apis/cloudformation/2010-05-15/docs-2.json +0 -725
  121. data/apis/cloudfront/2014-11-06/docs-2.json +0 -1125
  122. data/apis/cloudhsm/2014-05-30/docs-2.json +0 -472
  123. data/apis/cloudsearch/2013-01-01/docs-2.json +0 -865
  124. data/apis/cloudsearchdomain/2013-01-01/docs-2.json +0 -311
  125. data/apis/cloudtrail/2013-11-01/docs-2.json +0 -328
  126. data/apis/codedeploy/2014-10-06/docs-2.json +0 -1261
  127. data/apis/cognito-identity/2014-06-30/docs-2.json +0 -506
  128. data/apis/cognito-sync/2014-06-30/docs-2.json +0 -582
  129. data/apis/config/2014-10-17/docs-2.json +0 -513
  130. data/apis/config/2014-11-12/docs-2.json +0 -514
  131. data/apis/datapipeline/2012-10-29/docs-2.json +0 -607
  132. data/apis/directconnect/2012-10-25/docs-2.json +0 -494
  133. data/apis/ds/2015-04-16/docs-2.json +0 -753
  134. data/apis/dynamodb/2011-12-05/api-2.json +0 -949
  135. data/apis/dynamodb/2011-12-05/docs-2.json +0 -606
  136. data/apis/dynamodb/2011-12-05/paginators-1.json +0 -26
  137. data/apis/dynamodb/2011-12-05/waiters-2.json +0 -35
  138. data/apis/dynamodb/2012-08-10/docs-2.json +0 -977
  139. data/apis/ec2/2015-04-15/docs-2.json +0 -5368
  140. data/apis/ecs/2014-11-13/docs-2.json +0 -791
  141. data/apis/elasticache/2015-02-02/docs-2.json +0 -1390
  142. data/apis/elasticbeanstalk/2010-12-01/docs-2.json +0 -1114
  143. data/apis/elasticfilesystem/2015-02-01/docs-2.json +0 -414
  144. data/apis/elasticloadbalancing/2012-06-01/docs-2.json +0 -1078
  145. data/apis/elasticmapreduce/2009-03-31/docs-2.json +0 -969
  146. data/apis/elastictranscoder/2012-09-25/docs-2.json +0 -1152
  147. data/apis/email/2010-12-01/docs-2.json +0 -441
  148. data/apis/glacier/2012-06-01/docs-2.json +0 -575
  149. data/apis/iam/2010-05-08/docs-2.json +0 -2138
  150. data/apis/importexport/2010-06-01/docs-2.json +0 -432
  151. data/apis/kinesis/2013-12-02/docs-2.json +0 -424
  152. data/apis/kms/2014-11-01/docs-2.json +0 -628
  153. data/apis/lambda/2014-11-11/docs-2.json +0 -303
  154. data/apis/lambda/2015-03-31/docs-2.json +0 -490
  155. data/apis/logs/2014-03-28/docs-2.json +0 -599
  156. data/apis/machinelearning/2014-12-12/docs-2.json +0 -1034
  157. data/apis/monitoring/2010-08-01/docs-2.json +0 -514
  158. data/apis/opsworks/2013-02-18/docs-2.json +0 -1592
  159. data/apis/rds/2014-10-31/docs-2.json +0 -2074
  160. data/apis/redshift/2012-12-01/docs-2.json +0 -1890
  161. data/apis/route53/2013-04-01/docs-2.json +0 -1159
  162. data/apis/route53domains/2014-05-15/docs-2.json +0 -620
  163. data/apis/s3/2006-03-01/docs-2.json +0 -2213
  164. data/apis/sdb/2009-04-15/docs-2.json +0 -339
  165. data/apis/sns/2010-03-31/docs-2.json +0 -564
  166. data/apis/sqs/2012-11-05/docs-2.json +0 -503
  167. data/apis/ssm/2014-11-06/docs-2.json +0 -440
  168. data/apis/storagegateway/2013-06-30/docs-2.json +0 -1331
  169. data/apis/sts/2011-06-15/docs-2.json +0 -351
  170. data/apis/support/2013-04-15/docs-2.json +0 -680
  171. data/apis/swf/2012-01-25/docs-2.json +0 -1569
  172. data/apis/workspaces/2015-04-08/docs-2.json +0 -457
  173. data/lib/aws-sdk-core/api/customizer.rb +0 -75
  174. data/lib/aws-sdk-core/api/docstrings.rb +0 -38
  175. data/lib/aws-sdk-core/api/documenter.rb +0 -241
  176. data/lib/aws-sdk-core/api/operation_documenter.rb +0 -173
  177. data/lib/aws-sdk-core/api/operation_example.rb +0 -133
  178. data/lib/aws-sdk-core/api/service_customizations.rb +0 -140
  179. data/lib/aws-sdk-core/client_paging.rb +0 -31
  180. data/lib/aws-sdk-core/json/rest_handler.rb +0 -20
  181. data/lib/aws-sdk-core/json/rpc_body_handler.rb +0 -38
  182. data/lib/aws-sdk-core/json/rpc_headers_handler.rb +0 -34
  183. data/lib/aws-sdk-core/json/simple_body_handler.rb +0 -34
  184. data/lib/aws-sdk-core/paging/null_pager.rb +0 -20
  185. data/lib/aws-sdk-core/paging/null_provider.rb +0 -13
  186. data/lib/aws-sdk-core/paging/pager.rb +0 -70
  187. data/lib/aws-sdk-core/paging/provider.rb +0 -22
  188. data/lib/aws-sdk-core/rest_body_handler.rb +0 -111
  189. data/lib/aws-sdk-core/signers/handler.rb +0 -18
  190. data/lib/aws-sdk-core/xml/rest_handler.rb +0 -20
  191. data/lib/seahorse/client/param_converter.rb +0 -207
  192. data/lib/seahorse/client/param_validator.rb +0 -139
  193. data/lib/seahorse/client/plugins/json_simple.rb +0 -33
  194. data/lib/seahorse/client/plugins/param_conversion.rb +0 -29
  195. data/lib/seahorse/client/plugins/param_validation.rb +0 -30
  196. data/lib/seahorse/client/xml/builder.rb +0 -91
  197. data/lib/seahorse/model/shape_map.rb +0 -47
@@ -1,13 +0,0 @@
1
- module Aws
2
- module Paging
3
- class NullProvider
4
-
5
- # @param [String] operation_name
6
- # @return [NullPager]
7
- def pager(operation_name)
8
- NullPager.new
9
- end
10
-
11
- end
12
- end
13
- end
@@ -1,70 +0,0 @@
1
- module Aws
2
- module Paging
3
- class Pager
4
-
5
- def initialize(rules)
6
- if rules['more_results']
7
- @more_results = underscore(rules['more_results'])
8
- end
9
- if rules['limit_key']
10
- @limit_key = underscore(rules['limit_key']).to_sym
11
- end
12
- map_tokens(rules)
13
- end
14
-
15
- # @return [Symbol, nil]
16
- attr_reader :limit_key
17
-
18
- # @param [Seahorse::Client::Response] response
19
- # @return [Hash]
20
- def next_tokens(response)
21
- @tokens.each.with_object({}) do |(source, target), next_tokens|
22
- value = JMESPath.search(source, response.data)
23
- next_tokens[target.to_sym] = value unless empty_value?(value)
24
- end
25
- end
26
-
27
- # @api private
28
- def prev_tokens(response)
29
- @tokens.each.with_object({}) do |(_, target), tokens|
30
- value = JMESPath.search(target, response.context.params)
31
- tokens[target.to_sym] = value unless empty_value?(value)
32
- end
33
- end
34
-
35
- # @param [Seahorse::Client::Response] response
36
- # @return [Boolean]
37
- def truncated?(response)
38
- if @more_results
39
- JMESPath.search(@more_results, response.data)
40
- else
41
- next_tokens = self.next_tokens(response)
42
- prev_tokens = self.prev_tokens(response)
43
- !(next_tokens.empty? || next_tokens == prev_tokens)
44
- end
45
- end
46
-
47
- private
48
-
49
- def map_tokens(rules)
50
- input = Array(rules['input_token'])
51
- output = Array(rules['output_token'])
52
- @tokens = {}
53
- input.each.with_index do |key, n|
54
- @tokens[underscore(output[n])] = underscore(key)
55
- end
56
- end
57
-
58
- def underscore(str)
59
- str.
60
- gsub(' or ', '||').
61
- gsub(/\w+/) { |part| Seahorse::Util.underscore(part) }
62
- end
63
-
64
- def empty_value?(value)
65
- value.nil? || value == '' || value == [] || value == {}
66
- end
67
-
68
- end
69
- end
70
- end
@@ -1,22 +0,0 @@
1
- module Aws
2
- module Paging
3
- class Provider
4
-
5
- # @param [Hash] rules
6
- def initialize(rules)
7
- @operations = rules['pagination'].select { |k,v| v.key?('input_token') }
8
- end
9
-
10
- # @param [String] operation_name
11
- # @return [Pager]
12
- def pager(operation_name)
13
- if rules = @operations[operation_name]
14
- Pager.new(rules)
15
- else
16
- NullPager.new
17
- end
18
- end
19
-
20
- end
21
- end
22
- end
@@ -1,111 +0,0 @@
1
- module Aws
2
- # @api private
3
- class RestBodyHandler < Seahorse::Client::Handler
4
-
5
- # @param [Seahorse::Client::RequestContext] context
6
- # @return [Seahorse::Client::Response]
7
- def call(context)
8
- context.http_request.body = build_body(context)
9
- @handler.call(context).on_success do |response|
10
- response.data = extract_data(response.context) unless response.data
11
- end.on(200..599) do |respson|
12
- context[:request_id] = extract_request_id(context)
13
- end
14
- end
15
-
16
- private
17
-
18
- def extract_request_id(context)
19
- headers = context.http_response.headers
20
- headers['x-amz-request-id'] || headers['x-amzn-requestid']
21
- end
22
-
23
- def build_body(context)
24
- input = context.operation.input
25
- case
26
- when input.nil?
27
- nil
28
- when streaming?(input)
29
- context.params[input.payload]
30
- when input.payload
31
- if params = context.params[input.payload]
32
- serialize_params(input.payload_member, params)
33
- end
34
- else
35
- params = body_params(input, context.params)
36
- serialize_params(input, params) unless params.empty?
37
- end
38
- end
39
-
40
- def extract_data(context)
41
- if output = context.operation.output
42
- data = Structure.new(output.member_names)
43
- if streaming?(output)
44
- data[output.payload] = context.http_response.body
45
- elsif output.payload
46
- data[output.payload] = parse(context, output.payload_member)
47
- else
48
- parse(context, output, data)
49
- end
50
- data
51
- else
52
- EmptyStructure.new
53
- end
54
- end
55
-
56
- def body_members_shape(shape)
57
- if shape.payload
58
- shape.payload_member
59
- else
60
- members = shape.members.each.with_object({}) do |(name, member), hash|
61
- if member.location == 'body'
62
- hash[name.to_s] = member.definition
63
- end
64
- end
65
- definition = shape.definition.merge('members' => members)
66
- options = { shape_map: shape.shape_map }
67
- Seahorse::Model::Shapes::Shape.new(definition, options)
68
- end
69
- end
70
-
71
- def body_params(shape, params)
72
- shape.members.each.with_object({}) do |(member_name, member_shape), hash|
73
- if member_shape.location == 'body'
74
- hash[member_name] = params[member_name] if params.key?(member_name)
75
- end
76
- end
77
- end
78
-
79
- def streaming?(shape)
80
- shape.payload && (
81
- Seahorse::Model::Shapes::Blob === shape.payload_member ||
82
- Seahorse::Model::Shapes::String === shape.payload_member
83
- )
84
- end
85
-
86
- def serialize_params(shape, params)
87
- raise NotImplementedError, 'must be defiend in sublcasses'
88
- end
89
-
90
- def body_contents(context)
91
- context.http_response.body_contents
92
- end
93
-
94
- def parse(context, shape, target = nil)
95
- body = context.http_response.body_contents
96
- if body.bytesize == 0
97
- nil
98
- else
99
- parse_body(body, shape, target)
100
- end
101
- end
102
-
103
- # @param [String] body
104
- # @param [Seahorse::Model::Shapes::Structure] shape
105
- # @param [Structure] target
106
- def parse_body(body, shape, target)
107
- raise NotImplementedError, 'must be defiend in sublcasses'
108
- end
109
-
110
- end
111
- end
@@ -1,18 +0,0 @@
1
- module Aws
2
- module Signers
3
- class Handler
4
-
5
- def initialize(signer)
6
- @signer = signer
7
- end
8
-
9
- attr_accessor :handler
10
-
11
- def call(context)
12
- @signer.sign(context)
13
- @handler.call(context)
14
- end
15
-
16
- end
17
- end
18
- end
@@ -1,20 +0,0 @@
1
- module Aws
2
- module Xml
3
- class RestHandler < RestBodyHandler
4
-
5
- # @param [Seahorse::Model::Shapes::Structure] shape
6
- # @param [Hash] params
7
- def serialize_params(shape, params)
8
- Builder.new(shape).to_xml(params)
9
- end
10
-
11
- # @param [String] xml
12
- # @param [Seahorse::Model::Shapes::Structure] shape
13
- # @param [Structure, nil] target
14
- def parse_body(xml, shape, target)
15
- Parser.new(shape).parse(xml, target)
16
- end
17
-
18
- end
19
- end
20
- end
@@ -1,207 +0,0 @@
1
- require 'stringio'
2
- require 'date'
3
- require 'time'
4
- require 'tempfile'
5
- require 'thread'
6
-
7
- module Seahorse
8
- module Client
9
- class ParamConverter
10
-
11
- @mutex = Mutex.new
12
- @converters = Hash.new { |h,k| h[k] = {} }
13
-
14
- # @param [Model::Shapes::Shape] shape
15
- def initialize(shape)
16
- @shape = shape
17
- end
18
-
19
- # @param [Hash] params
20
- # @return [Hash]
21
- def convert(params)
22
- structure(@shape, params)
23
- end
24
-
25
- private
26
-
27
- def structure(structure, values)
28
- values = c(structure, values)
29
- if values.is_a?(Hash)
30
- values.each do |k, v|
31
- unless v.nil?
32
- if structure.member?(k)
33
- values[k] = member(structure.member(k), v)
34
- end
35
- end
36
- end
37
- end
38
- values
39
- end
40
-
41
- def list(list, values)
42
- values = c(list, values)
43
- if values.is_a?(Array)
44
- values.map { |v| member(list.member, v) }
45
- else
46
- values
47
- end
48
- end
49
-
50
- def map(map, values)
51
- values = c(map, values)
52
- if values.is_a?(Hash)
53
- values.each.with_object({}) do |(key, value), hash|
54
- hash[member(map.key, key)] = member(map.value, value)
55
- end
56
- else
57
- values
58
- end
59
- end
60
-
61
- def member(shape, value)
62
- case shape
63
- when Model::Shapes::Structure then structure(shape, value)
64
- when Model::Shapes::List then list(shape, value)
65
- when Model::Shapes::Map then map(shape, value)
66
- else c(shape, value)
67
- end
68
- end
69
-
70
- def c(shape, value)
71
- self.class.c(shape.class, value)
72
- end
73
-
74
- class << self
75
-
76
- # @param [Model::Shapes::InputShape] shape
77
- # @param [Hash] params
78
- # @return [Hash]
79
- def convert(shape, params)
80
- new(shape).convert(params)
81
- end
82
-
83
- # Registers a new value converter. Converters run in the context
84
- # of a shape and value class.
85
- #
86
- # # add a converter that stringifies integers
87
- # shape_class = Seahorse::Model::Shapes::StringShape
88
- # ParamConverter.add(shape_class, Integer) { |i| i.to_s }
89
- #
90
- # @param [Class<Model::Shapes::Shape>] shape_class
91
- # @param [Class] value_class
92
- # @param [#call] converter (nil) An object that responds to `#call`
93
- # accepting a single argument. This function should perform
94
- # the value conversion if possible, returning the result.
95
- # If the conversion is not possible, the original value should
96
- # be returned.
97
- # @return [void]
98
- def add(shape_class, value_class, converter = nil, &block)
99
- @converters[shape_class][value_class] = converter || block
100
- end
101
-
102
- # @api private
103
- def c(shape, value)
104
- if converter = converter_for(shape, value)
105
- converter.call(value)
106
- else
107
- value
108
- end
109
- end
110
-
111
- private
112
-
113
- def converter_for(shape_class, value)
114
- unless @converters[shape_class].key?(value.class)
115
- @mutex.synchronize {
116
- unless @converters[shape_class].key?(value.class)
117
- @converters[shape_class][value.class] = find(shape_class, value)
118
- end
119
- }
120
- end
121
- @converters[shape_class][value.class]
122
- end
123
-
124
- def find(shape_class, value)
125
- converter = nil
126
- each_base_class(shape_class) do |klass|
127
- @converters[klass].each do |value_class, block|
128
- if value_class === value
129
- converter = block
130
- break
131
- end
132
- end
133
- break if converter
134
- end
135
- converter
136
- end
137
-
138
- def each_base_class(shape_class, &block)
139
- shape_class.ancestors.each do |ancestor|
140
- yield(ancestor) if @converters.key?(ancestor)
141
- end
142
- end
143
-
144
- end
145
-
146
- add(Model::Shapes::Structure, Hash) { |h| h.dup }
147
- add(Model::Shapes::Structure, Struct) do |s|
148
- s.members.each.with_object({}) {|k,h| h[k] = s[k] }
149
- end
150
-
151
- add(Model::Shapes::Map, Hash) { |h| h.dup }
152
- add(Model::Shapes::Map, Struct) do |s|
153
- s.members.each.with_object({}) {|k,h| h[k] = s[k] }
154
- end
155
-
156
- add(Model::Shapes::List, Array) { |a| a.dup }
157
- add(Model::Shapes::List, Enumerable) { |value| value.to_a }
158
-
159
- add(Model::Shapes::String, String)
160
- add(Model::Shapes::String, Symbol) { |sym| sym.to_s }
161
-
162
- add(Model::Shapes::Integer, Integer)
163
- add(Model::Shapes::Integer, Float) { |f| f.to_i }
164
- add(Model::Shapes::Integer, String) do |str|
165
- begin
166
- Integer(str)
167
- rescue ArgumentError
168
- str
169
- end
170
- end
171
-
172
- add(Model::Shapes::Float, Float)
173
- add(Model::Shapes::Float, Integer) { |i| i.to_f }
174
- add(Model::Shapes::Float, String) do |str|
175
- begin
176
- Float(str)
177
- rescue ArgumentError
178
- str
179
- end
180
- end
181
-
182
- add(Model::Shapes::Timestamp, Time)
183
- add(Model::Shapes::Timestamp, Date) { |d| d.to_time }
184
- add(Model::Shapes::Timestamp, DateTime) { |dt| dt.to_time }
185
- add(Model::Shapes::Timestamp, Integer) { |i| Time.at(i) }
186
- add(Model::Shapes::Timestamp, String) do |str|
187
- begin
188
- Time.parse(str)
189
- rescue ArgumentError
190
- str
191
- end
192
- end
193
-
194
- add(Model::Shapes::Boolean, TrueClass)
195
- add(Model::Shapes::Boolean, FalseClass)
196
- add(Model::Shapes::Boolean, String) do |str|
197
- { 'true' => true, 'false' => false }[str]
198
- end
199
-
200
- add(Model::Shapes::Blob, IO)
201
- add(Model::Shapes::Blob, Tempfile)
202
- add(Model::Shapes::Blob, StringIO)
203
- add(Model::Shapes::Blob, String) { |str| StringIO.new(str) }
204
-
205
- end
206
- end
207
- end