Class: Praxis::Types::MultipartArray
- Inherits:
-
Attributor::Collection
- Object
- Attributor::Collection
- Praxis::Types::MultipartArray
- Includes:
- MediaTypeCommon
- Defined in:
- lib/praxis/types/multipart_array.rb,
lib/praxis/types/multipart_array/part_definition.rb
Defined Under Namespace
Classes: PartDefinition
Class Attribute Summary collapse
-
.attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
.identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
.multiple ⇒ Object
readonly
Returns the value of attribute multiple.
-
.options ⇒ Object
readonly
Returns the value of attribute options.
Instance Attribute Summary collapse
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#preamble ⇒ Object
Returns the value of attribute preamble.
Class Method Summary collapse
- .as_json_schema(attribute_options: {}, **_other) ⇒ Object
-
.as_openapi_request_body(attribute_options: {}) ⇒ Object
Multipart request bodies are special in OPEN API schema: # Request payload type: object properties: # Request parts id: # Part 1 (string value) type: string format: uuid address: # Part2 (object) type: object properties: street: type: string city: type: string profileImage: # Part 3 (an image) type: string format: binary.
- .construct(constructor_block, _options = {}) ⇒ Object
- .constructable? ⇒ Boolean
- .describe(shallow = true, example: nil) ⇒ Object
- .describe_attributes(shallow = true, example: nil) ⇒ Object
- .dump(value, **opts) ⇒ Object
- .dump_for_openapi(example) ⇒ Object
- .example(context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
- .file(name, payload_type = nil, **opts, &block) ⇒ Object
- .inherited(klass) ⇒ Object
- .json_schema_type ⇒ Object
- .load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) ⇒ Object
- .name_type(type = nil) ⇒ Object
- .part(name, payload_type = nil, multiple: false, filename: false, **opts, &block) ⇒ Object
- .part_attribute ⇒ Object
- .payload_attribute ⇒ Object
- .payload_type(type = nil, **opts, &block) ⇒ Object
Instance Method Summary collapse
- #dump(**opts) ⇒ Object
-
#initialize(content_type: self.class.identifier.to_s) ⇒ MultipartArray
constructor
A new instance of MultipartArray.
- #part(name) ⇒ Object
- #part?(name) ⇒ Boolean
- #payload_type ⇒ Object
- #push(*parts, context: Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
- #validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
Constructor Details
#initialize(content_type: self.class.identifier.to_s) ⇒ MultipartArray
Returns a new instance of MultipartArray.
266 267 268 269 |
# File 'lib/praxis/types/multipart_array.rb', line 266 def initialize(content_type: self.class.identifier.to_s) super() self.content_type = content_type end |
Class Attribute Details
.attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
36 37 38 |
# File 'lib/praxis/types/multipart_array.rb', line 36 def attributes @attributes end |
.identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
36 37 38 |
# File 'lib/praxis/types/multipart_array.rb', line 36 def identifier @identifier end |
.multiple ⇒ Object (readonly)
Returns the value of attribute multiple.
36 37 38 |
# File 'lib/praxis/types/multipart_array.rb', line 36 def multiple @multiple end |
.options ⇒ Object (readonly)
Returns the value of attribute options.
36 37 38 |
# File 'lib/praxis/types/multipart_array.rb', line 36 def @options end |
Instance Attribute Details
#content_type ⇒ Object
Returns the value of attribute content_type.
264 265 266 |
# File 'lib/praxis/types/multipart_array.rb', line 264 def content_type @content_type end |
#preamble ⇒ Object
Returns the value of attribute preamble.
263 264 265 |
# File 'lib/praxis/types/multipart_array.rb', line 263 def preamble @preamble end |
Class Method Details
.as_json_schema(attribute_options: {}, **_other) ⇒ Object
206 207 208 |
# File 'lib/praxis/types/multipart_array.rb', line 206 def self.as_json_schema(attribute_options: {}, **_other) as_openapi_request_body(attribute_options: ) end |
.as_openapi_request_body(attribute_options: {}) ⇒ Object
Multipart request bodies are special in OPEN API schema: # Request payload type: object properties: # Request parts
id: # Part 1 (string value)
type: string
format: uuid
address: # Part2 (object)
type: object
properties:
street:
type: string
city:
type: string
profileImage: # Part 3 (an image)
type: string
format: binary
NOTE: not sure if this
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/praxis/types/multipart_array.rb', line 171 def self.as_openapi_request_body(attribute_options: {}) hash = { type: json_schema_type } opts = .merge() hash[:description] = opts[:description] if opts[:description] hash[:default] = opts[:default] if opts[:default] unless attributes.empty? props = {} encoding = {} attributes.each do |part_name, part_attribute| part_example = part_attribute.example key_to_use = part_name.is_a?(Regexp) ? part_name.source : part_name if (payload_attribute = part_attribute.[:payload_attribute]) props[key_to_use] = payload_attribute.as_json_schema(example: part_example.payload) end # { # contentType: 'fff', # headers: { # custom1: 'safd' # } next unless (headers_attribute = part_attribute.[:headers_attribute]) # Does this 'Content-Type' string check work?...can it be a symbol? what does it mean anyway? encoding[key_to_use][:contentType] = headers_attribute['Content-Type'] if headers_attribute['Content-Type'] # TODO?rethink? ...is this correct?: att a 'headers' key with some header schemas if this part have some encoding[key_to_use]['headers'] = headers_attribute.as_json_schema(example: part_example.headers) end hash[:properties] = props if props.presence hash[:encoding] = encoding unless encoding.empty? end hash end |
.construct(constructor_block, _options = {}) ⇒ Object
43 44 45 |
# File 'lib/praxis/types/multipart_array.rb', line 43 def self.construct(constructor_block, = {}) Class.new(self, &constructor_block) end |
.constructable? ⇒ Boolean
39 40 41 |
# File 'lib/praxis/types/multipart_array.rb', line 39 def self.constructable? true end |
.describe(shallow = true, example: nil) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/praxis/types/multipart_array.rb', line 210 def self.describe(shallow = true, example: nil) type_name = Attributor.type_name(self) hash = { name: type_name.gsub(Attributor::MODULE_PREFIX_REGEX, ''), family: family, id: id } hash[:example] = example if example hash[:part_name] = { type: name_type.describe(true) } unless shallow hash[:attributes] = {} if attributes.keys.any? { |name| name.is_a? ::String } hash[:pattern_attributes] = {} if attributes.keys.any? { |name| name.is_a? Regexp } if hash.key?(:attributes) || hash.key?(:pattern_attributes) describe_attributes(shallow, example: example).each do |name, sub_hash| case name when ::String hash[:attributes][name] = sub_hash when Regexp hash[:pattern_attributes][name.source] = sub_hash end end else hash[:part_payload] = { type: payload_type.describe(true) } end end hash end |
.describe_attributes(shallow = true, example: nil) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/praxis/types/multipart_array.rb', line 241 def self.describe_attributes(shallow = true, example: nil) attributes.each_with_object({}) do |(part_name, part_attribute), parts| sub_example = example.part(part_name) if example sub_example = sub_example.first if sub_example && multiple.include?(part_name) sub_hash = part_attribute.describe(shallow, example: sub_example) if ( = sub_hash.delete(:options)) sub_hash[:options] = {} sub_hash[:options][:multiple] = true if multiple.include?(part_name) if (payload_attribute = .delete :payload_attribute) && payload_attribute.[:required] sub_hash[:options][:required] = true end end sub_hash[:type] = MultipartPart.describe(shallow, example: sub_example, options: part_attribute.) parts[part_name] = sub_hash end end |
.dump(value, **opts) ⇒ Object
359 360 361 |
# File 'lib/praxis/types/multipart_array.rb', line 359 def self.dump(value, **opts) value.dump(**opts) end |
.dump_for_openapi(example) ⇒ Object
374 375 376 |
# File 'lib/praxis/types/multipart_array.rb', line 374 def self.dump_for_openapi(example) example.map { |part| MultipartPart.dump_for_openapi(part) } end |
.example(context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/praxis/types/multipart_array.rb', line 126 def self.example(context = Attributor::DEFAULT_ROOT_CONTEXT, **) example = new attributes.each do |name, attribute| next if name.is_a? Regexp sub_context = generate_subcontext(context, name) part = attribute.example(sub_context) part.name = name example.push part next unless multiple.include? name part = attribute.example(sub_context + ['2']) part.name = name example.push part end example end |
.file(name, payload_type = nil, **opts, &block) ⇒ Object
105 106 107 |
# File 'lib/praxis/types/multipart_array.rb', line 105 def self.file(name, payload_type = nil, **opts, &block) part(name, payload_type, filename: true, **opts, &block) end |
.inherited(klass) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/praxis/types/multipart_array.rb', line 16 def self.inherited(klass) super klass.instance_eval do @attributes = FuzzyHash.new @saved_blocks = [] @multiple = [] @options = {} @payload_type = Attributor::String @name_type = Attributor::String @member_type = Praxis::MultipartPart @payload_attribute = nil @part_attribute = nil @identifier = MediaTypeIdentifier.load('multipart/form-data').freeze end end |
.json_schema_type ⇒ Object
148 149 150 |
# File 'lib/praxis/types/multipart_array.rb', line 148 def self.json_schema_type :object end |
.load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/praxis/types/multipart_array.rb', line 109 def self.load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) return value if value.is_a?(self) || value.nil? raise ArgumentError, "content_type is required to load values of type String for #{Attributor.type_name(self)}" if value.is_a?(::String) && content_type.nil? parser = Praxis::MultipartParser.new({ 'Content-Type' => content_type }, value) preamble, parts = parser.parse instance = new instance.push(*parts) instance.preamble = preamble instance.content_type = content_type instance end |
.name_type(type = nil) ⇒ Object
47 48 49 50 51 |
# File 'lib/praxis/types/multipart_array.rb', line 47 def self.name_type(type = nil) return @name_type if type.nil? @name_type = Attributor.resolve_type type end |
.part(name, payload_type = nil, multiple: false, filename: false, **opts, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/praxis/types/multipart_array.rb', line 73 def self.part(name, payload_type = nil, multiple: false, filename: false, **opts, &block) @attributes.default_proc = nil if name.is_a?(Regexp) raise 'part with regexp name may not take :multiple option' if multiple raise 'part with regexp name may not be required' if opts[:required] == true end self.multiple << name if multiple compiler = Attributor::DSLCompiler.new(self, **opts) filename_attribute = compiler.define('filename', String, required: true) if filename if block_given? definition = PartDefinition.new(&block) payload_attribute = definition.payload_attribute header_attribute = definition.headers_attribute filename_attribute = definition.filename_attribute || filename_attribute attributes[name] = compiler.define(name, Praxis::MultipartPart, payload_attribute: payload_attribute, headers_attribute: header_attribute, filename_attribute: filename_attribute) else payload_attribute = compiler.define(name, payload_type || self.payload_type, **opts) attributes[name] = compiler.define(name, Praxis::MultipartPart, payload_attribute: payload_attribute, filename_attribute: filename_attribute) end end |
.part_attribute ⇒ Object
69 70 71 |
# File 'lib/praxis/types/multipart_array.rb', line 69 def self.part_attribute @part_attribute ||= Attributor::Attribute.new(Praxis::MultipartPart, payload_attribute: payload_attribute) end |
.payload_attribute ⇒ Object
65 66 67 |
# File 'lib/praxis/types/multipart_array.rb', line 65 def self.payload_attribute @payload_attribute ||= Attributor::Attribute.new(@payload_type) end |
.payload_type(type = nil, **opts, &block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/praxis/types/multipart_array.rb', line 53 def self.payload_type(type = nil, **opts, &block) if type.nil? return @payload_type unless block_given? type = Attributor::Struct end @payload_type = Attributor.resolve_type(type) @payload_attribute = Attributor::Attribute.new(@payload_type, **opts, &block) @part_attribute = nil @payload_type end |
Instance Method Details
#dump(**opts) ⇒ Object
363 364 365 366 367 368 369 370 371 372 |
# File 'lib/praxis/types/multipart_array.rb', line 363 def dump(**opts) boundary = content_type.parameters.get 'boundary' parts = collect do |part| part.dump(**opts) end all_entities = parts.join("\r\n--#{boundary}\r\n") "--#{boundary}\r\n#{all_entities}\r\n--#{boundary}--\r\n" end |
#part(name) ⇒ Object
319 320 321 322 323 324 325 |
# File 'lib/praxis/types/multipart_array.rb', line 319 def part(name) if self.class.multiple.include?(name) self.select { |i| i.name == name } else find { |i| i.name == name } end end |
#part?(name) ⇒ Boolean
327 328 329 |
# File 'lib/praxis/types/multipart_array.rb', line 327 def part?(name) any? { |i| i.name == name } end |
#payload_type ⇒ Object
277 278 279 |
# File 'lib/praxis/types/multipart_array.rb', line 277 def payload_type self.class.payload_type end |
#push(*parts, context: Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/praxis/types/multipart_array.rb', line 281 def push(*parts, context: Attributor::DEFAULT_ROOT_CONTEXT) part, *rest = parts return push(part, context: context).push(*rest, context: context) if rest.any? original_context = context part.name = self.class.name_type.load(part.name, self.class.generate_subcontext(context, part.name)) key = part.name context = self.class.generate_subcontext(context, key) # If no attributes are defined, we always use the default # payload_attribute, otherwise we constrain the parts # to the defined names. attribute = if self.class.attributes.empty? self.class.part_attribute elsif (default_thingy = self.class.attributes[key]) default_thingy end if attribute part.attribute = attribute part.load_payload(context + ['payload']) part.load_headers(context + ['headers']) return self << part elsif self.class.[:case_insensitive_load] name = self.class.attributes.keys.find do |k| k.is_a?(::String) && key.downcase == k.downcase end if name part.name = name return push(part, context: original_context) end end raise Attributor::AttributorException, "Unknown part name received: #{key.inspect} while loading #{Attributor.humanize_context(context)}" end |
#validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/praxis/types/multipart_array.rb', line 331 def validate(context = Attributor::DEFAULT_ROOT_CONTEXT) errors = each_with_index.each_with_object([]) do |(part, idx), error_list| sub_context = if part.name self.class.generate_subcontext(context, part.name) else context + ["at(#{idx})"] end error_list.push(*part.validate(sub_context)) end self.class.attributes.each do |name, attribute| payload_attribute = attribute.[:payload_attribute] if !part?(name) if payload_attribute.[:required] sub_context = self.class.generate_subcontext(context, name) errors.push "Attribute #{Attributor.humanize_context(sub_context)} is required" end # Return, don't bother checking nullability as it hasn't been provided elsif !self.part(name) && !Attribute.nullable_attribute?(payload_attribute.) sub_context = self.class.generate_subcontext(context, name) errors.push "Attribute #{Attributor.humanize_context(sub_context)} is not nullable" end end errors end |