seahorse 0.1.0 → 1.0.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +12 -0
  3. data/VERSION +1 -0
  4. data/lib/seahorse/api_error.rb +15 -0
  5. data/lib/seahorse/block_io.rb +24 -0
  6. data/lib/seahorse/context.rb +34 -0
  7. data/lib/seahorse/http/api_error.rb +29 -0
  8. data/lib/seahorse/http/client.rb +152 -0
  9. data/lib/seahorse/http/error_parser.rb +105 -0
  10. data/lib/seahorse/http/headers.rb +70 -0
  11. data/lib/seahorse/http/middleware/content_length.rb +28 -0
  12. data/lib/seahorse/http/networking_error.rb +20 -0
  13. data/lib/seahorse/http/request.rb +132 -0
  14. data/lib/seahorse/http/response.rb +29 -0
  15. data/lib/seahorse/http.rb +36 -0
  16. data/lib/seahorse/json/parse_error.rb +18 -0
  17. data/lib/seahorse/json.rb +30 -0
  18. data/lib/seahorse/middleware/around_handler.rb +24 -0
  19. data/lib/seahorse/middleware/build.rb +26 -0
  20. data/lib/seahorse/middleware/host_prefix.rb +48 -0
  21. data/lib/seahorse/middleware/parse.rb +42 -0
  22. data/lib/seahorse/middleware/request_handler.rb +24 -0
  23. data/lib/seahorse/middleware/response_handler.rb +25 -0
  24. data/lib/seahorse/middleware/retry.rb +43 -0
  25. data/lib/seahorse/middleware/send.rb +62 -0
  26. data/lib/seahorse/middleware/validate.rb +29 -0
  27. data/lib/seahorse/middleware.rb +16 -0
  28. data/lib/seahorse/middleware_builder.rb +246 -0
  29. data/lib/seahorse/middleware_stack.rb +73 -0
  30. data/lib/seahorse/output.rb +20 -0
  31. data/lib/seahorse/structure.rb +40 -0
  32. data/lib/seahorse/stubbing/client_stubs.rb +115 -0
  33. data/lib/seahorse/stubbing/stubs.rb +32 -0
  34. data/lib/seahorse/time_helper.rb +35 -0
  35. data/lib/seahorse/union.rb +10 -0
  36. data/lib/seahorse/validator.rb +20 -0
  37. data/lib/seahorse/waiters/errors.rb +15 -0
  38. data/lib/seahorse/waiters/poller.rb +132 -0
  39. data/lib/seahorse/waiters/waiter.rb +79 -0
  40. data/lib/seahorse/xml/formatter.rb +68 -0
  41. data/lib/seahorse/xml/node.rb +123 -0
  42. data/lib/seahorse/xml/parse_error.rb +18 -0
  43. data/lib/seahorse/xml.rb +58 -0
  44. data/lib/seahorse.rb +23 -13
  45. data/sig/lib/seahorse/api_error.rbs +10 -0
  46. data/sig/lib/seahorse/document.rbs +2 -0
  47. data/sig/lib/seahorse/http/api_error.rbs +21 -0
  48. data/sig/lib/seahorse/http/headers.rbs +47 -0
  49. data/sig/lib/seahorse/http/response.rbs +21 -0
  50. data/sig/lib/seahorse/simple_delegator.rbs +3 -0
  51. data/sig/lib/seahorse/structure.rbs +18 -0
  52. data/sig/lib/seahorse/stubbing/client_stubs.rbs +103 -0
  53. data/sig/lib/seahorse/stubbing/stubs.rbs +14 -0
  54. data/sig/lib/seahorse/union.rbs +6 -0
  55. metadata +73 -54
  56. data/LICENSE +0 -12
  57. data/README.md +0 -3
  58. data/Rakefile +0 -7
  59. data/lib/seahorse/api_translator/inflector.rb +0 -37
  60. data/lib/seahorse/api_translator/operation.rb +0 -150
  61. data/lib/seahorse/api_translator/shape.rb +0 -235
  62. data/lib/seahorse/controller.rb +0 -87
  63. data/lib/seahorse/model.rb +0 -82
  64. data/lib/seahorse/operation.rb +0 -66
  65. data/lib/seahorse/param_validator.rb +0 -158
  66. data/lib/seahorse/railtie.rb +0 -7
  67. data/lib/seahorse/router.rb +0 -20
  68. data/lib/seahorse/shape_builder.rb +0 -84
  69. data/lib/seahorse/type.rb +0 -220
  70. data/lib/seahorse/version.rb +0 -3
  71. data/lib/tasks/seahorse_tasks.rake +0 -24
@@ -1,158 +0,0 @@
1
- # Copyright 2011-2013 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 'date'
15
- require 'time'
16
- require 'pathname'
17
-
18
- module Seahorse
19
- # @api private
20
- class ParamValidator
21
-
22
- # @param [Hash] rules
23
- def initialize rules
24
- @rules = (rules || {})['members'] || {}
25
- end
26
-
27
- # @param [Hash] params A hash of request params.
28
- # @raise [ArgumentError] Raises an `ArgumentError` if any of the given
29
- # request parameters are invalid.
30
- # @return [Boolean] Returns `true` if the `params` are valid.
31
- def validate! params
32
- validate_structure(@rules, params || {})
33
- true
34
- end
35
-
36
- private
37
-
38
- def validate_structure rules, params, context = "params"
39
- # require params to be a hash
40
- unless params.is_a?(Hash)
41
- raise ArgumentError, "expected a hash for #{context}"
42
- end
43
-
44
- # check for missing required params
45
- rules.each_pair do |param_name, rule|
46
- if rule['required']
47
- unless params.key?(param_name) or params.key?(param_name.to_sym)
48
- msg = "missing required option :#{param_name} in #{context}"
49
- raise ArgumentError, msg
50
- end
51
- end
52
- end
53
-
54
- # validate hash members
55
- params.each_pair do |param_name, param_value|
56
- if param_rules = rules[param_name.to_s]
57
- member_context = "#{context}[#{param_name.inspect}]"
58
- validate_member(param_rules, param_value, member_context)
59
- else
60
- msg = "unexpected option #{param_name.inspect} found in #{context}"
61
- raise ArgumentError, msg
62
- end
63
- end
64
- end
65
-
66
- def validate_list rules, params, context
67
- # require an array
68
- unless params.is_a?(Array)
69
- raise ArgumentError, "expected an array for #{context}"
70
- end
71
- # validate array members
72
- params.each_with_index do |param_value,n|
73
- validate_member(rules, param_value, context + "[#{n}]")
74
- end
75
- end
76
-
77
- def validate_map rules, params, context
78
- # require params to be a hash
79
- unless params.is_a?(Hash)
80
- raise ArgumentError, "expected a hash for #{context}"
81
- end
82
- # validate hash keys and members
83
- params.each_pair do |key,param_value|
84
- unless key.is_a?(String)
85
- msg = "expected hash keys for #{context} to be strings"
86
- raise ArgumentError, msg
87
- end
88
- validate_member(rules, param_value, context + "[#{key.inspect}]")
89
- end
90
- end
91
-
92
- def validate_member rules, param, context
93
- member_rules = rules['members'] || {}
94
- case rules['type']
95
- when 'structure' then validate_structure(member_rules, param, context)
96
- when 'list' then validate_list(member_rules, param, context)
97
- when 'map' then validate_map(member_rules, param, context)
98
- else validate_scalar(rules, param, context)
99
- end
100
- end
101
-
102
- def validate_scalar rules, param, context
103
- case rules['type']
104
- when 'string', nil
105
- unless param.respond_to?(:to_str)
106
- raise ArgumentError, "expected #{context} to be a string"
107
- end
108
- when 'integer'
109
- unless param.respond_to?(:to_int)
110
- raise ArgumentError, "expected #{context} to be an integer"
111
- end
112
- when 'timestamp'
113
- case param
114
- when Time, DateTime, Date, Integer
115
- when /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/
116
- else
117
- msg = "expected #{context} to be a Time/DateTime/Date object, "
118
- msg << "an integer or an iso8601 string"
119
- raise ArgumentError, msg
120
- end
121
- when 'boolean'
122
- unless [true,false].include?(param)
123
- raise ArgumentError, "expected #{context} to be a boolean"
124
- end
125
- when 'float'
126
- unless param.is_a?(Numeric)
127
- raise ArgumentError, "expected #{context} to be a Numeric (float)"
128
- end
129
- when 'base64', 'binary'
130
- unless
131
- param.is_a?(String) or
132
- (param.respond_to?(:read) and param.respond_to?(:rewind)) or
133
- param.is_a?(Pathname)
134
- then
135
- msg = "expected #{context} to be a string, an IO object or a "
136
- msg << "Pathname object"
137
- raise ArgumentError, msg
138
- end
139
- else
140
- raise ArgumentError, "unhandled type `#{rules['type']}' for #{context}"
141
- end
142
- end
143
-
144
- class << self
145
-
146
- # @param [Hash] rules
147
- # @param [Hash] params
148
- # @raise [ArgumentError] Raises an `ArgumentError` when one or more
149
- # of the request parameters are invalid.
150
- # @return [Boolean] Returns `true` when params are valid.
151
- def validate! rules, params
152
- ParamValidator.new(rules).validate!(params)
153
- end
154
-
155
- end
156
-
157
- end
158
- end
@@ -1,7 +0,0 @@
1
- module Seahorse
2
- class Railtie < Rails::Railtie
3
- rake_tasks do
4
- load File.dirname(__FILE__) + '/../tasks/seahorse_tasks.rake'
5
- end
6
- end
7
- end
@@ -1,20 +0,0 @@
1
- module Seahorse
2
- class Router
3
- def initialize(model)
4
- @model = model
5
- end
6
-
7
- def add_routes(router)
8
- operations = @model.operations
9
- controller = @model.model_name.pluralize
10
- operations.each do |name, operation|
11
- router.match "/#{name}" => "#{controller}##{operation.action}",
12
- defaults: { format: 'json' },
13
- via: [:get, operation.verb.to_sym].uniq
14
- router.match operation.url => "#{controller}##{operation.action}",
15
- defaults: { format: 'json' },
16
- via: operation.verb
17
- end
18
- end
19
- end
20
- end
@@ -1,84 +0,0 @@
1
- require_relative './type'
2
-
3
- module Seahorse
4
- class ShapeBuilder
5
- def self.build_default_types
6
- hash = HashWithIndifferentAccess.new
7
- hash.update string: [StringType, nil],
8
- timestamp: [TimestampType, nil],
9
- integer: [IntegerType, nil],
10
- boolean: [BooleanType, nil],
11
- list: [ListType, nil],
12
- structure: [StructureType, nil]
13
- hash
14
- end
15
-
16
- def self.type(type, supertype = 'structure', &block)
17
- klass = Class.new(type_class_for(supertype))
18
- klass.type = type
19
- @@types[type] = [klass, block]
20
- end
21
-
22
- def self.type_class_for(type)
23
- @@types[type] ? @@types[type][0] : nil
24
- end
25
-
26
- def initialize(context)
27
- @context = context
28
- @desc = nil
29
- end
30
-
31
- def build(&block)
32
- init_blocks = []
33
- init_blocks << block if block_given?
34
-
35
- # collect the init block for this type and all of its super types
36
- klass = @context.class
37
- while klass != Type
38
- if block = @@types[klass.type][1]
39
- init_blocks << block
40
- end
41
- klass = klass.superclass
42
- end
43
-
44
- init_blocks.reverse.each do |init_block|
45
- instance_eval(&init_block)
46
- end
47
- end
48
-
49
- def method_missing(type, *args, &block)
50
- if @@types[type]
51
- send_type(type, *args, &block)
52
- else
53
- super
54
- end
55
- end
56
-
57
- def desc(text)
58
- @desc = text
59
- end
60
-
61
- def model(model)
62
- @context.model = model
63
- end
64
-
65
- private
66
-
67
- def send_type(type, *args, &block)
68
- klass, init_block = *@@types[type.to_s]
69
- shape = klass.new(*args)
70
- shape.documentation = @desc
71
- @context.add(shape)
72
- if init_block || block
73
- old_context, @context = @context, shape
74
- instance_eval(&init_block) if init_block
75
- instance_eval(&block) if block
76
- @context = old_context
77
- end
78
- @desc = nil
79
- true
80
- end
81
-
82
- @@types = build_default_types
83
- end
84
- end
data/lib/seahorse/type.rb DELETED
@@ -1,220 +0,0 @@
1
- module Seahorse
2
- class Type
3
- attr_accessor :name, :required, :model, :location, :header, :uri, :as
4
- attr_accessor :documentation
5
-
6
- def self.type; @type || name.to_s.underscore.gsub(/_type$|^.+\//, '') end
7
- def self.type=(v) @type = v end
8
- def self.inspect; "Type(#{type})" end
9
-
10
- def initialize(*args)
11
- name, opts = nil, {}
12
- if args.size == 0
13
- name = type
14
- elsif args.size == 2
15
- name, opts = args.first, args.last
16
- elsif Hash === args.first
17
- opts = args.first
18
- else
19
- name = args.first
20
- end
21
-
22
- self.name = name.to_s
23
- opts.each {|k, v| send("#{k}=", v) }
24
- end
25
-
26
- def inspect
27
- variables = instance_variables.map do |v|
28
- next if v.to_s =~ /^@(?:(?:default_)?type|name|model)$/
29
- [v.to_s[1..-1], instance_variable_get(v).inspect].join('=')
30
- end.compact.join(' ')
31
- variables = ' ' + variables if variables.length > 0
32
- "#<Type(#{type})#{variables}>"
33
- end
34
-
35
- def type
36
- @type ||= self.class.type.to_s
37
- end
38
-
39
- def default_type
40
- klass = self.class
41
- last_klass = nil
42
- while klass != Type
43
- last_klass = klass
44
- klass = klass.superclass
45
- end
46
- @default_type ||= last_klass.type
47
- end
48
-
49
- def complex?; false end
50
-
51
- def add(shape)
52
- raise NotImplementedError, "Cannot add #{shape.inspect} to #{type}"
53
- end
54
-
55
- def to_hash
56
- hash = {'type' => default_type}
57
- hash['required'] = true if required
58
- hash['location'] = location if location
59
- hash['location'] = 'uri' if uri
60
- if header
61
- header_name = header == true ? name : header
62
- hash['location'] = 'header'
63
- hash['location_name'] = header_name
64
- hash['name'] = header_name
65
- end
66
- hash['documentation'] = documentation if documentation
67
- hash
68
- end
69
-
70
- def from_input(data, filter = true) data end
71
- def to_output(data) pull_value(data).to_s end
72
- def to_strong_params; name.to_s end
73
-
74
- def pull_value(value)
75
- found = false
76
- names = as ? as : name
77
- if names
78
- names = [names].flatten
79
- names.each do |name|
80
- if value.respond_to?(name)
81
- value = value.send(name)
82
- found = true
83
- elsif Hash === value
84
- if value.has_key?(name)
85
- value = value[name]
86
- found = true
87
- end
88
- else
89
- raise ArgumentError, "no property `#{name}' while looking for " +
90
- "`#{names.join('.')}' on #{value.inspect}"
91
- end
92
- end
93
- end
94
- found ? value : nil
95
- end
96
- end
97
-
98
- class StringType < Type
99
- def from_input(data, filter = true) data.to_s end
100
- end
101
-
102
- class TimestampType < Type
103
- def from_input(data, filter = true)
104
- String === data ? Time.parse(data) : data
105
- end
106
- end
107
-
108
- class IntegerType < Type
109
- def from_input(data, filter = true) Integer(data) end
110
- def to_output(data) (value = pull_value(data)) ? value.to_i : nil end
111
- end
112
-
113
- class BooleanType < Type
114
- def from_input(data, filter = true) !!pull_value(data) end
115
- end
116
-
117
- class ListType < Type
118
- attr_accessor :collection
119
-
120
- def initialize(*args)
121
- super
122
- end
123
-
124
- def complex?; true end
125
-
126
- def add(shape)
127
- self.collection = shape
128
- end
129
-
130
- def to_hash
131
- hash = super
132
- hash['members'] = collection ? collection.to_hash : {}
133
- hash
134
- end
135
-
136
- def from_input(data, filter = true)
137
- data.each_with_index {|v, i| data[i] = collection.from_input(v, filter) }
138
- data
139
- end
140
-
141
- def to_output(data)
142
- pull_value(data).map {|v| collection.to_output(v) }
143
- end
144
-
145
- def to_strong_params
146
- collection.complex? ? collection.to_strong_params : []
147
- end
148
- end
149
-
150
- class StructureType < Type
151
- attr_accessor :members
152
-
153
- def initialize(*args)
154
- @members = {}
155
- super
156
- end
157
-
158
- def complex?; true end
159
-
160
- def add(shape)
161
- members[shape.name.to_s] = shape
162
- end
163
-
164
- def to_hash
165
- hash = super
166
- hash['members'] = members.inject({}) do |hsh, (k, v)|
167
- hsh[k.to_s] = v.to_hash
168
- hsh
169
- end
170
- hash
171
- end
172
-
173
- def from_input(data, filter = true)
174
- return nil unless data
175
- data.dup.each do |name, value|
176
- if members[name]
177
- if filter && members[name].type == 'list' && members[name].collection.model &&
178
- ActiveRecord::Base > members[name].collection.model
179
- then
180
- data.delete(name)
181
- data[name + '_attributes'] = members[name].from_input(value, filter)
182
- else
183
- data[name] = members[name].from_input(value, filter)
184
- end
185
- elsif filter
186
- data.delete(name)
187
- end
188
- end
189
- data
190
- end
191
-
192
- def to_output(data)
193
- if Hash === data
194
- data = data.with_indifferent_access unless HashWithIndifferentAccess === data
195
- end
196
-
197
- members.inject({}) do |hsh, (name, member)|
198
- value = member.to_output(data)
199
- hsh[name] = value if value
200
- hsh
201
- end
202
- end
203
-
204
- def to_strong_params
205
- members.map do |name, member|
206
- if member.complex?
207
- if member.type == 'list' && member.collection.model &&
208
- ActiveRecord::Base > member.collection.model
209
- then
210
- name += '_attributes'
211
- end
212
-
213
- {name => member.to_strong_params}
214
- else
215
- name
216
- end
217
- end
218
- end
219
- end
220
- end
@@ -1,3 +0,0 @@
1
- module Seahorse
2
- VERSION = "0.1.0"
3
- end
@@ -1,24 +0,0 @@
1
- require 'oj'
2
-
3
- namespace :seahorse do
4
- desc 'Builds API clients'
5
- task :api => :environment do
6
- filename = 'service.json'
7
- service = {
8
- 'format' => 'rest-json',
9
- 'type' => 'rest-json',
10
- 'endpoint_prefix' => '',
11
- 'operations' => {}
12
- }
13
-
14
- Dir.glob("#{Rails.root}/app/models/api/*.rb").each {|f| load f }
15
- Seahorse::Model.apis.values.each do |api|
16
- service['operations'].update(api.to_hash['operations'])
17
- end
18
-
19
- File.open(filename, 'w') do |f|
20
- f.puts(Oj.dump(service, indent: 2))
21
- end
22
- puts "Wrote service description: #{filename}"
23
- end
24
- end