Module: Philiprehberger::Multipart

Defined in:
lib/philiprehberger/multipart.rb,
lib/philiprehberger/multipart/part.rb,
lib/philiprehberger/multipart/parser.rb,
lib/philiprehberger/multipart/builder.rb,
lib/philiprehberger/multipart/version.rb,
lib/philiprehberger/multipart/mime_types.rb

Defined Under Namespace

Modules: MimeTypes Classes: Builder, Error, Parser, Part

Constant Summary collapse

VERSION =
'0.4.0'

Class Method Summary collapse

Class Method Details

.build(boundary: nil) {|builder| ... } ⇒ Builder

Build a multipart/form-data body using a DSL block

Parameters:

  • boundary (String, nil) (defaults to: nil)

    optional custom boundary string

Yields:

  • (builder)

    the builder instance for adding fields and files

Yield Parameters:

Returns:

  • (Builder)

    the configured builder

Raises:

  • (Error)

    if no block is given



21
22
23
24
25
26
27
# File 'lib/philiprehberger/multipart.rb', line 21

def self.build(boundary: nil, &block)
  raise Error, 'A block is required' unless block

  builder = Builder.new(boundary: boundary)
  builder.instance_eval(&block)
  builder
end

.parse(body, content_type:) ⇒ Array<Part>

Parse a multipart/form-data body into Part objects

Parameters:

  • body (String)

    the raw multipart body

  • content_type (String)

    the Content-Type header value (must include boundary)

Returns:

  • (Array<Part>)

    parsed parts

Raises:

  • (Error)

    if the boundary cannot be extracted or the body is malformed



35
36
37
# File 'lib/philiprehberger/multipart.rb', line 35

def self.parse(body, content_type:)
  Parser.parse(body, content_type: content_type)
end