class FormatParser::ActiveStorage::BlobIO

Public Class Methods

new(blob) click to toggle source

@param blob the file with linked service @return [BlobIO]

# File lib/active_storage/blob_io.rb, line 8
def initialize(blob)
  @blob = blob
  @service = blob.service
  @pos = 0
end

Public Instance Methods

pos() click to toggle source

Emulates IO#pos

@return [Integer] the current offset (in bytes) of the io

# File lib/active_storage/blob_io.rb, line 46
def pos
  @pos
end
read(n_bytes) click to toggle source

Emulates IO#read, but requires the number of bytes to read. Rely on `ActiveStorage::Service.download_chunk` of each hosting type (local, S3, Azure, etc)

@param n_bytes how many bytes to read @return [String] the read bytes

# File lib/active_storage/blob_io.rb, line 19
def read(n_bytes)
  # HTTP ranges are exclusive.
  http_range = (@pos..(@pos + n_bytes - 1))
  body = @service.download_chunk(@blob.key, http_range)
  @pos += body.bytesize
  body.force_encoding(Encoding::ASCII_8BIT)
end
seek(offset) click to toggle source

Emulates IO#seek

@param [Integer] offset size @return [Integer] always return 0, `seek` only mutates `pos` attribute

# File lib/active_storage/blob_io.rb, line 31
def seek(offset)
  @pos = offset
  0
end
size() click to toggle source

Emulates IO#size.

@return [Integer] the size of the blob size from ActiveStorage

# File lib/active_storage/blob_io.rb, line 39
def size
  @blob.byte_size
end