kitamomonga-mechanize 0.9.3.20090724215219

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