Sha256: 9b12d3fbe215b8897cf557f8047e412a71661a320b0885da2cb1d5cac0a340cf

Contents?: true

Size: 1.42 KB

Versions: 18

Compression:

Stored size: 1.42 KB

Contents

##
# This is the base class for the Pluggable Parsers.  If Mechanize cannot find
# an appropriate class to use for the content type, this class will be used.
# For example, if you download an image/jpeg, Mechanize will not know how to
# parse it, so this class will be instantiated.
#
# This is a good class to use as the base class for building your own
# pluggable parsers.
#
# == Example
#
#   require 'mechanize'
#
#   agent = Mechanize.new
#   agent.get('http://example.com/foo.jpg').class  #=> Mechanize::File

class Mechanize::File

  include Mechanize::Parser

  ##
  # The HTTP response body, the raw file contents

  attr_accessor :body

  ##
  # The filename for this file based on the content-disposition of the
  # response or the basename of the URL

  attr_accessor :filename

  alias content body

  ##
  # Creates a new file retrieved from the given +uri+ and +response+ object.
  # The +body+ is the HTTP response body and +code+ is the HTTP status.

  def initialize uri = nil, response = nil, body = nil, code = nil
    @uri  = uri
    @body = body
    @code = code

    @full_path = false unless defined? @full_path

    fill_header response
    extract_filename

    yield self if block_given?
  end

  ##
  # Use this method to save the content of this object to +filename+

  def save filename = nil
    filename = find_free_name filename

    open filename, 'wb' do |f|
      f.write body
    end
  end

  alias save_as save

end

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
mechanize-2.5.1 lib/mechanize/file.rb
mechanize-2.5 lib/mechanize/file.rb
mechanize-2.4 lib/mechanize/file.rb
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file.rb
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file.rb
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file.rb
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file.rb
frameworks-capybara-0.2.0.rc2 vendor/bundle/ruby/1.8/gems/mechanize-2.3/lib/mechanize/file.rb
mechanize-2.3 lib/mechanize/file.rb
mechanize-2.2.1 lib/mechanize/file.rb
mechanize-2.2 lib/mechanize/file.rb
mechanize-2.1.1 lib/mechanize/file.rb
domo-0.0.5 vendor/bundle/ruby/1.9.1/gems/mechanize-2.1/lib/mechanize/file.rb
diamond-mechanize-2.4 lib/mechanize/file.rb
diamond-mechanize-2.3 lib/mechanize/file.rb
diamond-mechanize-2.1 lib/mechanize/file.rb
mechanize-2.1 lib/mechanize/file.rb
mechanize-2.1.pre.1 lib/mechanize/file.rb