lib/mechanize/form.rb in mechanize-0.5.3 vs lib/mechanize/form.rb in mechanize-0.5.4
- old
+ new
@@ -1,5 +1,7 @@
+require 'mime/types'
+
module WWW
class Mechanize
# =Synopsis
# GlobalForm provides all access to form fields, such as the buttons,
# check boxes, and text input.
@@ -104,25 +106,22 @@
# This method calculates the request data to be sent back to the server
# for this form, depending on if this is a regular post, get, or a
# multi-part post,
def request_data
query_params = build_query()
- query = nil
case @enctype.downcase
when 'multipart/form-data'
boundary = rand_string(20)
@enctype << "; boundary=#{boundary}"
params = []
query_params.each { |k,v| params << param_to_multipart(k, v) }
@file_uploads.each { |f| params << file_to_multipart(f) }
- query = params.collect { |p| "--#{boundary}\r\n#{p}" }.join('') +
+ params.collect { |p| "--#{boundary}\r\n#{p}" }.join('') +
"--#{boundary}--\r\n"
else
- query = WWW::Mechanize.build_query_string(query_params)
+ WWW::Mechanize.build_query_string(query_params)
end
-
- query
end
private
def parse
@fields = WWW::Mechanize::List.new
@@ -145,11 +144,11 @@
when 'radio'
@radiobuttons << RadioButton.new(node.attributes['name'], node.attributes['value'], node.attributes.has_key?('checked'), self)
when 'checkbox'
@checkboxes << CheckBox.new(node.attributes['name'], node.attributes['value'], node.attributes.has_key?('checked'), self)
when 'file'
- @file_uploads << FileUpload.new(node.attributes['name'], node.attributes['value'])
+ @file_uploads << FileUpload.new(node.attributes['name'], nil)
when 'submit'
@buttons << Button.new(node.attributes['name'], node.attributes['value'])
when 'image'
@buttons << ImageButton.new(node.attributes['name'], node.attributes['value'])
end
@@ -185,16 +184,27 @@
def file_to_multipart(file)
body = "Content-Disposition: form-data; name=\"" +
"#{mime_value_quote(file.name)}\"; " +
"filename=\"#{mime_value_quote(file.file_name || '')}\"\r\n" +
"Content-Transfer-Encoding: binary\r\n"
+
+ if file.file_data.nil? and ! file.file_name.nil?
+ file.file_data = ::File.open(file.file_name, "rb") { |f| f.read }
+ file.mime_type = MIME::Types.type_for(file.file_name).first
+ end
+
if file.mime_type != nil
body << "Content-Type: #{file.mime_type}\r\n"
end
- body << "\r\n#{file.file_data}\r\n"
-
+ body <<
+ if file.file_data.respond_to? :read
+ "\r\n#{file.file_data.read}\r\n"
+ else
+ "\r\n#{file.file_data}\r\n"
+ end
+
body
end
end
# =Synopsis
@@ -204,10 +214,13 @@
#
# ==Example
# Find a form and print out its fields
# form = page.forms.first # => WWW::Mechanize::Form
# form.fields.each { |f| puts f.name }
+ # Set the input field 'name' to "Aaron"
+ # form['name'] = 'Aaron'
+ # puts form['name']
class Form < GlobalForm
attr_reader :node
def initialize(node)
@node = node
@@ -235,9 +248,25 @@
index = val.to_i unless value.nil?
value = val if value.nil?
end
self.fields.name(k.to_s).[](index).value = value
end
+ end
+
+ # Fetch the value of the first input field with the name passed in
+ # ==Example
+ # Fetch the value set in the input field 'name'
+ # puts form['name']
+ def [](field_name)
+ field(field_name).value
+ end
+
+ # Set the value of the first input field with the name passed in
+ # ==Example
+ # Set the value in the input field 'name' to "Aaron"
+ # form['name'] = 'Aaron'
+ def []=(field_name, value)
+ field(field_name).value = value
end
# Treat form fields like accessors.
def method_missing(id,*args)
method = id.to_s.gsub(/=$/, '')