Class: FileGrabber::Grab

Inherits:
Object
  • Object
show all
Defined in:
lib/filegrabber/grab.rb

Instance Method Summary collapse

Constructor Details

#initializeGrab

Returns a new instance of Grab.



23
24
25
26
# File 'lib/filegrabber/grab.rb', line 23

def initialize
  @grabs = {}
  instance_eval File.read(CONFIG_FILENAME)
end

Instance Method Details

#download(link_selector) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/filegrabber/grab.rb', line 50

def download link_selector
  @docpages.each do |page|
    link = page.link(link_selector)
    DownloadedDocument.new(page.filename).download do
      RemoteDocument.new(link).download
    end.tap do |doc|
      Log.info "Done. File size: #{doc.size}"
      if doc.download_failed?
        Log.warn "Download failed. Retry on next run."
      end
    end
  end
end

#grab(name) ⇒ Object



32
33
34
# File 'lib/filegrabber/grab.rb', line 32

def grab name
  instance_eval &@grabs[name]
end

#grabber(name, &block) ⇒ Object



28
29
30
# File 'lib/filegrabber/grab.rb', line 28

def grabber name, &block
  @grabs[name] = block
end

#index(url, link_selector) ⇒ Object



44
45
46
47
48
# File 'lib/filegrabber/grab.rb', line 44

def index url, link_selector
  @docpages = IndexPage.new(url, 'index.html')
    .links(link_selector)
    .map { |link| DocumentPage.new(link) }
end

#set_http_basic_password(value) ⇒ Object



40
41
42
# File 'lib/filegrabber/grab.rb', line 40

def set_http_basic_password value
  HTTP_BASIC_AUTH[:password] = value
end

#set_http_basic_username(value) ⇒ Object



36
37
38
# File 'lib/filegrabber/grab.rb', line 36

def set_http_basic_username value
  HTTP_BASIC_AUTH[:username] = value
end