Sha256: 490a4e4f4b72c236626a934ccddf4b28f4c5a9d9470bbfe2a64ac84bd37c7b2a

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 KB

Contents

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 = Mechanize.new
  #  agent.pluggable_parser.pdf = 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

Version data entries

11 entries across 11 versions & 6 rubygems

Version Path
kitamomonga-mechanize-0.9.3.20090724215219 lib/mechanize/file_saver.rb
tenderlove-mechanize-0.9.3.20090911221705 lib/mechanize/file_saver.rb
domo-0.0.4 vendor/bundle/ruby/1.9.1/gems/mechanize-1.0.0/lib/mechanize/file_saver.rb
aai10-mechanize-2.0.1.0 lib/mechanize/file_saver.rb
neocoin-mechanize-2.0.2 lib/mechanize/file_saver.rb
mechanize-2.0.1 lib/mechanize/file_saver.rb
mechanize-2.0 lib/mechanize/file_saver.rb
mechanize-2.0.pre.2 lib/mechanize/file_saver.rb
mechanize-2.0.pre.1 lib/mechanize/file_saver.rb
mechanize-1.0.1.beta.20110107104205 lib/mechanize/file_saver.rb
mechanize-1.0.0 lib/mechanize/file_saver.rb