1 (** Wrapper around Perl [WWW::Mechanize] class.
3 * Copyright (C) 2004 Merjis Ltd.
5 * $Id: pl_WWW_Mechanize.ml,v 1.1 2004-11-25 21:24:51 rich Exp $
12 let _ = eval "use WWW::Mechanize"
14 class www_mechanize sv =
17 inherit lwp_useragent sv
19 method agent_alias alias =
20 call_method_void sv "agent_alias" [sv_of_string alias]
21 method known_agent_aliases =
22 let svlist = call_method_array sv "known_agent_aliases" [] in
23 List.map string_of_sv svlist
26 call_method_void sv "get" [sv_of_string url]
28 call_method_void sv "reload" []
30 call_method_void sv "back" []
32 method follow_link ?text ?text_regex ?url ?url_regex ?url_abs ?url_abs_regex
33 ?name ?name_regex ?tag ?tag_regex ?n () =
35 let add name f = function
37 | Some p -> args := sv_of_string name :: f p :: !args
39 add "text" sv_of_string text;
40 add "text_regex" sv_of_string text_regex;
41 add "url" sv_of_string url;
42 add "url_regex" sv_of_string url_regex;
43 add "url_abs" sv_of_string url_abs;
44 add "url_abs_regex" sv_of_string url_abs_regex;
45 add "name" sv_of_string name;
46 add "name_regex" sv_of_string name_regex;
47 add "tag" sv_of_string tag;
48 add "tag_regex" sv_of_string tag_regex;
50 call_method_void sv "follow_link" !args
52 (* XXX What do these next two functions return? *)
53 method form_number n =
54 call_method_void sv "form_number" [sv_of_int n]
55 method form_name name =
56 call_method_void sv "form_name" [sv_of_string name]
58 (* XXX There is an arrayref variant of this method, but what
59 * it does is apparently undocumented.
61 method field name value n =
62 call_method_void sv "field" [sv_of_string name; sv_of_string value;
64 method set_fields fields =
66 List.iter (fun (k, v) ->
67 (* Note: reversed k, v because we'll reverse the whole list.*)
68 args := sv_of_string v :: sv_of_string k :: !args) fields;
69 let args = List.rev !args in
70 call_method_void sv "set_fields" args
73 let sv = call_method sv "value" [sv_of_string name; sv_of_int n] in
76 (* XXX Doesn't support setting criteria. *)
77 method set_visible names =
78 let names = List.map sv_of_string names in
79 call_method_void sv "set_visible" names
81 method tick ?set name value =
82 let args = match set with
83 None -> [ sv_of_string name; sv_of_string value ]
84 | Some b -> [ sv_of_string name; sv_of_string value; sv_of_bool b ] in
85 call_method_void sv "tick" args
87 method untick name value =
88 call_method_void sv "untick" [ sv_of_string name; sv_of_string value ]
90 method click ?xy button =
91 let args = match xy with
92 None -> [ sv_of_string button ]
93 | Some (x, y) -> [ sv_of_string button; sv_of_int x; sv_of_int y ] in
94 call_method_void sv "click" args
96 call_method_void sv "click" []
97 method click_button ?name ?number ?value ?xy () =
99 let add name f = function
101 | Some p -> args := sv_of_string name :: f p :: !args
103 add "name" sv_of_string name;
104 add "number" sv_of_int number;
105 add "value" sv_of_string value;
109 args := sv_of_string "x" :: sv_of_int x ::
110 sv_of_string "y" :: sv_of_int y :: !args);
111 call_method_void sv "click" !args
113 method select name value =
114 call_method_void sv "select" [ sv_of_string name; sv_of_string value ]
115 method select_multiple name values =
116 let av = av_empty () in
117 List.iter (av_push av) (List.map sv_of_string values);
118 call_method_void sv "select" [ sv_of_string name; arrayref av ]
121 call_method_void sv "submit" []
123 (*method submit_form ?form_number ?form_name ?fields ?button ?xy () *)
126 let sv = call_method sv "success" [] in
129 let sv = call_method sv "uri" [] in
132 let sv = call_method sv "response" [] in
133 new Pl_HTTP_Response.http_response sv
135 let sv = call_method sv "res" [] in
136 new Pl_HTTP_Response.http_response sv
138 let sv = call_method sv "status" [] in
141 let sv = call_method sv "ct" [] in
144 let sv = call_method sv "base" [] in
147 let sv = call_method sv "content" [] in
150 (* method current_forms = *)
153 let sv = call_method sv "is_html" [] in
156 let sv = call_method sv "title" [] in
159 (* method find_link .... = *)
160 (* method find_all_links .... = *)
162 (* method add_header .... = *)
163 (* method delete_header .... = *)
166 let sv = call_method sv "quiet" [] in
169 call_method_void sv "quiet" [sv_of_bool b]
170 (* method stack_depth ... = *)
172 let sv = call_method sv "redirect_ok" [] in
175 (* method request ... = *)
176 (* method update_html ... = *)
180 (* XXX Should be able to pass args to constructor of LWP::UserAgent. *)
181 (* XXX WWW::Mechanize has additional parameters. *)
183 let sv = call_class_method "WWW::Mechanize" "new" [] in