Class: FormatParser::ActiveStorage::BlobIO
- Inherits:
-
Object
- Object
- FormatParser::ActiveStorage::BlobIO
- Defined in:
- lib/active_storage/blob_io.rb
Instance Method Summary collapse
- #initialize(blob) ⇒ BlobIO constructor
-
#pos ⇒ Integer
Emulates IO#pos.
-
#read(n_bytes) ⇒ String
Emulates IO#read, but requires the number of bytes to read.
-
#seek(offset) ⇒ Integer
Emulates IO#seek.
-
#size ⇒ Integer
Emulates IO#size.
Constructor Details
#initialize(blob) ⇒ BlobIO
8 9 10 11 12 |
# File 'lib/active_storage/blob_io.rb', line 8 def initialize(blob) @blob = blob @service = blob.service @pos = 0 end |
Instance Method Details
#pos ⇒ Integer
Emulates IO#pos
46 47 48 |
# File 'lib/active_storage/blob_io.rb', line 46 def pos @pos end |
#read(n_bytes) ⇒ String
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)
19 20 21 22 23 24 25 |
# 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) ⇒ Integer
Emulates IO#seek
31 32 33 34 |
# File 'lib/active_storage/blob_io.rb', line 31 def seek(offset) @pos = offset 0 end |
#size ⇒ Integer
Emulates IO#size.
39 40 41 |
# File 'lib/active_storage/blob_io.rb', line 39 def size @blob.byte_size end |