Added wrappers around other methods of WWW::Mechanize::Links.
[perl4caml.git] / wrappers / pl_WWW_Mechanize.ml
index a29a5a9..d5a2376 100644 (file)
@@ -2,7 +2,7 @@
   *
   * Copyright (C) 2004 Merjis Ltd.
   *
-  * $Id: pl_WWW_Mechanize.ml,v 1.1 2004-11-25 21:24:51 rich Exp $
+  * $Id: pl_WWW_Mechanize.ml,v 1.5 2005-05-19 11:10:18 rich Exp $
   *)
 
 open Perl
@@ -49,18 +49,24 @@ object (self)
     add "n" sv_of_int n;
     call_method_void sv "follow_link" !args
 
-  (* XXX What do these next two functions return? *)
+  method forms =
+    let svlist = call_method_array sv "forms" [] in
+    List.map (new Pl_HTML_Form.html_form) svlist
   method form_number n =
-    call_method_void sv "form_number" [sv_of_int n]
+    let sv = call_method sv "form_number" [sv_of_int n] in
+    new Pl_HTML_Form.html_form sv
   method form_name name =
-    call_method_void sv "form_name" [sv_of_string name]
+    let sv = call_method sv "form_name" [sv_of_string name] in
+    new Pl_HTML_Form.html_form sv
 
   (* XXX There is an arrayref variant of this method, but what
    * it does is apparently undocumented.
    *)
-  method field name value n =
-    call_method_void sv "field" [sv_of_string name; sv_of_string value;
-                                sv_of_int n]
+  method field ?n name value =
+    let args = match n with
+       None -> [sv_of_string name; sv_of_string value]
+      | Some n -> [sv_of_string name; sv_of_string value; sv_of_int n] in
+    call_method_void sv "field" args
   method set_fields fields =
     let args = ref [] in
     List.iter (fun (k, v) ->
@@ -69,8 +75,11 @@ object (self)
     let args = List.rev !args in
     call_method_void sv "set_fields" args
 
-  method value name n =
-    let sv = call_method sv "value" [sv_of_string name; sv_of_int n] in
+  method value ?n name =
+    let args = match n with
+       None -> [sv_of_string name]
+      | Some n -> [sv_of_string name; sv_of_int n] in
+    let sv = call_method sv "value" args in
     string_of_sv sv
 
   (* XXX Doesn't support setting criteria. *)
@@ -146,9 +155,10 @@ object (self)
   method content =
     let sv = call_method sv "content" [] in
     string_of_sv sv
-  (* method forms = *)
   (* method current_forms = *)
-  (* method links = *)
+  method links =
+    let svs = call_method_array sv "links" [] in
+    List.map (new www_mechanize_link) svs
   method is_html =
     let sv = call_method sv "is_html" [] in
     bool_of_sv sv
@@ -177,8 +187,40 @@ object (self)
 
 end
 
+and www_mechanize_link sv =
+
+object (self)
+
+  method sv = sv
+
+  method url =
+    let sv = call_method sv "url" [] in
+    string_of_sv sv
+
+  method text =
+    let sv = call_method sv "text" [] in
+    string_of_sv sv
+
+  method name =
+    let sv = call_method sv "name" [] in
+    string_of_sv sv
+
+  method tag =
+    let sv = call_method sv "tag" [] in
+    string_of_sv sv
+
+  method base =
+    let sv = call_method sv "base" [] in
+    string_of_sv sv
+
+end
+
 (* XXX Should be able to pass args to constructor of LWP::UserAgent. *)
 (* XXX WWW::Mechanize has additional parameters. *)
-let new_ () =
-  let sv = call_class_method "WWW::Mechanize" "new" [] in
+let new_ ?autocheck () =
+  let args = ref [] in
+  let may f = function None -> () | Some v -> f v in
+  may (fun v ->
+        args := sv_of_string "autocheck" :: sv_of_bool v :: !args) autocheck;
+  let sv = call_class_method "WWW::Mechanize" "new" !args in
   new www_mechanize sv