Initial import
[Net-Plesk.git] / lib / Net / Plesk / Response.pm
1 package Net::Plesk::Response;
2
3 use strict;
4 use XML::Simple;
5 use XML::XPath;
6 use XML::XPath::XMLParser;
7
8 =head1 NAME
9
10 Net::Plesk::Response - Plesk response object
11
12 =head1 SYNOPSIS
13
14   my $response = $plesk->some_method( $and, $args );
15
16   if ( $response->is_success ) {
17
18     my $id  = $response->id;
19     #...
20
21   } else {
22
23     my $error = $response->error; #error code
24     my $errortext = $response->errortext; #error message
25     #...
26   }
27
28 =head1 DESCRIPTION
29
30 The "Net::Plesk::Response" class represents Plesk responses.
31
32 =cut
33
34 sub new {
35   my $proto = shift;
36   my $class = ref($proto) || $proto;
37   my $self = {};
38   bless($self, $class);
39
40   my $data = shift;
41   $data =~ /^\<\?xml version=\"1.0\"\?\>(.*)$/s; 
42
43   my $xp = XML::XPath->new(xml => $1);
44   my $nodeset = $xp->find('//result');
45   foreach my $node ($nodeset->get_nodelist) {
46     push @{$self->{'results'}}, XML::XPath::XMLParser::as_string($node);
47   }
48
49   $self;
50 }
51
52 sub is_success { 
53   my $self = shift;
54   my $status = 1;
55   foreach my $result (@{$self->{'results'}}) {
56     $status = (XMLin($result)->{'status'} eq 'ok');
57     last unless $status;
58   }
59   $status;
60 }
61
62 sub error {
63   my $self = shift;
64   my @errcode;
65   foreach my $result (@{$self->{'results'}}) {
66     my $errcode = XMLin($result)->{'errcode'};
67     push @errcode, $errcode if $errcode;
68   }
69   return wantarray ? @errcode : $errcode[0];
70 }
71
72 sub errortext {
73   my $self = shift;
74   my @errtext;
75   foreach my $result (@{$self->{'results'}}) {
76     my $errtext = XMLin($result)->{'errtext'};
77     push @errtext, $errtext if $errtext;
78   }
79   return wantarray ? @errtext : $errtext[0];
80 }
81
82 sub id {
83   my $self = shift;
84   my @id;
85   foreach my $result (@{$self->{'results'}}) {
86     my $id = XMLin($result)->{'id'};
87     push @id, $id if $id;
88   }
89   return wantarray ? @id : $id[0];
90 }
91
92
93 =head1 BUGS
94
95 Needs better documentation.
96
97 =head1 SEE ALSO
98
99 L<Net::Plesk>,
100
101 =head1 AUTHOR
102
103 Jeff Finucane E<lt>jeff@cmh.netE<gt>
104
105 =head1 COPYRIGHT AND LICENSE
106
107 Copyright (C) 2006 Jeff Finucane
108
109 This library is free software; you can redistribute it and/or modify
110 it under the same terms as Perl itself.
111
112 =cut
113
114 1;