class DownloadTV::Filterer

Builds and applies filters to the results

Attributes

filters[R]

Public Class Methods

new(filters_config) click to toggle source
# File lib/download_tv/filterer.rb, line 9
def initialize(filters_config)
  @filters = []
  build_filters(filters_config)
end

Public Instance Methods

build_exclude_filter(str) click to toggle source
# File lib/download_tv/filterer.rb, line 18
def build_exclude_filter(str)
  @filters << ->(n) { n.upcase.include?(str) }
end
build_filters(filters_config) click to toggle source
# 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) click to toggle source
# File lib/download_tv/filterer.rb, line 14
def build_include_filter(str)
  @filters << ->(n) { !n.upcase.include?(str) }
end
filter(shows) click to toggle source

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

# 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