Class: Feedx::Producer

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

Overview

Produces a relation as am encoded stream to a remote location.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) { ... } ⇒ Producer

Returns a new instance of Producer.

Parameters:

  • url (String)

    the destination URL.

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :enum (Enumerable, ActiveRecord::Relation)

    relation or enumerator to stream.

  • :format (Symbol, Class<Feedx::Format::Abstract>)

    custom formatter. Default: from file extension.

  • :compress (Symbol, Class<Feedx::Compression::Abstract>)

    enable compression. Default: from file extension.

  • :last_modified (Time, Proc)

    the last modified time, used to determine if a push is necessary.

Yields:

  • A block factory to generate the relation or enumerator.

Yield Returns:

  • (Enumerable, ActiveRecord::Relation)

    the relation or enumerator to stream.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
# File 'lib/feedx/producer.rb', line 21

def initialize(url, opts={}, &block)
  @enum = opts[:enum] || block
  raise ArgumentError, "#{self.class.name}.new expects an :enum option or a block factory" unless @enum

  @blob     = BFS::Blob.new(url)
  @format   = detect_format(opts[:format])
  @compress = detect_compress(opts[:compress])
  @last_mod = opts[:last_modified]
end

Class Method Details

.perform(url, opts = {}, &block) ⇒ Object

See constructor.



9
10
11
# File 'lib/feedx/producer.rb', line 9

def self.perform(url, opts={}, &block)
  new(url, opts, &block).perform
end

Instance Method Details

#performObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/feedx/producer.rb', line 31

def perform
  enum = @enum.is_a?(Proc) ? @enum.call : @enum
  last_mod = @last_mod.is_a?(Proc) ? @last_mod.call(enum) : @last_mod
  current  = (last_mod.to_f * 1000).floor

  begin
    previous = @blob.info.[META_LAST_MODIFIED].to_i
    return -1 unless current > previous
  rescue BFS::FileNotFound # rubocop:disable Lint/HandleExceptions
  end if current.positive?

  @blob.create metadata: { META_LAST_MODIFIED => current.to_s } do |io|
    @compress.wrap(io) {|w| write_all(enum, w) }
  end
  @blob.info.size
end