mechanize 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mechanize might be problematic. Click here for more details.
- data/lib/mechanize.rb +63 -10
- metadata +2 -2
data/lib/mechanize.rb
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
# set cookies (I might be wrong), does not automatically redirect and has
|
7
7
|
# problems with some html documents.
|
8
8
|
|
9
|
-
Version = "0.2.
|
9
|
+
Version = "0.2.3"
|
10
10
|
|
11
11
|
# required due to the missing get_fields method in Ruby 1.8.2
|
12
12
|
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "mechanize", "net-overrides")
|
@@ -26,10 +26,10 @@ class Field
|
|
26
26
|
|
27
27
|
def initialize(name, value)
|
28
28
|
@name, @value = name, value
|
29
|
-
@user, @password = nil
|
30
29
|
end
|
31
30
|
|
32
31
|
# Returns an array of Field objects
|
32
|
+
# TODO: is this correct?
|
33
33
|
def self.extract_all_from(root_node)
|
34
34
|
fields = []
|
35
35
|
root_node.each_recursive {|node|
|
@@ -75,6 +75,22 @@ class Button
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
+
class RadioButton
|
79
|
+
attr_accessor :name, :value, :checked
|
80
|
+
|
81
|
+
def initialize(name, value, checked)
|
82
|
+
@name, @value, @checked = name, value, checked
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class CheckBox
|
87
|
+
attr_accessor :name, :value, :checked
|
88
|
+
|
89
|
+
def initialize(name, value, checked)
|
90
|
+
@name, @value, @checked = name, value, checked
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
78
94
|
class SelectList
|
79
95
|
attr_accessor :name, :value, :options
|
80
96
|
|
@@ -113,7 +129,7 @@ class GlobalForm
|
|
113
129
|
attr_reader :form_node, :elements_node
|
114
130
|
attr_accessor :method, :action, :name
|
115
131
|
|
116
|
-
attr_reader :fields, :buttons, :file_uploads
|
132
|
+
attr_reader :fields, :buttons, :file_uploads, :radiobuttons, :checkboxes
|
117
133
|
|
118
134
|
def initialize(form_node, elements_node)
|
119
135
|
@form_node, @elements_node = form_node, elements_node
|
@@ -125,23 +141,62 @@ class GlobalForm
|
|
125
141
|
parse
|
126
142
|
end
|
127
143
|
|
144
|
+
def build_query
|
145
|
+
query = {}
|
146
|
+
|
147
|
+
fields().each do |f|
|
148
|
+
query[f.name] = f.value || ""
|
149
|
+
end
|
150
|
+
|
151
|
+
checkboxes().each do |f|
|
152
|
+
query[f.name] = f.value || "on" if f.checked
|
153
|
+
end
|
154
|
+
|
155
|
+
radio_groups = {}
|
156
|
+
radiobuttons().each do |f|
|
157
|
+
radio_groups[f.name] ||= []
|
158
|
+
radio_groups[f.name] << f
|
159
|
+
end
|
160
|
+
|
161
|
+
# take one radio button from each group
|
162
|
+
radio_groups.each_value do |g|
|
163
|
+
checked = g.select {|f| f.checked}
|
164
|
+
|
165
|
+
if checked.size == 1
|
166
|
+
f = checked.first
|
167
|
+
query[f.name] = f.value || ""
|
168
|
+
elsif checked.size > 1
|
169
|
+
raise "multiple radiobuttons are checked in the same group!"
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
query
|
174
|
+
end
|
175
|
+
|
128
176
|
def parse
|
129
177
|
@fields = []
|
130
178
|
@buttons = []
|
131
179
|
@file_uploads = []
|
180
|
+
@radiobuttons = []
|
181
|
+
@checkboxes = []
|
182
|
+
|
132
183
|
@elements_node.each_recursive {|node|
|
133
184
|
case node.name.downcase
|
134
185
|
when 'input'
|
135
186
|
case (node.attributes['type'] || '').downcase
|
136
|
-
when 'text', 'password', 'hidden', '
|
187
|
+
when 'text', 'password', 'hidden', 'int'
|
137
188
|
@fields << Field.new(node.attributes['name'], node.attributes['value'])
|
189
|
+
when 'radio'
|
190
|
+
@radiobuttons << RadioButton.new(node.attributes['name'], node.attributes['value'], node.attributes.has_key?('checked'))
|
191
|
+
when 'checkbox'
|
192
|
+
@checkboxes << CheckBox.new(node.attributes['name'], node.attributes['value'], node.attributes.has_key?('checked'))
|
138
193
|
when 'file'
|
139
194
|
@file_uploads << FileUpload.new(node.attributes['name'], node.attributes['value'])
|
140
195
|
when 'submit'
|
141
196
|
@buttons << Button.new(node.attributes['name'], node.attributes['value'])
|
142
197
|
end
|
143
|
-
when 'textarea'
|
144
|
-
@fields << Field.new(node.attributes['name'], node.
|
198
|
+
when 'textarea'
|
199
|
+
@fields << Field.new(node.attributes['name'], node.all_text)
|
145
200
|
when 'select'
|
146
201
|
@fields << SelectList.new(node.attributes['name'], node)
|
147
202
|
end
|
@@ -319,10 +374,8 @@ class Mechanize
|
|
319
374
|
end
|
320
375
|
|
321
376
|
def submit(form, button=nil)
|
322
|
-
query =
|
323
|
-
|
324
|
-
query[f.name] = f.value || ""
|
325
|
-
end
|
377
|
+
query = form.build_query
|
378
|
+
|
326
379
|
query[button.name] = button.value || "" if button and button.name
|
327
380
|
|
328
381
|
uri = to_absolute_uri(form.action)
|