Class: Parts::ParamPart

Inherits:
Object
  • Object
show all
Includes:
Part
Defined in:
lib/parts.rb

Overview

Represents a parametric part to be filled with given value.

Instance Method Summary collapse

Methods included from Part

file?, new, #to_io

Constructor Details

#initialize(boundary, name, value, headers = {}) ⇒ ParamPart

Returns a new instance of ParamPart.

Parameters:

  • boundary (String)
  • name (#to_s)
  • value (String)
  • headers (Hash) (defaults to: {})

    Content-Type is used, if present.



39
40
41
42
# File 'lib/parts.rb', line 39

def initialize(boundary, name, value, headers = {})
  @part = build_part(boundary, name, value, headers)
  @io = StringIO.new(@part)
end

Instance Method Details

#build_part(boundary, name, value, headers = {}) ⇒ Object

Parameters:

  • boundary (String)
  • name (#to_s)
  • value (String)
  • headers (Hash) (defaults to: {})

    Content-Type is used, if present.



52
53
54
55
56
57
58
59
# File 'lib/parts.rb', line 52

def build_part(boundary, name, value, headers = {})
  part = ''
  part << "--#{boundary}\r\n"
  part << "Content-Disposition: form-data; name=\"#{name.to_s}\"\r\n"
  part << "Content-Type: #{headers["Content-Type"]}\r\n" if headers["Content-Type"]
  part << "\r\n"
  part << "#{value}\r\n"
end

#lengthObject



44
45
46
# File 'lib/parts.rb', line 44

def length
 @part.bytesize
end