Class: Praxis::Multipart

Inherits:
Attributor::Hash
  • Object
show all
Defined in:
lib/praxis/types/multipart.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



59
60
61
# File 'lib/praxis/types/multipart.rb', line 59

def headers
  @headers
end

#partsObject

Returns the value of attribute parts.



58
59
60
# File 'lib/praxis/types/multipart.rb', line 58

def parts
  @parts
end

#preambleObject

Returns the value of attribute preamble.



57
58
59
# File 'lib/praxis/types/multipart.rb', line 57

def preamble
  @preamble
end

Class Method Details

.example(context = nil, options: {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/praxis/types/multipart.rb', line 29

def self.example(context=nil, options: {})
  form = MIME::Multipart::FormData.new

  super(context, options: 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)
  #result.each do |k, v|
  #  result.parts[k] = MultipartPart.new(v)
  #end

  #result.headers = {'Content-Type'}


end

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/praxis/types/multipart.rb', line 7

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

  hash = Hash[parts.collect { |name, part| [name, part.body] }]

  instance = super(hash, context, **options)

  instance.preamble = preamble
  instance.parts = parts
  instance.headers = headers

  instance
end

Instance Method Details

#validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object



62
63
64
# File 'lib/praxis/types/multipart.rb', line 62

def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)
  super
end