Sha256: cb466e3ddf4b13fa218770781541931b7190a202724af11bb185e4fbc4a3d7b2

Contents?: true

Size: 999 Bytes

Versions: 1

Compression:

Stored size: 999 Bytes

Contents

module Downlow
  class Extractor
    
    def self.handles(which)
      @@handlers ||= []
      @@handlers << [which, self]
    end
    
    def self.extractor_for(path)
      @@handlers.each do |matcher, klass|
        return klass if matcher.match path
      end
    end
    
    def self.extract(url, options = {})
      klass = extractor_for(url)
      extractor = klass.new(url, options)
      extractor.extract
      extractor.final_path
    end
    
    attr_reader :path, :options, :final_path
    attr_accessor :tmp_dir, :destination
    
    def initialize(path, options = {})
      @path        = Pathname.new(path)
      @options     = options
      @tmp_dir     = Pathname.new(options[:tmp_dir] || 'tmp').expand_path
      @tmp_dir.mkpath
      @destination = Pathname.new(options[:destination] || tmp_dir + self.path.stem).expand_path
    end
    
    def extract
      raise "Should be overridden by subclass"
    end
    
    def extracted?
      !!@final_path
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
downlow-0.1.0 lib/downlow/extractor.rb