Class: Downlow::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/downlow/extractor.rb

Direct Known Subclasses

Dir, TarGz, Zip

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Extractor

Returns a new instance of Extractor.



29
30
31
32
33
34
35
# File 'lib/downlow/extractor.rb', line 29

def initialize(path, options = {})
  @path        = Pathname.new(path)
  @options     = options
  @tmp_dir     = Pathname.new(options[:tmp_dir] || 'tmp').expand_path + 'extract'
  @tmp_dir.mkpath
  @destination = Pathname.new(options[:destination] || tmp_dir + self.path.stem).expand_path
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



27
28
29
# File 'lib/downlow/extractor.rb', line 27

def destination
  @destination
end

#final_pathObject (readonly)

Returns the value of attribute final_path.



26
27
28
# File 'lib/downlow/extractor.rb', line 26

def final_path
  @final_path
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/downlow/extractor.rb', line 26

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/downlow/extractor.rb', line 26

def path
  @path
end

#tmp_dirObject

Returns the value of attribute tmp_dir.



27
28
29
# File 'lib/downlow/extractor.rb', line 27

def tmp_dir
  @tmp_dir
end

Class Method Details

.extract(url, options = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/downlow/extractor.rb', line 19

def self.extract(url, options = {})
  klass = extractor_for(url)
  extractor = klass.new(url, options)
  extractor.extract
  extractor.final_path
end

.extractor_for(path) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/downlow/extractor.rb', line 9

def self.extractor_for(path)
  @@handlers.each do |matcher, options, klass|
    if options[:file_only] && !File.file?(path)
      next
    else
      return klass if matcher.match(path)
    end
  end
end

.handles(which, options = {}) ⇒ Object



4
5
6
7
# File 'lib/downlow/extractor.rb', line 4

def self.handles(which, options = {})
  @@handlers ||= []
  @@handlers << [which, options, self]
end

Instance Method Details

#extractObject



37
38
39
# File 'lib/downlow/extractor.rb', line 37

def extract
  raise "Should be overridden by subclass"
end

#extracted?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/downlow/extractor.rb', line 41

def extracted?
  !!@final_path
end