Sha256: c89081e4d0b3781fa632906a00a14298f200e925c44995a3e431271ee1c1a2e3

Contents?: true

Size: 1.17 KB

Versions: 24

Compression:

Stored size: 1.17 KB

Contents

module WWW
  class Mechanize
    # = Synopsis
    # This is a pluggable parser that automatically saves every file
    # it encounters.  It saves the files as a tree, reflecting the
    # host and file path.
    #
    # == Example to save all PDF's
    #  require 'rubygems'
    #  require 'mechanize'
    #
    #  agent = WWW::Mechanize.new
    #  agent.pluggable_parser.pdf = WWW::Mechanize::FileSaver
    #  agent.get('http://example.com/foo.pdf')
    #
    class FileSaver < File
      attr_reader :filename
  
      def initialize(uri=nil, response=nil, body=nil, code=nil)
        super(uri, response, body, code)
        path = uri.path.empty? ? 'index.html' : uri.path.gsub(/^[\/]*/, '')
        path += 'index.html' if path =~ /\/$/
  
        split_path = path.split(/\//)
        filename = split_path.length > 0 ? split_path.pop : 'index.html'
        joined_path = split_path.join(::File::SEPARATOR)
        path = if joined_path.empty?
          uri.host
        else
          "#{uri.host}#{::File::SEPARATOR}#{joined_path}"
        end
  
        @filename = "#{path}#{::File::SEPARATOR}#{filename}"
        FileUtils.mkdir_p(path)
        save_as(@filename)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
eric-mechanize-0.9.3.20090623142847 lib/www/mechanize/file_saver.rb
knu-mechanize-0.9.3.20090623142847 lib/www/mechanize/file_saver.rb
tenderlove-mechanize-0.9.3.20090617085936 lib/www/mechanize/file_saver.rb
tenderlove-mechanize-0.9.3.20090623142847 lib/www/mechanize/file_saver.rb
mechanize-ntlm-0.9.1 lib/www/mechanize/file_saver.rb
mechanize-0.7.2 lib/www/mechanize/file_saver.rb
mechanize-0.7.4 lib/www/mechanize/file_saver.rb
mechanize-0.7.8 lib/www/mechanize/file_saver.rb
mechanize-0.7.5 lib/www/mechanize/file_saver.rb
mechanize-0.7.0 lib/www/mechanize/file_saver.rb
mechanize-0.7.1 lib/www/mechanize/file_saver.rb
mechanize-0.7.3 lib/www/mechanize/file_saver.rb
mechanize-0.7.6 lib/www/mechanize/file_saver.rb
mechanize-0.7.7 lib/www/mechanize/file_saver.rb
mechanize-0.8.0 lib/www/mechanize/file_saver.rb
mechanize-0.8.3 lib/www/mechanize/file_saver.rb
mechanize-0.8.1 lib/www/mechanize/file_saver.rb
mechanize-0.8.2 lib/www/mechanize/file_saver.rb
mechanize-0.8.4 lib/www/mechanize/file_saver.rb
mechanize-0.8.5 lib/www/mechanize/file_saver.rb