Class: Feedx::Stream
- Inherits:
-
Object
- Object
- Feedx::Stream
- Defined in:
- lib/feedx/stream.rb
Overview
Abstract stream handler around a remote blob.
Instance Attribute Summary collapse
-
#blob ⇒ Object
readonly
Returns the value of attribute blob.
Class Method Summary collapse
-
.open(url, **opts) ⇒ Object
Behaves like new, but accepts an optional block.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the underlying connection.
-
#create(**opts) {|formatted| ... } ⇒ Object
Opens the remote for writing.
-
#initialize(url, format: nil, compress: nil, **opts) ⇒ Stream
constructor
A new instance of Stream.
-
#open(**opts) {|formatted| ... } ⇒ Object
Opens the remote for reading.
Constructor Details
#initialize(url, format: nil, compress: nil, **opts) ⇒ Stream
Returns a new instance of Stream.
26 27 28 29 30 31 32 33 |
# File 'lib/feedx/stream.rb', line 26 def initialize(url, format: nil, compress: nil, **opts) @blob = BFS::Blob.new(url) @format = detect_format(format) @compress = detect_compress(compress) @opts = opts BFS.defer(self, :close) end |
Instance Attribute Details
#blob ⇒ Object (readonly)
Returns the value of attribute blob.
7 8 9 |
# File 'lib/feedx/stream.rb', line 7 def blob @blob end |
Class Method Details
.open(url, **opts) ⇒ Object
Behaves like new, but accepts an optional block. If a block is given, streams are automatically closed after the block is yielded.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/feedx/stream.rb', line 11 def self.open(url, **opts) stream = new(url, **opts) return stream unless block_given? begin yield stream ensure stream.close end end |
Instance Method Details
#close ⇒ Object
Closes the underlying connection.
60 61 62 |
# File 'lib/feedx/stream.rb', line 60 def close @blob.close end |
#create(**opts) {|formatted| ... } ⇒ Object
Opens the remote for writing.
51 52 53 54 55 56 57 |
# File 'lib/feedx/stream.rb', line 51 def create(**opts, &block) @blob.create(**opts) do |io| @compress.writer(io, **@opts) do |cio| @format.encoder(cio, **@opts, &block) end end end |
#open(**opts) {|formatted| ... } ⇒ Object
Opens the remote for reading.
39 40 41 42 43 44 45 |
# File 'lib/feedx/stream.rb', line 39 def open(**opts, &block) @blob.open(**opts) do |io| @compress.reader(io, **@opts) do |cio| @format.decoder(cio, **@opts, &block) end end end |