Class: Praxis::Multipart
- Inherits:
-
Attributor::Hash
- Object
- Attributor::Hash
- Praxis::Multipart
- Defined in:
- lib/praxis/types/multipart.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#parts ⇒ Object
Returns the value of attribute parts.
-
#preamble ⇒ Object
Returns the value of attribute preamble.
Class Method Summary collapse
- .backconvert_part_data(part) ⇒ Object
- .describe(shallow = false, **opts) ⇒ Object
- .example(context = nil, options: {}) ⇒ Object
- .inherited(klass) ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
94 95 96 |
# File 'lib/praxis/types/multipart.rb', line 94 def headers @headers end |
#parts ⇒ Object
Returns the value of attribute parts.
93 94 95 |
# File 'lib/praxis/types/multipart.rb', line 93 def parts @parts end |
#preamble ⇒ Object
Returns the value of attribute preamble.
92 93 94 |
# File 'lib/praxis/types/multipart.rb', line 92 def preamble @preamble end |
Class Method Details
.backconvert_part_data(part) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/praxis/types/multipart.rb', line 37 def self.backconvert_part_data(part) filename = part.filename body = part.payload name = part.name content_type = part.headers['Content-Type'] # cheat and unparse the headers back to a head string. should be ok... head = part.headers.collect {|k,v| "#{k}: #{v}" }.join("\r\n") data = nil if filename == "" # filename is blank which means no file has been selected return data elsif filename body.rewind data = {:filename => filename, :type => content_type, :name => name, :tempfile => body, :head => head} elsif !filename && content_type && body.is_a?(IO) body.rewind # Generic multipart cases, not coming from a form data = {:type => content_type, :name => name, :tempfile => body, :head => head} else data = body end part.payload = data part end |
.describe(shallow = false, **opts) ⇒ Object
101 102 103 104 105 |
# File 'lib/praxis/types/multipart.rb', line 101 def self.describe(shallow = false, **opts) hash = super(**opts) hash.merge!(family: 'multipart') hash end |
.example(context = nil, options: {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/praxis/types/multipart.rb', line 71 def self.example(context=nil, options: {}) form = MIME::Multipart::FormData.new super(context, options: ).each do |k,v| body = if v.respond_to?(:dump) && !v.kind_of?(String) JSON.pretty_generate(v.dump) else v end entity = MIME::Text.new(body) form.add entity, String(k) end content_type = form.headers.get('Content-Type') body = form.body.to_s self.load(body, context, content_type: content_type) end |
.inherited(klass) ⇒ Object
6 7 8 9 |
# File 'lib/praxis/types/multipart.rb', line 6 def self.inherited(klass) warn "DEPRECATION: Praxis::Multipart is deprecated and will be removed in 1.0. Use Praxis::Types::MultipartArray instead" super end |
.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/praxis/types/multipart.rb', line 11 def self.load(value, context=Attributor::DEFAULT_ROOT_CONTEXT, content_type:nil) return value if value.kind_of?(self) || value.nil? unless (value.kind_of?(::String) && ! content_type.nil?) raise Attributor::CoercionError, context: context, from: value.class, to: self.name, value: value end headers = {'Content-Type' => content_type} parser = MultipartParser.new(headers, value) preamble, parts = parser.parse parts_hash = parts.each_with_object({}) do |part, hash| hash[part.name] = self.backconvert_part_data(part) end hash = Hash[parts_hash.collect { |name, part| [name, part.body] }] instance = super(hash, context, **) instance.preamble = preamble instance.parts = parts_hash instance.headers = headers instance end |
Instance Method Details
#validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
97 98 99 |
# File 'lib/praxis/types/multipart.rb', line 97 def validate(context=Attributor::DEFAULT_ROOT_CONTEXT) super end |