Class: Retab::Multipart::FilePart

Inherits:
Object
  • Object
show all
Defined in:
lib/retab/multipart.rb

Overview

A single binary file part. ‘content` may be a String of bytes, a `Pathname`, or any IO-like object (File/StringIO). Normalised to an IO so net/http streams it as a real file upload.

Constant Summary collapse

DEFAULT_CONTENT_TYPE =
"application/octet-stream"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, filename: "file", content_type: DEFAULT_CONTENT_TYPE) ⇒ FilePart

Returns a new instance of FilePart.



50
51
52
53
54
# File 'lib/retab/multipart.rb', line 50

def initialize(content, filename: "file", content_type: DEFAULT_CONTENT_TYPE)
  @content = content
  @filename = filename
  @content_type = content_type
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



48
49
50
# File 'lib/retab/multipart.rb', line 48

def content_type
  @content_type
end

#filenameObject (readonly)

Returns the value of attribute filename.



48
49
50
# File 'lib/retab/multipart.rb', line 48

def filename
  @filename
end

Instance Method Details

#form_optsObject



68
69
70
# File 'lib/retab/multipart.rb', line 68

def form_opts
  {filename: @filename, content_type: @content_type}
end

#ioObject

An IO net/http can read the bytes from. Strings are wrapped in a StringIO; Pathnames are opened in binary mode; existing IOs pass through unchanged.



59
60
61
62
63
64
65
66
# File 'lib/retab/multipart.rb', line 59

def io
  return @content if @content.respond_to?(:read)
  if defined?(Pathname) && @content.is_a?(Pathname)
    return @content.open("rb")
  end

  StringIO.new(@content.to_s)
end