Class: Square::FileParam

Inherits:
Object
  • Object
show all
Defined in:
lib/square/file_param.rb

Overview

FileParam is a utility class for handling files in multipart form data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io:, filename: nil, content_type: nil) ⇒ FileParam

Create a new file parameter.

Parameters:

  • io (#read)

    A readable object (File, StringIO, etc.)

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

    Optional filename

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

    Optional content type



17
18
19
20
21
# File 'lib/square/file_param.rb', line 17

def initialize(io:, filename: nil, content_type: nil)
  @io = io
  @filename = filename
  @content_type = content_type
end

Instance Attribute Details

#content_typeString? (readonly)

The content type

Returns:

  • (String, nil)

    the current value of content_type



9
10
11
# File 'lib/square/file_param.rb', line 9

def content_type
  @content_type
end

#filenameString? (readonly)

The filename

Returns:

  • (String, nil)

    the current value of filename



9
10
11
# File 'lib/square/file_param.rb', line 9

def filename
  @filename
end

#ioIO (readonly)

The readable object

Returns:

  • (IO)

    the current value of io



9
10
11
# File 'lib/square/file_param.rb', line 9

def io
  @io
end

Class Method Details

.from_filepath(filepath:, filename: nil, content_type: nil) ⇒ FileParam

Creates a FileParam instance from a filepath.

Parameters:

  • filepath (String)

    Path to the file

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

    Optional filename (defaults to basename of filepath)

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

    Optional content type

Returns:

Raises:

  • (StandardError)

    If the file cannot be opened or read



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/square/file_param.rb', line 30

def self.from_filepath(filepath:, filename: nil, content_type: nil)
  begin
    file = File.open(filepath, "rb")
  rescue StandardError => e
    raise "Unable to open file #{filepath}: #{e.message}"
  end

  new(
    io: file,
    filename: filename || File.basename(filepath),
    content_type: content_type
  )
end

.from_string(content:, filename:, content_type: nil) ⇒ FileParam

Creates a FileParam instance from a string.

Parameters:

  • content (String)

    The content string

  • filename (String, nil)

    Required filename

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

    Optional content type

Returns:



50
51
52
53
54
55
56
# File 'lib/square/file_param.rb', line 50

def self.from_string(content:, filename:, content_type: nil)
  new(
    io: StringIO.new(content),
    filename: filename,
    content_type: content_type
  )
end

Instance Method Details

#closenil

Closes the file IO if it responds to close.

Returns:

  • (nil)


78
79
80
81
# File 'lib/square/file_param.rb', line 78

def close
  @io.close if @io.respond_to?(:close)
  nil
end

#to_form_data_part(name:, content_type: nil) ⇒ Square::Internal::Multipart::FormDataPart

Maps this FileParam to a FormDataPart.

Parameters:

  • name (String)

    The name of the form field

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

    Overrides the content type, if provided

Returns:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/square/file_param.rb', line 63

def to_form_data_part(name:, content_type: nil)
  content_type ||= @content_type
  headers = content_type ? { "Content-Type" => content_type } : nil

  Square::Internal::Multipart::FormDataPart.new(
    name: name,
    value: @io,
    filename: @filename,
    headers: headers
  )
end