Class: DownloadTV::Filterer

Inherits:
Object
  • Object
show all
Defined in:
lib/download_tv/filterer.rb

Overview

Builds and applies filters to the results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filters_config) ⇒ Filterer

Returns a new instance of Filterer.



9
10
11
12
# File 'lib/download_tv/filterer.rb', line 9

def initialize(filters_config)
  @filters = []
  build_filters(filters_config)
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



7
8
9
# File 'lib/download_tv/filterer.rb', line 7

def filters
  @filters
end

Instance Method Details

#build_exclude_filter(str) ⇒ Object



18
19
20
# File 'lib/download_tv/filterer.rb', line 18

def build_exclude_filter(str)
  @filters << ->(n) { n.upcase.include?(str) }
end

#build_filters(filters_config) ⇒ Object



22
23
24
25
26
27
# File 'lib/download_tv/filterer.rb', line 22

def build_filters(filters_config)
  return unless filters_config

  filters_config[:includes].map { |i| build_include_filter(i) }
  filters_config[:excludes].map { |i| build_exclude_filter(i) }
end

#build_include_filter(str) ⇒ Object



14
15
16
# File 'lib/download_tv/filterer.rb', line 14

def build_include_filter(str)
  @filters << ->(n) { !n.upcase.include?(str) }
end

#filter(shows) ⇒ Object

Iteratively applies filters until they’ve all been applied or applying the next filter would result in no results



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/download_tv/filterer.rb', line 32

def filter(shows)
  # shows is tuple (show name, link)
  @filters.each do |f|
    new_shows = shows.reject { |name, _link| f.call(name) }
    # Go to next filter if the filter removes every release
    next if new_shows.empty?

    shows = new_shows
  end

  shows
end