Class: FormatParser::RemoteIO
- Inherits:
-
Object
- Object
- FormatParser::RemoteIO
- Defined in:
- lib/remote_io.rb
Overview
Acts as a wrapper for turning a given URL into an IO object you can read from and seek in. Uses Faraday under the hood to perform fetches, so if you apply Faraday configuration tweaks using ‘Faraday.default_connection = …` these will take effect for these RemoteIO objects as well
Defined Under Namespace
Classes: IntermittentFailure, InvalidRequest
Instance Method Summary collapse
-
#initialize(uri) ⇒ RemoteIO
constructor
A new instance of RemoteIO.
-
#pos ⇒ Object
Emulates IO#pos.
-
#read(n_bytes) ⇒ String
Emulates IO#read, but requires the number of bytes to read The read will be limited to the size of the remote resource relative to the current offset in the IO, so if you are at offset 0 in the IO of size 10, doing a ‘read(20)` will only return you 10 bytes of result, and not raise any exceptions.
-
#seek(offset) ⇒ Object
Emulates IO#seek.
-
#size ⇒ Integer
Emulates IO#size.
Constructor Details
#initialize(uri) ⇒ RemoteIO
Returns a new instance of RemoteIO.
18 19 20 21 22 23 |
# File 'lib/remote_io.rb', line 18 def initialize(uri) require 'faraday' @uri = uri @pos = 0 @remote_size = false end |
Instance Method Details
#pos ⇒ Object
Emulates IO#pos
32 33 34 |
# File 'lib/remote_io.rb', line 32 def pos @pos end |
#read(n_bytes) ⇒ String
Emulates IO#read, but requires the number of bytes to read The read will be limited to the size of the remote resource relative to the current offset in the IO, so if you are at offset 0 in the IO of size 10, doing a ‘read(20)` will only return you 10 bytes of result, and not raise any exceptions.
52 53 54 55 56 57 58 59 60 |
# File 'lib/remote_io.rb', line 52 def read(n_bytes) http_range = (@pos..(@pos + n_bytes - 1)) maybe_size, maybe_body = request_range(http_range) if maybe_size && maybe_body @remote_size = maybe_size @pos += maybe_body.bytesize maybe_body.force_encoding(Encoding::ASCII_8BIT) end end |
#seek(offset) ⇒ Object
Emulates IO#seek
26 27 28 29 |
# File 'lib/remote_io.rb', line 26 def seek(offset) @pos = offset 0 # always return 0 end |
#size ⇒ Integer
Emulates IO#size.
39 40 41 42 |
# File 'lib/remote_io.rb', line 39 def size raise 'Remote size not yet obtained, need to perform at least one read() to retrieve it' unless @remote_size @remote_size end |