Class: Skriptorium::BaseSource

Inherits:
Object
  • Object
show all
Defined in:
lib/skriptorium/base_source.rb

Direct Known Subclasses

DevTo::Source, RSS::Source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BaseSource

Returns a new instance of BaseSource.



5
6
7
8
9
# File 'lib/skriptorium/base_source.rb', line 5

def initialize(config)
  @config = config
  @limit = config['limit'] || 10
  @filter = config['filter']
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/skriptorium/base_source.rb', line 3

def config
  @config
end

#limitObject (readonly)

Returns the value of attribute limit.



3
4
5
# File 'lib/skriptorium/base_source.rb', line 3

def limit
  @limit
end

Instance Method Details

#apply_filter(stream) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/skriptorium/base_source.rb', line 21

def apply_filter(stream)
  return stream unless @filter

  stream.select do |article|
    pass = @filter.all? do |name, opts|
      case name
      when 'front_matter'
        opts.all? do |k, v|
          article.front_matter(k.strip) == v.to_s.strip
        end
      else
        raise "Invalid filter #{name}: #{opts.inspect}"
      end
    end
    # puts "  article \"#{article.title}\" pass=#{pass}"
    pass
  end
end

#fetchArray<Article>

Returns:

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/skriptorium/base_source.rb', line 12

def fetch
  raise NotImplementedError, "#{self.class} must implement #fetch_latest"
end

#fetch_latestObject



16
17
18
19
# File 'lib/skriptorium/base_source.rb', line 16

def fetch_latest
  filtered = apply_filter(fetch)
  filtered.first(@limit)
end