Class: Mechanize::DirectorySaver

Inherits:
Download
  • Object
show all
Defined in:
lib/mechanize/directory_saver.rb

Overview

Unlike Mechanize::FileSaver, the directory saver places all downloaded files in a single pre-specified directory.

You must register the directory to save to before using the directory saver:

agent.pluggable_parser['image'] = \
  Mechanize::DirectorySaver.save_to 'images'

Constant Summary

Constants included from Parser

Parser::SPECIAL_FILENAMES

Instance Attribute Summary

Attributes inherited from Download

#body_io, #filename

Attributes included from Parser

#code, #response, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Download

#body, #save

Methods included from Parser

#extract_filename, #fill_header, #find_free_name

Constructor Details

#initialize(uri = nil, response = nil, body_io = nil, code = nil) ⇒ DirectorySaver

Saves the body_io into the directory specified for this DirectorySaver by save_to. The filename is chosen by Mechanize::Parser#extract_filename.

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mechanize/directory_saver.rb', line 37

def initialize uri = nil, response = nil, body_io = nil, code = nil
  directory = self.class.directory

  raise Mechanize::Error,
    'no save directory specified - ' \
    'use Mechanize::DirectorySaver.save_to ' \
    'and register the resulting class' unless directory

  super

  path = File.join directory, @filename

  save path
end

Class Method Details

.directoryObject

The directory downloaded files will be saved to.



29
30
31
# File 'lib/mechanize/directory_saver.rb', line 29

def self.directory
  @directory
end

.save_to(directory) ⇒ Object

Creates a DirectorySaver subclass that will save responses to the given directory.



18
19
20
21
22
23
24
# File 'lib/mechanize/directory_saver.rb', line 18

def self.save_to directory
  directory = File.expand_path directory

  Class.new self do |klass|
    klass.instance_variable_set :@directory, directory
  end
end