Class: Nestful::Formats::MultipartFormat

Inherits:
Format
  • Object
show all
Defined in:
lib/nestful/formats/multipart_format.rb

Constant Summary collapse

EOL =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Format

#extension

Constructor Details

#initialize(*args) ⇒ MultipartFormat

Returns a new instance of MultipartFormat.



10
11
12
13
14
15
# File 'lib/nestful/formats/multipart_format.rb', line 10

def initialize(*args)
  super
  @boundary = ActiveSupport::SecureRandom.hex(10)
  @stream   = Tempfile.new("nf.#{rand(1000)}")
  @stream.binmode
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



8
9
10
# File 'lib/nestful/formats/multipart_format.rb', line 8

def boundary
  @boundary
end

#streamObject (readonly)

Returns the value of attribute stream.



8
9
10
# File 'lib/nestful/formats/multipart_format.rb', line 8

def stream
  @stream
end

Instance Method Details

#decode(body) ⇒ Object



29
30
31
# File 'lib/nestful/formats/multipart_format.rb', line 29

def decode(body)
  body
end

#encode(params, options = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/nestful/formats/multipart_format.rb', line 21

def encode(params, options = nil)
  to_multipart(params)
  stream.write("--" + boundary + "--" + EOL)
  stream.flush
  stream.rewind
  stream
end

#mime_typeObject



17
18
19
# File 'lib/nestful/formats/multipart_format.rb', line 17

def mime_type
  %Q{multipart/form-data; boundary=#{boundary}}
end