Browse files

Download#body was missing. Issue #202 by Angas

Mechanize::Download is now a subclass of Mechanize::File.
  • Loading branch information...
1 parent f2a551c commit a322d78ce71ed37a81db91b57acb97284dfd0a20 @knu knu committed Feb 16, 2012
Showing with 21 additions and 20 deletions.
  1. +5 −0 CHANGELOG.rdoc
  2. +2 −2 lib/mechanize.rb
  3. +12 −18 lib/mechanize/download.rb
  4. +2 −0 test/test_mechanize.rb
View
5 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
View
4 lib/mechanize.rb
@@ -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'
View
30 lib/mechanize/download.rb
@@ -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
##
View
2 test/test_mechanize.rb
@@ -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

0 comments on commit a322d78

Please sign in to comment.