Class: Downlow::Fetcher

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

Direct Known Subclasses

Git, Http, Local

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Fetcher

Returns a new instance of Fetcher.



25
26
27
28
29
30
31
32
# File 'lib/downlow/fetcher.rb', line 25

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

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



23
24
25
# File 'lib/downlow/fetcher.rb', line 23

def destination
  @destination
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



22
23
24
# File 'lib/downlow/fetcher.rb', line 22

def local_path
  @local_path
end

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/downlow/fetcher.rb', line 22

def options
  @options
end

#tmp_dirObject

Returns the value of attribute tmp_dir.



23
24
25
# File 'lib/downlow/fetcher.rb', line 23

def tmp_dir
  @tmp_dir
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/downlow/fetcher.rb', line 22

def url
  @url
end

Class Method Details

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



15
16
17
18
19
20
# File 'lib/downlow/fetcher.rb', line 15

def self.fetch(url, options = {})
  klass = fetcher_for(url)
  fetcher = klass.new(url, options)
  fetcher.fetch
  fetcher.local_path
end

.fetcher_for(url) ⇒ Object



9
10
11
12
13
# File 'lib/downlow/fetcher.rb', line 9

def self.fetcher_for(url)
  @@handlers.each do |matcher, klass|
    return klass if matcher.match url
  end
end

.handles(which) ⇒ Object



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

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

Instance Method Details

#fetchObject



34
35
36
# File 'lib/downlow/fetcher.rb', line 34

def fetch
  raise "Should be overridden by subclass"
end

#fetched?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/downlow/fetcher.rb', line 38

def fetched?
  !!@local_path
end