Class: GRApiManager::FilePayload
- Inherits:
-
Object
- Object
- GRApiManager::FilePayload
- Defined in:
- lib/gr_api_manager.rb
Overview
FilePayload — wraps an uploaded file (multipart or raw) with a clean API.
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#tempfile ⇒ Object
readonly
Returns the value of attribute tempfile.
Instance Method Summary collapse
-
#extension ⇒ Object
Convenience: the file extension derived from the original filename.
-
#initialize(tempfile:, filename:, content_type:) ⇒ FilePayload
constructor
A new instance of FilePayload.
- #inspect ⇒ Object
-
#read ⇒ Object
Returns the raw binary content of the file as a String (encoding: BINARY).
-
#save_to(dest_path) ⇒ Object
Saves the uploaded content to dest_path on disk.
-
#to_base64 ⇒ Object
Returns the file content encoded as a Base64 string (no newlines).
-
#to_h ⇒ Object
Human-friendly summary — safe to include in JSON responses.
-
#to_hex ⇒ Object
Returns the file content encoded as a lowercase hexadecimal string.
Constructor Details
#initialize(tempfile:, filename:, content_type:) ⇒ FilePayload
Returns a new instance of FilePayload.
302 303 304 305 306 307 |
# File 'lib/gr_api_manager.rb', line 302 def initialize(tempfile:, filename:, content_type:) @tempfile = tempfile @filename = filename.to_s @content_type = content_type.to_s @size = tempfile.respond_to?(:size) ? tempfile.size : (tempfile.respond_to?(:length) ? tempfile.length : tempfile.to_s.bytesize) end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
300 301 302 |
# File 'lib/gr_api_manager.rb', line 300 def content_type @content_type end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
300 301 302 |
# File 'lib/gr_api_manager.rb', line 300 def filename @filename end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
300 301 302 |
# File 'lib/gr_api_manager.rb', line 300 def size @size end |
#tempfile ⇒ Object (readonly)
Returns the value of attribute tempfile.
300 301 302 |
# File 'lib/gr_api_manager.rb', line 300 def tempfile @tempfile end |
Instance Method Details
#extension ⇒ Object
Convenience: the file extension derived from the original filename.
338 339 340 |
# File 'lib/gr_api_manager.rb', line 338 def extension File.extname(@filename).downcase end |
#inspect ⇒ Object
352 353 354 355 |
# File 'lib/gr_api_manager.rb', line 352 def inspect "#<GRApiManager::FilePayload filename=#{@filename.inspect} " \ "content_type=#{@content_type.inspect} size=#{@size}>" end |
#read ⇒ Object
Returns the raw binary content of the file as a String (encoding: BINARY).
310 311 312 313 314 315 316 317 |
# File 'lib/gr_api_manager.rb', line 310 def read if @tempfile.respond_to?(:read) @tempfile.rewind if @tempfile.respond_to?(:rewind) @tempfile.read else @tempfile.to_s.dup.force_encoding(Encoding::BINARY) end end |
#save_to(dest_path) ⇒ Object
Saves the uploaded content to dest_path on disk. Returns dest_path.
330 331 332 333 334 335 |
# File 'lib/gr_api_manager.rb', line 330 def save_to(dest_path) dir = File.dirname(dest_path) FileUtils.mkdir_p(dir) unless Dir.exist?(dir) File.open(dest_path, 'wb') { |f| f.write(read) } dest_path end |
#to_base64 ⇒ Object
Returns the file content encoded as a Base64 string (no newlines).
320 321 322 |
# File 'lib/gr_api_manager.rb', line 320 def to_base64 Base64.strict_encode64(read) end |
#to_h ⇒ Object
Human-friendly summary — safe to include in JSON responses.
343 344 345 346 347 348 349 350 |
# File 'lib/gr_api_manager.rb', line 343 def to_h { filename: @filename, content_type: @content_type, size: @size, extension: extension } end |
#to_hex ⇒ Object
Returns the file content encoded as a lowercase hexadecimal string.
325 326 327 |
# File 'lib/gr_api_manager.rb', line 325 def to_hex read.unpack1('H*') end |