class TransmissionRSS::Feed

Attributes

regexp[R]
url[R]

Public Class Methods

new(config = {}) click to toggle source
# File lib/transmission-rss/feed.rb, line 5
def initialize(config = {})
  @download_paths = {}

  case config
  when Hash
    @url = URI.encode(config['url'] || config.keys.first)

    @download_path = config['download_path']

    matchers = Array(config['regexp']).map do |e|
      e.is_a?(String) ? e : e['matcher']
    end

    @regexp = build_regexp(matchers)

    initialize_download_paths(config['regexp'])
  else
    @url = config.to_s
  end
end

Public Instance Methods

download_path(title = nil) click to toggle source
# File lib/transmission-rss/feed.rb, line 26
def download_path(title = nil)
  return @download_path if title.nil?

  @download_paths.each do |regexp, path|
    return path if title =~ to_regexp(regexp)
  end

  return @download_path
end
matches_regexp?(title) click to toggle source
# File lib/transmission-rss/feed.rb, line 36
def matches_regexp?(title)
  @regexp.nil? || !(title =~ @regexp).nil?
end

Private Instance Methods

build_regexp(matchers) click to toggle source
# File lib/transmission-rss/feed.rb, line 42
def build_regexp(matchers)
  matchers = Array(matchers).map { |m| to_regexp(m) }
  matchers.empty? ? nil : Regexp.union(matchers)
end
initialize_download_paths(regexps) click to toggle source
# File lib/transmission-rss/feed.rb, line 47
def initialize_download_paths(regexps)
  return unless regexps.is_a?(Array)

  regexps.each do |regexp|
    matcher = regexp['matcher']
    path    = regexp['download_path']

    @download_paths[matcher] = path if matcher && path
  end
end
to_regexp(s) click to toggle source
# File lib/transmission-rss/feed.rb, line 58
def to_regexp(s)
  Regexp.new(s, Regexp::IGNORECASE)
end