tenderlove-mechanize 0.9.3.20090623142847 → 0.9.3.20090911221705

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. data/Manifest.txt +55 -48
  2. data/Rakefile +12 -22
  3. data/lib/mechanize.rb +618 -4
  4. data/lib/mechanize/chain.rb +33 -0
  5. data/lib/mechanize/chain/auth_headers.rb +78 -0
  6. data/lib/mechanize/chain/body_decoding_handler.rb +46 -0
  7. data/lib/mechanize/chain/connection_resolver.rb +76 -0
  8. data/lib/mechanize/chain/custom_headers.rb +21 -0
  9. data/lib/{www/mechanize → mechanize}/chain/handler.rb +1 -1
  10. data/lib/mechanize/chain/header_resolver.rb +51 -0
  11. data/lib/mechanize/chain/parameter_resolver.rb +22 -0
  12. data/lib/{www/mechanize → mechanize}/chain/post_connect_hook.rb +0 -0
  13. data/lib/mechanize/chain/pre_connect_hook.rb +20 -0
  14. data/lib/mechanize/chain/request_resolver.rb +30 -0
  15. data/lib/mechanize/chain/response_body_parser.rb +38 -0
  16. data/lib/mechanize/chain/response_header_handler.rb +48 -0
  17. data/lib/mechanize/chain/response_reader.rb +39 -0
  18. data/lib/mechanize/chain/ssl_resolver.rb +40 -0
  19. data/lib/mechanize/chain/uri_resolver.rb +75 -0
  20. data/lib/mechanize/content_type_error.rb +14 -0
  21. data/lib/mechanize/cookie.rb +70 -0
  22. data/lib/mechanize/cookie_jar.rb +188 -0
  23. data/lib/mechanize/file.rb +71 -0
  24. data/lib/mechanize/file_response.rb +60 -0
  25. data/lib/mechanize/file_saver.rb +37 -0
  26. data/lib/mechanize/form.rb +378 -0
  27. data/lib/mechanize/form/button.rb +9 -0
  28. data/lib/mechanize/form/check_box.rb +11 -0
  29. data/lib/mechanize/form/field.rb +30 -0
  30. data/lib/mechanize/form/file_upload.rb +22 -0
  31. data/lib/mechanize/form/image_button.rb +21 -0
  32. data/lib/mechanize/form/multi_select_list.rb +67 -0
  33. data/lib/mechanize/form/option.rb +49 -0
  34. data/lib/mechanize/form/radio_button.rb +49 -0
  35. data/lib/mechanize/form/select_list.rb +43 -0
  36. data/lib/mechanize/headers.rb +11 -0
  37. data/lib/mechanize/history.rb +65 -0
  38. data/lib/mechanize/inspect.rb +88 -0
  39. data/lib/{www/mechanize → mechanize}/monkey_patch.rb +4 -6
  40. data/lib/mechanize/page.rb +206 -0
  41. data/lib/mechanize/page/base.rb +8 -0
  42. data/lib/mechanize/page/frame.rb +20 -0
  43. data/lib/mechanize/page/image.rb +26 -0
  44. data/lib/mechanize/page/label.rb +20 -0
  45. data/lib/mechanize/page/link.rb +48 -0
  46. data/lib/mechanize/page/meta.rb +50 -0
  47. data/lib/mechanize/pluggable_parsers.rb +101 -0
  48. data/lib/mechanize/redirect_limit_reached_error.rb +16 -0
  49. data/lib/mechanize/redirect_not_get_or_head_error.rb +18 -0
  50. data/lib/mechanize/response_code_error.rb +22 -0
  51. data/lib/mechanize/unsupported_scheme_error.rb +8 -0
  52. data/lib/mechanize/util.rb +67 -0
  53. data/mechanize.gemspec +8 -8
  54. data/test/chain/test_argument_validator.rb +2 -2
  55. data/test/chain/test_auth_headers.rb +2 -2
  56. data/test/chain/test_custom_headers.rb +2 -2
  57. data/test/chain/test_header_resolver.rb +3 -3
  58. data/test/chain/test_parameter_resolver.rb +4 -4
  59. data/test/chain/test_request_resolver.rb +4 -4
  60. data/test/chain/test_response_reader.rb +3 -3
  61. data/test/helper.rb +1 -1
  62. data/test/htdocs/tc_bad_charset.html +9 -0
  63. data/test/htdocs/tc_charset.html +6 -0
  64. data/test/htdocs/test_bad_encoding.html +52 -0
  65. data/test/test_authenticate.rb +3 -3
  66. data/test/test_bad_links.rb +1 -1
  67. data/test/test_blank_form.rb +1 -1
  68. data/test/test_checkboxes.rb +1 -1
  69. data/test/test_content_type.rb +2 -2
  70. data/test/test_cookie_class.rb +12 -12
  71. data/test/test_cookie_jar.rb +13 -13
  72. data/test/test_cookies.rb +1 -1
  73. data/test/test_encoded_links.rb +1 -1
  74. data/test/test_errors.rb +2 -2
  75. data/test/test_follow_meta.rb +3 -3
  76. data/test/test_form_action.rb +1 -1
  77. data/test/test_form_as_hash.rb +1 -1
  78. data/test/test_form_button.rb +2 -2
  79. data/test/test_form_no_inputname.rb +1 -1
  80. data/test/test_forms.rb +1 -1
  81. data/test/test_frames.rb +1 -1
  82. data/test/test_get_headers.rb +1 -1
  83. data/test/test_gzipping.rb +2 -2
  84. data/test/test_hash_api.rb +1 -1
  85. data/test/test_history.rb +7 -7
  86. data/test/test_history_added.rb +1 -1
  87. data/test/test_html_unscape_forms.rb +7 -7
  88. data/test/test_if_modified_since.rb +1 -1
  89. data/test/test_keep_alive.rb +1 -1
  90. data/test/test_links.rb +2 -2
  91. data/test/test_mech.rb +2 -2
  92. data/test/test_mechanize_file.rb +7 -7
  93. data/test/test_meta.rb +2 -2
  94. data/test/test_multi_select.rb +1 -1
  95. data/test/test_no_attributes.rb +1 -1
  96. data/test/test_option.rb +1 -1
  97. data/test/test_page.rb +3 -3
  98. data/test/test_pluggable_parser.rb +14 -14
  99. data/test/test_post_form.rb +1 -1
  100. data/test/test_pretty_print.rb +2 -2
  101. data/test/test_radiobutton.rb +1 -1
  102. data/test/test_redirect_limit_reached.rb +1 -3
  103. data/test/test_redirect_verb_handling.rb +1 -3
  104. data/test/test_referer.rb +1 -1
  105. data/test/test_relative_links.rb +1 -1
  106. data/test/test_request.rb +1 -1
  107. data/test/test_response_code.rb +3 -3
  108. data/test/test_save_file.rb +3 -3
  109. data/test/test_scheme.rb +3 -3
  110. data/test/test_select.rb +2 -2
  111. data/test/test_select_all.rb +1 -1
  112. data/test/test_select_none.rb +1 -1
  113. data/test/test_select_noopts.rb +1 -1
  114. data/test/test_set_fields.rb +1 -1
  115. data/test/test_ssl_server.rb +1 -1
  116. data/test/test_subclass.rb +1 -1
  117. data/test/test_textarea.rb +1 -1
  118. data/test/test_upload.rb +1 -1
  119. data/test/test_verbs.rb +1 -1
  120. metadata +61 -56
  121. data/lib/www/mechanize.rb +0 -619
  122. data/lib/www/mechanize/chain.rb +0 -34
  123. data/lib/www/mechanize/chain/auth_headers.rb +0 -80
  124. data/lib/www/mechanize/chain/body_decoding_handler.rb +0 -48
  125. data/lib/www/mechanize/chain/connection_resolver.rb +0 -78
  126. data/lib/www/mechanize/chain/custom_headers.rb +0 -23
  127. data/lib/www/mechanize/chain/header_resolver.rb +0 -53
  128. data/lib/www/mechanize/chain/parameter_resolver.rb +0 -24
  129. data/lib/www/mechanize/chain/pre_connect_hook.rb +0 -22
  130. data/lib/www/mechanize/chain/request_resolver.rb +0 -32
  131. data/lib/www/mechanize/chain/response_body_parser.rb +0 -40
  132. data/lib/www/mechanize/chain/response_header_handler.rb +0 -50
  133. data/lib/www/mechanize/chain/response_reader.rb +0 -41
  134. data/lib/www/mechanize/chain/ssl_resolver.rb +0 -42
  135. data/lib/www/mechanize/chain/uri_resolver.rb +0 -77
  136. data/lib/www/mechanize/content_type_error.rb +0 -16
  137. data/lib/www/mechanize/cookie.rb +0 -72
  138. data/lib/www/mechanize/cookie_jar.rb +0 -191
  139. data/lib/www/mechanize/file.rb +0 -73
  140. data/lib/www/mechanize/file_response.rb +0 -62
  141. data/lib/www/mechanize/file_saver.rb +0 -39
  142. data/lib/www/mechanize/form.rb +0 -360
  143. data/lib/www/mechanize/form/button.rb +0 -8
  144. data/lib/www/mechanize/form/check_box.rb +0 -13
  145. data/lib/www/mechanize/form/field.rb +0 -28
  146. data/lib/www/mechanize/form/file_upload.rb +0 -24
  147. data/lib/www/mechanize/form/image_button.rb +0 -23
  148. data/lib/www/mechanize/form/multi_select_list.rb +0 -69
  149. data/lib/www/mechanize/form/option.rb +0 -51
  150. data/lib/www/mechanize/form/radio_button.rb +0 -38
  151. data/lib/www/mechanize/form/select_list.rb +0 -45
  152. data/lib/www/mechanize/headers.rb +0 -12
  153. data/lib/www/mechanize/history.rb +0 -67
  154. data/lib/www/mechanize/inspect.rb +0 -90
  155. data/lib/www/mechanize/page.rb +0 -181
  156. data/lib/www/mechanize/page/base.rb +0 -10
  157. data/lib/www/mechanize/page/frame.rb +0 -22
  158. data/lib/www/mechanize/page/link.rb +0 -50
  159. data/lib/www/mechanize/page/meta.rb +0 -51
  160. data/lib/www/mechanize/pluggable_parsers.rb +0 -103
  161. data/lib/www/mechanize/redirect_limit_reached_error.rb +0 -18
  162. data/lib/www/mechanize/redirect_not_get_or_head_error.rb +0 -20
  163. data/lib/www/mechanize/response_code_error.rb +0 -25
  164. data/lib/www/mechanize/unsupported_scheme_error.rb +0 -10
  165. data/lib/www/mechanize/util.rb +0 -76
@@ -1,8 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents a Submit button in a form.
5
- class Button < Field; end
6
- end
7
- end
8
- end
@@ -1,13 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents a check box found in a Form. To activate the
5
- # CheckBox in the Form, set the checked method to true.
6
- class CheckBox < RadioButton
7
- def query_value
8
- [[@name, @value || "on"]]
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,28 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents a field in a form. It handles the following input
5
- # tags found in a form:
6
- # text, password, hidden, int, textarea
7
- #
8
- # To set the value of a field, just use the value method:
9
- # field.value = "foo"
10
- class Field
11
- attr_accessor :name, :value
12
-
13
- def initialize(name, value)
14
- @name = Util.html_unescape(name)
15
- @value = if value.is_a? String
16
- Util.html_unescape(value)
17
- else
18
- value
19
- end
20
- end
21
-
22
- def query_value
23
- [[@name, @value || '']]
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,24 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents a file upload field found in a form. To use this
5
- # class, set WWW::FileUpload#file_data= to the data of the file you want
6
- # to upload and WWW::FileUpload#mime_type= to the appropriate mime type
7
- # of the file.
8
- # See the example in EXAMPLES[link://files/EXAMPLES_txt.html]
9
- class FileUpload < Field
10
- attr_accessor :file_name # File name
11
- attr_accessor :mime_type # Mime Type (Optional)
12
-
13
- alias :file_data :value
14
- alias :file_data= :value=
15
-
16
- def initialize(name, file_name)
17
- @file_name = Util.html_unescape(file_name)
18
- @file_data = nil
19
- super(name, @file_data)
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,23 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents an image button in a form. Use the x and y methods
5
- # to set the x and y positions for where the mouse "clicked".
6
- class ImageButton < Button
7
- attr_accessor :x, :y
8
-
9
- def initialize(name, value)
10
- @x = nil
11
- @y = nil
12
- super(name, value)
13
- end
14
-
15
- def query_value
16
- super <<
17
- [@name + ".x", (@x || 0).to_s] <<
18
- [@name + ".y", (@y || 0).to_s]
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,69 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents a select list where multiple values can be selected.
5
- # MultiSelectList#value= accepts an array, and those values are used as
6
- # values for the select list. For example, to select multiple values,
7
- # simply do this:
8
- # list.value = ['one', 'two']
9
- # Single values are still supported, so these two are the same:
10
- # list.value = ['one']
11
- # list.value = 'one'
12
- class MultiSelectList < Field
13
- attr_accessor :options
14
-
15
- def initialize(name, node)
16
- value = []
17
- @options = []
18
-
19
- # parse
20
- node.search('option').each do |n|
21
- option = Option.new(n, self)
22
- @options << option
23
- end
24
- super(name, value)
25
- end
26
-
27
- def query_value
28
- value ? value.collect { |v| [name, v] } : ''
29
- end
30
-
31
- # Select no options
32
- def select_none
33
- @value = []
34
- options.each { |o| o.untick }
35
- end
36
-
37
- # Select all options
38
- def select_all
39
- @value = []
40
- options.each { |o| o.tick }
41
- end
42
-
43
- # Get a list of all selected options
44
- def selected_options
45
- @options.find_all { |o| o.selected? }
46
- end
47
-
48
- def value=(values)
49
- select_none
50
- [values].flatten.each do |value|
51
- option = options.find { |o| o.value == value }
52
- if option.nil?
53
- @value.push(value)
54
- else
55
- option.select
56
- end
57
- end
58
- end
59
-
60
- def value
61
- value = []
62
- value.push(*@value)
63
- value.push(*selected_options.collect { |o| o.value })
64
- value
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,51 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class contains option an option found within SelectList. A
5
- # SelectList can have many Option classes associated with it. An option
6
- # can be selected by calling Option#tick, or Option#click. For example,
7
- # select the first option in a list:
8
- # select_list.first.tick
9
- class Option
10
- attr_reader :value, :selected, :text, :select_list
11
-
12
- alias :to_s :value
13
- alias :selected? :selected
14
-
15
- def initialize(node, select_list)
16
- @text = node.inner_text
17
- @value = Util.html_unescape(node['value'] || node.inner_text)
18
- @selected = node.has_attribute? 'selected'
19
- @select_list = select_list # The select list this option belongs to
20
- end
21
-
22
- # Select this option
23
- def select
24
- unselect_peers
25
- @selected = true
26
- end
27
-
28
- # Unselect this option
29
- def unselect
30
- @selected = false
31
- end
32
-
33
- alias :tick :select
34
- alias :untick :unselect
35
-
36
- # Toggle the selection value of this option
37
- def click
38
- unselect_peers
39
- @selected = !@selected
40
- end
41
-
42
- private
43
- def unselect_peers
44
- if @select_list.instance_of? SelectList
45
- @select_list.select_none
46
- end
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,38 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents a radio button found in a Form. To activate the
5
- # RadioButton in the Form, set the checked method to true.
6
- class RadioButton < Field
7
- attr_accessor :checked
8
-
9
- def initialize(name, value, checked, form)
10
- @checked = checked
11
- @form = form
12
- super(name, value)
13
- end
14
-
15
- def check
16
- uncheck_peers
17
- @checked = true
18
- end
19
-
20
- def uncheck
21
- @checked = false
22
- end
23
-
24
- def click
25
- checked ? uncheck : check
26
- end
27
-
28
- private
29
- def uncheck_peers
30
- @form.radiobuttons_with(:name => name).each do |b|
31
- next if b.value == value
32
- b.uncheck
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,45 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Form
4
- # This class represents a select list or drop down box in a Form. Set the
5
- # value for the list by calling SelectList#value=. SelectList contains a
6
- # list of Option that were found. After finding the correct option, set
7
- # the select lists value to the option value:
8
- # selectlist.value = selectlist.options.first.value
9
- # Options can also be selected by "clicking" or selecting them. See Option
10
- class SelectList < MultiSelectList
11
- def initialize(name, node)
12
- super(name, node)
13
- if selected_options.length > 1
14
- selected_options.reverse[1..selected_options.length].each do |o|
15
- o.unselect
16
- end
17
- end
18
- end
19
-
20
- def value
21
- value = super
22
- if value.length > 0
23
- value.last
24
- elsif @options.length > 0
25
- @options.first.value
26
- else
27
- nil
28
- end
29
- end
30
-
31
- def value=(new)
32
- if new != new.to_s and new.respond_to? :first
33
- super([new.first])
34
- else
35
- super([new.to_s])
36
- end
37
- end
38
-
39
- def query_value
40
- value ? [[name, value]] : nil
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,12 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- class Headers < Hash
4
- def [](key)
5
- super(key.downcase)
6
- end
7
- def []=(key, value)
8
- super(key.downcase, value)
9
- end
10
- end
11
- end
12
- end
@@ -1,67 +0,0 @@
1
- module WWW
2
- class Mechanize
3
- ##
4
- # This class manages history for your mechanize object.
5
- class History < Array
6
- attr_accessor :max_size
7
-
8
- def initialize(max_size = nil)
9
- @max_size = max_size
10
- @history_index = {}
11
- end
12
-
13
- def initialize_copy(orig)
14
- super
15
- @history_index = orig.instance_variable_get(:@history_index).dup
16
- end
17
-
18
- def push(page, uri = nil)
19
- super(page)
20
- @history_index[(uri ? uri : page.uri).to_s] = page
21
- if @max_size && self.length > @max_size
22
- while self.length > @max_size
23
- self.shift
24
- end
25
- end
26
- self
27
- end
28
- alias :<< :push
29
-
30
- def visited?(url)
31
- ! visited_page(url).nil?
32
- end
33
-
34
- def visited_page(url)
35
- @history_index[(url.respond_to?(:uri) ? url.uri : url).to_s]
36
- end
37
-
38
- def clear
39
- @history_index.clear
40
- super
41
- end
42
-
43
- def shift
44
- return nil if length == 0
45
- page = self[0]
46
- self[0] = nil
47
- super
48
- remove_from_index(page)
49
- page
50
- end
51
-
52
- def pop
53
- return nil if length == 0
54
- page = super
55
- remove_from_index(page)
56
- page
57
- end
58
-
59
- private
60
- def remove_from_index(page)
61
- @history_index.each do |k,v|
62
- @history_index.delete(k) if v == page
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,90 +0,0 @@
1
- require 'pp'
2
-
3
- module WWW
4
- # :stopdoc:
5
- class Mechanize
6
- def pretty_print(q)
7
- q.object_group(self) {
8
- q.breakable
9
- q.pp cookie_jar
10
- q.breakable
11
- q.pp current_page
12
- }
13
- end
14
-
15
- class Page
16
- def pretty_print(q)
17
- q.object_group(self) {
18
- q.breakable
19
- q.group(1, '{url', '}') {q.breakable; q.pp uri }
20
- q.breakable
21
- q.group(1, '{meta', '}') {
22
- meta.each { |link| q.breakable; q.pp link }
23
- }
24
- q.breakable
25
- q.group(1, '{title', '}') { q.breakable; q.pp title }
26
- q.breakable
27
- q.group(1, '{iframes', '}') {
28
- iframes.each { |link| q.breakable; q.pp link }
29
- }
30
- q.breakable
31
- q.group(1, '{frames', '}') {
32
- frames.each { |link| q.breakable; q.pp link }
33
- }
34
- q.breakable
35
- q.group(1, '{links', '}') {
36
- links.each { |link| q.breakable; q.pp link }
37
- }
38
- q.breakable
39
- q.group(1, '{forms', '}') {
40
- forms.each { |form| q.breakable; q.pp form }
41
- }
42
- }
43
- end
44
-
45
- class Link
46
- def pretty_print(q)
47
- q.object_group(self) {
48
- q.breakable; q.pp text
49
- q.breakable; q.pp href
50
- }
51
- end
52
- end
53
- end
54
-
55
- class Form
56
- def pretty_print(q)
57
- q.object_group(self) {
58
- q.breakable; q.group(1, '{name', '}') { q.breakable; q.pp name }
59
- q.breakable; q.group(1, '{method', '}') { q.breakable; q.pp method }
60
- q.breakable; q.group(1, '{action', '}') { q.breakable; q.pp action }
61
- q.breakable; q.group(1, '{fields', '}') {
62
- fields.each do |field|
63
- q.breakable
64
- q.pp field
65
- end
66
- }
67
- q.breakable; q.group(1, '{radiobuttons', '}') {
68
- radiobuttons.each { |b| q.breakable; q.pp b }
69
- }
70
- q.breakable; q.group(1, '{checkboxes', '}') {
71
- checkboxes.each { |b| q.breakable; q.pp b }
72
- }
73
- q.breakable; q.group(1, '{file_uploads', '}') {
74
- file_uploads.each { |b| q.breakable; q.pp b }
75
- }
76
- q.breakable; q.group(1, '{buttons', '}') {
77
- buttons.each { |b| q.breakable; q.pp b }
78
- }
79
- }
80
- end
81
-
82
- class RadioButton
83
- def pretty_print_instance_variables
84
- [:@checked, :@name, :@value]
85
- end
86
- end
87
- end
88
- end
89
- # :startdoc:
90
- end