Skip to content

Commit

Permalink
Download#body was missing. Issue #202 by Angas
Browse files Browse the repository at this point in the history
Mechanize::Download is now a subclass of Mechanize::File.
  • Loading branch information
knu committed Feb 16, 2012
1 parent f2a551c commit a322d78
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,10 @@
= Mechanize CHANGELOG

=== 2.2.2 / ????-??-??

* Bug fixes
* Download#body was missing. Issue #202 by Angas

=== 2.2.1 / 2012-02-13

* Bug fixes
Expand Down
4 changes: 2 additions & 2 deletions lib/mechanize.rb
Expand Up @@ -1183,12 +1183,12 @@ def add_to_history(page)
require 'mechanize/cookie'
require 'mechanize/cookie_jar'
require 'mechanize/parser'
require 'mechanize/download'
require 'mechanize/directory_saver'
require 'mechanize/file'
require 'mechanize/file_connection'
require 'mechanize/file_request'
require 'mechanize/file_response'
require 'mechanize/download'
require 'mechanize/directory_saver'
require 'mechanize/form'
require 'mechanize/history'
require 'mechanize/http'
Expand Down
30 changes: 12 additions & 18 deletions lib/mechanize/download.rb
Expand Up @@ -5,39 +5,33 @@
#
# See Mechanize::PluggableParser for instructions on using this class.

class Mechanize::Download

include Mechanize::Parser

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

attr_accessor :filename
class Mechanize::Download < Mechanize::File

##
# Accessor for the IO-like that contains the body

attr_reader :body_io

# This method returns an IO-like object, not a String like
# Mechanize::File and Mechanize::Page do.
alias content body_io

# Returns a whole content body. Use save() instead if you just want
# to download the content into a file.
def body
@body_io.read.tap {
@body_io.rewind
}
end

##
# Creates a new download retrieved from the given +uri+ and +response+
# object. The +body_io+ is an IO-like containing the HTTP response body and
# +code+ is the HTTP status.

def initialize uri = nil, response = nil, body_io = nil, code = nil
@uri = uri
super uri, response, nil, code
@body_io = body_io
@code = code

@full_path = false unless defined? @full_path

fill_header response
extract_filename

yield self if block_given?
end

##
Expand Down
2 changes: 2 additions & 0 deletions test/test_mechanize.rb
Expand Up @@ -844,6 +844,8 @@ def test_parse_download

assert_kind_of Mechanize::Download, download
assert_kind_of StringIO, download.content
assert_equal 'raw', download.body
assert_equal 'raw', download.body, 'test if body() is repeatable'
end

def test_parse_html
Expand Down

0 comments on commit a322d78

Please sign in to comment.