Class: Feedx::Stream

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

Overview

Abstract stream handler around a remote blob.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, format: nil, compress: nil, **opts) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • url (String)

    the blob URL.

  • opts (Hash)

    options

Options Hash (**opts):



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

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

#closeObject

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.

Parameters:

  • opts (Hash)

    BFS::Blob#create options

Yields:

  • A block over a formatted stream.

Yield Parameters:



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.

Parameters:

  • opts (Hash)

    BFS::Blob#open options

Yields:

  • A block over a formatted stream.

Yield Parameters:



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