Module: Rextract::ArchiveResponse

Included in:
Browser
Defined in:
lib/rextract/browser.rb

Instance Method Summary collapse

Instance Method Details

#archive_dirObject



17
18
19
# File 'lib/rextract/browser.rb', line 17

def archive_dir
  @archive_dir || (archive_dir = default_archive_dir)
end

#archive_dir=(dir_path) ⇒ Object



25
26
27
28
29
# File 'lib/rextract/browser.rb', line 25

def archive_dir=(dir_path)
  dir = File.expand_path(dir_path)
  ensure_dir(dir_path)
  @archive_dir = dir
end

#default_archive_dir(base = "~/tmp/") ⇒ Object



13
14
15
# File 'lib/rextract/browser.rb', line 13

def default_archive_dir(base = "~/tmp/")
  File.expand_path(base + Time.now.strftime("%Y-%m-%d_%H-%M-%S/")) 
end

#ensure_dir(dir_path) ⇒ Object



21
22
23
# File 'lib/rextract/browser.rb', line 21

def ensure_dir(dir_path)
  FileUtils.mkdir_p(dir_path) unless File.exists?(dir_path)
end

#get(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rextract/browser.rb', line 31

def get(*args)
  url = args.is_a?(Hash) ? args[:url] : args.first
  body_path   = "#{archive_dir}/#{sanitize_url(url)}.html"
  header_path = "#{archive_dir}/#{sanitize_url(url)}.headers"
  ensure_dir(archive_dir)
  
  page = super(*args)

  write_to_file(body_path, page.body.to_s)
  
  header_output = ''
  PP.pp(page.header, header_output)
  
  write_to_file(header_path, header_output)
  
  page
end

#initialize(*args) ⇒ Object



8
9
10
11
# File 'lib/rextract/browser.rb', line 8

def initialize(*args)
  @archive_dir=nil
  super(*args)
end

#sanitize_url(url) ⇒ Object



55
56
57
# File 'lib/rextract/browser.rb', line 55

def sanitize_url(url)
  url.gsub(/[^A-z0-9_\-\.]/, "_")
end

#write_to_file(path, data) ⇒ Object



49
50
51
52
53
# File 'lib/rextract/browser.rb', line 49

def write_to_file(path, data)
  File.open(path, "w+") do |f|
    f.write(data)
  end
end