Added proper LGPL statements to all files.
[perl4caml.git] / wrappers / pl_WWW_Mechanize.ml
index 2582b9d..03151b7 100644 (file)
@@ -1,8 +1,22 @@
-(** Wrapper around Perl [WWW::Mechanize] class.
-  *
-  * Copyright (C) 2004 Merjis Ltd.
-  *
-  * $Id: pl_WWW_Mechanize.ml,v 1.2 2004-11-26 13:33:10 rich Exp $
+(** Wrapper around Perl [WWW::Mechanize] class. *)
+(*  Copyright (C) 2003 Merjis Ltd.
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Library General Public
+    License as published by the Free Software Foundation; either
+    version 2 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this library; see the file COPYING.  If not, write to
+    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+    Boston, MA 02111-1307, USA.
+
+    $Id: pl_WWW_Mechanize.ml,v 1.8 2008-03-01 13:02:21 rich Exp $
   *)
 
 open Perl
@@ -129,7 +143,33 @@ object (self)
   method submit () =
     call_method_void sv "submit" []
 
-  (*method submit_form ?form_number ?form_name ?fields ?button ?xy () *)
+  method submit_form ?form_number ?form_name ?fields ?button ?xy () =
+    let args = ref [] in
+    let add name f = function
+      | None -> ()
+      | Some p -> args := sv_of_string name :: f p :: !args
+    in
+    add "form_number" sv_of_int form_number;
+    add "form_name" sv_of_string form_name;
+    (match fields with
+     | None -> ()
+     | Some fields ->
+        let hv = hv_empty () in
+        List.iter (
+          fun (name, value) ->
+            hv_set hv name (sv_of_string value)
+        ) fields;
+        let sv = hashref hv in
+        args := sv_of_string "fields" :: sv :: !args
+    );
+    add "button" sv_of_string button;
+    (match xy with
+     | None -> ()
+     | Some (x, y) ->
+        args := sv_of_string "x" :: sv_of_int x ::
+          sv_of_string "y" :: sv_of_int y :: !args);
+    let sv = call_method sv "submit_form" !args in
+    new Pl_HTTP_Response.http_response sv
 
   method success =
     let sv = call_method sv "success" [] in
@@ -156,7 +196,9 @@ object (self)
     let sv = call_method sv "content" [] in
     string_of_sv sv
   (* 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
@@ -185,8 +227,44 @@ 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
+
+  method url_abs =
+    let sv = call_method sv "url_abs" [] 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