mechanize 0.2.1 → 0.2.2
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 +16 -5
- data/lib/mechanize/parsing.rb +1 -1
- data/test/test_parsing.rb +38 -0
- metadata +3 -3
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.2"
|
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,14 +26,16 @@ class Field
|
|
26
26
|
|
27
27
|
def initialize(name, value)
|
28
28
|
@name, @value = name, value
|
29
|
+
@user, @password = nil
|
29
30
|
end
|
30
31
|
|
31
32
|
# Returns an array of Field objects
|
32
33
|
def self.extract_all_from(root_node)
|
33
34
|
fields = []
|
34
35
|
root_node.each_recursive {|node|
|
35
|
-
if node.name.downcase == 'input' and
|
36
|
-
|
36
|
+
if (node.name.downcase == 'input' and
|
37
|
+
%w(text password hidden checkbox radio int).include?(node.attributes['type'].downcase)) or
|
38
|
+
%w(textarea option).include?(node.name.downcase)
|
37
39
|
fields << Field.new(node.attributes['name'], node.attributes['value'])
|
38
40
|
end
|
39
41
|
}
|
@@ -131,13 +133,15 @@ class GlobalForm
|
|
131
133
|
case node.name.downcase
|
132
134
|
when 'input'
|
133
135
|
case (node.attributes['type'] || '').downcase
|
134
|
-
when 'text', 'password', 'hidden', 'checkbox', 'radio'
|
136
|
+
when 'text', 'password', 'hidden', 'checkbox', 'radio', 'int'
|
135
137
|
@fields << Field.new(node.attributes['name'], node.attributes['value'])
|
136
138
|
when 'file'
|
137
139
|
@file_uploads << FileUpload.new(node.attributes['name'], node.attributes['value'])
|
138
140
|
when 'submit'
|
139
141
|
@buttons << Button.new(node.attributes['name'], node.attributes['value'])
|
140
142
|
end
|
143
|
+
when 'textarea', 'select'
|
144
|
+
@fields << Field.new(node.attributes['name'], node.attributes['value'])
|
141
145
|
when 'select'
|
142
146
|
@fields << SelectList.new(node.attributes['name'], node)
|
143
147
|
end
|
@@ -277,6 +281,11 @@ class Mechanize
|
|
277
281
|
self.user_agent = AGENT_ALIASES[al] || raise("unknown agent alias")
|
278
282
|
end
|
279
283
|
|
284
|
+
def basic_authetication(user, password)
|
285
|
+
@user = user
|
286
|
+
@password = password
|
287
|
+
end
|
288
|
+
|
280
289
|
def get(url)
|
281
290
|
cur_page = current_page() || Page.new
|
282
291
|
|
@@ -314,7 +323,7 @@ class Mechanize
|
|
314
323
|
form.fields.each do |f|
|
315
324
|
query[f.name] = f.value || ""
|
316
325
|
end
|
317
|
-
query[button.name] = button.value || "" if button
|
326
|
+
query[button.name] = button.value || "" if button and button.name
|
318
327
|
|
319
328
|
uri = to_absolute_uri(form.action)
|
320
329
|
case form.method.upcase
|
@@ -391,6 +400,8 @@ class Mechanize
|
|
391
400
|
|
392
401
|
request.add_header('User-Agent', @user_agent) if @user_agent
|
393
402
|
|
403
|
+
request.basic_auth(@user, @password) if @user
|
404
|
+
|
394
405
|
# Invoke pre-request-hook (use it to add custom headers or content)
|
395
406
|
|
396
407
|
pre_request_hook.call(request) if pre_request_hook
|
data/lib/mechanize/parsing.rb
CHANGED
data/test/test_parsing.rb
CHANGED
@@ -9,6 +9,44 @@ class TestParsing < Test::Unit::TestCase
|
|
9
9
|
assert_equal ["asdf", ["abc"], "def"], root_for("<i>asdf<b>abc</b>def</i>").collect_text_recursively
|
10
10
|
end
|
11
11
|
|
12
|
+
def test_index_in_parent
|
13
|
+
table = root_for %(<table><tr><td>A</td><td>B</td></tr><tr><td colspan="2">C</td></tr></table>)
|
14
|
+
node = table.find_first_recursive {|n| n.name == 'tr'}
|
15
|
+
assert_equal node, node.parent.elements[node.index_in_parent]
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_table_to_matrix
|
19
|
+
table = root_for %(<table><tr><td>A</td><td>B</td></tr><tr><td colspan="2">C</td></tr></table>)
|
20
|
+
matrix = table_to_matrix(table)
|
21
|
+
assert_equal "A", matrix[0][0].all_text
|
22
|
+
assert_equal "B", matrix[0][1].all_text
|
23
|
+
assert_equal "C", matrix[1][0].all_text
|
24
|
+
assert_equal nil, matrix[1][1]
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_extract_from_table
|
28
|
+
table = root_for %(
|
29
|
+
<table>
|
30
|
+
<tr>
|
31
|
+
<td>A</td>
|
32
|
+
<td>B</td>
|
33
|
+
<td>C</td>
|
34
|
+
</tr>
|
35
|
+
<tr>
|
36
|
+
<td>A.1</td>
|
37
|
+
<td>B.1</td>
|
38
|
+
<td>C.1</td>
|
39
|
+
</tr>
|
40
|
+
<tr>
|
41
|
+
<td>A.2</td>
|
42
|
+
<td>B.2</td>
|
43
|
+
<td>C.2</td>
|
44
|
+
</tr>
|
45
|
+
</table>)
|
46
|
+
|
47
|
+
assert_equal [ ["A.1", "C.1"], ["A.2", "C.2"] ], extract_from_table(table, ["A", "C"])
|
48
|
+
end
|
49
|
+
|
12
50
|
private
|
13
51
|
|
14
52
|
def root_for(str)
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.
|
2
|
+
rubygems_version: 0.8.4
|
3
3
|
specification_version: 1
|
4
4
|
name: mechanize
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.2.
|
7
|
-
date: 2005-04-
|
6
|
+
version: 0.2.2
|
7
|
+
date: 2005-04-17
|
8
8
|
summary: Automated web-browsing.
|
9
9
|
require_paths:
|
10
10
|
- lib
|