Class: GRApiManager::FilePayload

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

Overview


FilePayload — wraps an uploaded file (multipart or raw) with a clean API.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_typeObject (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

#filenameObject (readonly)

Returns the value of attribute filename.



300
301
302
# File 'lib/gr_api_manager.rb', line 300

def filename
  @filename
end

#sizeObject (readonly)

Returns the value of attribute size.



300
301
302
# File 'lib/gr_api_manager.rb', line 300

def size
  @size
end

#tempfileObject (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

#extensionObject

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

#inspectObject



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

#readObject

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_base64Object

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_hObject

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_hexObject

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