Class: HTTP::FormData::Part

Inherits:
Object
  • Object
show all
Includes:
Readable
Defined in:
lib/http/form_data/part.rb

Overview

Represents a body part of multipart/form-data request.

Examples:

Usage with String


body = "Message"
FormData::Part.new body, content_type: 'foobar.txt; charset="UTF-8"'

Direct Known Subclasses

File

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Readable

#read, #rewind, #size, #to_s

Constructor Details

#initialize(body, content_type: nil, filename: nil) ⇒ Part

Creates a new Part with the given body and options

Examples:

Part.new("hello", content_type: "text/plain")

Parameters:

  • body (#to_s)
  • content_type (String) (defaults to: nil)

    Value of Content-Type header

  • filename (String) (defaults to: nil)

    Value of filename parameter



45
46
47
48
49
# File 'lib/http/form_data/part.rb', line 45

def initialize(body, content_type: nil, filename: nil)
  @io = StringIO.new(body.to_s)
  @content_type = content_type
  @filename = filename
end

Instance Attribute Details

#content_typeString? (readonly)

Returns the content type of this part

Examples:

part.content_type # => "application/json"

Returns:

  • (String, nil)


25
26
27
# File 'lib/http/form_data/part.rb', line 25

def content_type
  @content_type
end

#filenameString? (readonly)

Returns the filename of this part

Examples:

part.filename # => "avatar.png"

Returns:

  • (String, nil)


34
35
36
# File 'lib/http/form_data/part.rb', line 34

def filename
  @filename
end