Sha256: e8ca8676d3dac77b1f48020c602ee96adf3fd8dd3294e8660303cd57d5f144d3

Contents?: true

Size: 965 Bytes

Versions: 1

Compression:

Stored size: 965 Bytes

Contents

require "yaml"

module CourseraDownloader
  class Policy
    def initialize(file)
      @patterns = YAML::load(File.read(file))

      @patterns.each_pair do |group, patterns|
        @patterns[group] = patterns.map do |pattern|
          Regexp.new(pattern)
        end
      end
    end

    def url_action(url)
      return :none if !url || (url.scheme && !(url.scheme != "http" || url.scheme != "https"))

      url = url.to_s
      return :disable if disable_url?(url)
      return :download if download_url?(url)
    end

    private

    def download_url?(url)
      @patterns["blacklist"].each do |pattern|
        return false if url.match(pattern)
      end

      match = false
      @patterns["whitelist"].each do |pattern|
        match = true if url.match(pattern)
      end

      match
    end

    def disable_url?(url)
      @patterns["disable"].each do |pattern|
        return true if url.match(pattern)
      end

      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coursera_downloader-1.0.0 lib/coursera_downloader/policy.rb