Class: Feedx::Consumer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/feedx/consumer.rb

Overview

Consumes an enumerates over a feed.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, klass, format_options: {}, cache: nil, **opts) ⇒ Consumer

Returns a new instance of Consumer.

Parameters:

  • url (String)

    the destination URL.

  • klass (Class)

    the record class.

  • opts (Hash)

    options

Options Hash (**opts):



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

def initialize(url, klass, format_options: {}, cache: nil, **opts)
  @klass = klass
  @url = url
  @opts = opts.merge(format_options)
  @cache = cache

  return if format_options.empty? || (defined?(Gem::Deprecate) && Gem::Deprecate.skip)

  warn "WARNING: passing format_options is deprecated; pass the options inline instead (called from #{caller(2..2).first})."
end

Class Method Details

.each(url, klass, **opts, &block) ⇒ Object

See constructor.



11
12
13
# File 'lib/feedx/consumer.rb', line 11

def self.each(url, klass, **opts, &block)
  new(url, klass, **opts).each(&block)
end

Instance Method Details

#each(&block) ⇒ Boolean

Returns true if performed.

Returns:

  • (Boolean)

    returns true if performed.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/feedx/consumer.rb', line 33

def each(&block)
  stream = Feedx::Stream.new(@url, **@opts)
  remote_rev = nil

  if @cache
       = stream.blob.info.
    local_rev  = @cache.read.to_i
    remote_rev = ([META_LAST_MODIFIED] || [META_LAST_MODIFIED_DC]).to_i
    return false if remote_rev.positive? && remote_rev <= local_rev
  end

  stream.open do |fmt|
    fmt.decode_each(@klass, **@opts, &block)
  end
  @cache.write(remote_rev) if @cache && remote_rev

  true
ensure
  stream&.close
end