Class: Square::FileParam
- Inherits:
-
Object
- Object
- Square::FileParam
- Defined in:
- lib/square/file_param.rb
Overview
FileParam is a utility class for handling files in multipart form data.
Instance Attribute Summary collapse
-
#content_type ⇒ String?
readonly
The content type.
-
#filename ⇒ String?
readonly
The filename.
-
#io ⇒ IO
readonly
The readable object.
Class Method Summary collapse
-
.from_filepath(filepath:, filename: nil, content_type: nil) ⇒ FileParam
Creates a FileParam instance from a filepath.
-
.from_string(content:, filename:, content_type: nil) ⇒ FileParam
Creates a FileParam instance from a string.
Instance Method Summary collapse
-
#close ⇒ nil
Closes the file IO if it responds to close.
-
#initialize(io:, filename: nil, content_type: nil) ⇒ FileParam
constructor
Create a new file parameter.
-
#to_form_data_part(name:, content_type: nil) ⇒ Square::Internal::Multipart::FormDataPart
Maps this FileParam to a FormDataPart.
Constructor Details
#initialize(io:, filename: nil, content_type: nil) ⇒ FileParam
Create a new file parameter.
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_type ⇒ String? (readonly)
The content type
9 10 11 |
# File 'lib/square/file_param.rb', line 9 def content_type @content_type end |
#filename ⇒ String? (readonly)
The filename
9 10 11 |
# File 'lib/square/file_param.rb', line 9 def filename @filename end |
#io ⇒ IO (readonly)
The readable object
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.
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.}" 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.
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
#close ⇒ nil
Closes the file IO if it responds to close.
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.
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 |