Class: PageDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/page_downloader.rb,
lib/page_downloader/filters.rb,
lib/page_downloader/version.rb,
lib/page_downloader/filters/embed_style.rb,
lib/page_downloader/filters/embed_script.rb,
lib/page_downloader/remote_content_fetcher.rb,
lib/page_downloader/filters/to_document_object.rb,
lib/page_downloader/filters/document_object_to_string.rb

Defined Under Namespace

Classes: Filters, RemoteContentFetcher

Constant Summary collapse

VERSION =
"1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, options = {}) ⇒ PageDownloader



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/page_downloader.rb', line 13

def initialize(page, options = {})
  @page = page
  @fetcher = options.fetch(:fetcher, RemoteContentFetcher.new)
  @filters = options[:filters]

  unless @filters
    resolver = UrlResolver.new(referer: page)
    options =  { fetcher: fetcher, url_resolver: resolver }

    @filters ||= Filters.new [
      Filters::ToDocumentObject.new,
      Filters::EmbedScript.new(options),
      Filters::EmbedStyle.new(options),
      Filters::DocumentObjectToString.new,
    ]
  end
end

Instance Attribute Details

#fetcherObject (readonly)

Returns the value of attribute fetcher.



11
12
13
# File 'lib/page_downloader.rb', line 11

def fetcher
  @fetcher
end

#filtersObject (readonly)

Returns the value of attribute filters.



11
12
13
# File 'lib/page_downloader.rb', line 11

def filters
  @filters
end

#pageObject (readonly)

Returns the value of attribute page.



11
12
13
# File 'lib/page_downloader.rb', line 11

def page
  @page
end

Instance Method Details

#to_file(destination) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/page_downloader.rb', line 31

def to_file(destination)
  content = filters.filter(fetcher.fetch(page))

  File.open(destination, "w") do |f|
    f.puts(content)
  end
end