Import original source of WebService-Northern911 0.1
[WebService-Northern911.git] / lib / WebService / Northern911 / Response.pm
1 package WebService::Northern911::Response;
2
3 use strict;
4
5 sub new {
6   my $class = shift;
7   my $self;
8
9   my $tree = shift;
10   $tree = $tree->{'parameters'} if exists $tree->{'parameters'};
11   my ($key) = grep /Result$/, keys(%$tree); # should only ever be one key
12   $tree = $tree->{$key} or die "can't parse transaction result";
13
14   my $error_message = join("\n",
15     map { $_->{ErrorMessage} }
16     @{ $tree->{Errors}{Error} }
17   );
18
19   $self = {
20     is_success    => $tree->{Accepted},
21     error_message => $error_message,
22   };
23
24   if ( $key eq 'QueryCustomerResult' ) {
25     if ($tree->{Accepted}) {
26       $self->{customer} = $tree->{Customer};
27     } elsif ( $tree->{Errors}{Error}[0]{ErrorCode} == 101 ) {
28       # 101 = Customer does not exist.  But the query was successful...
29       $self->{is_success} = 1;
30     }
31   } elsif ( $key eq 'GetVendorDumpURL' ) {
32     $self->{url} = $tree->{VendorDumpURL};
33   }
34
35   bless $self, $class;
36 }
37
38 sub is_success {
39   $_[0]->{is_success};
40 }
41
42 sub error_message {
43   $_[0]->{error_message};
44 }
45
46 sub customer {
47   $_[0]->{customer};
48 }
49
50 sub url {
51   $_[0]->{url};
52 }
53
54 =head1 NAME
55
56 WebService::Northern911::Response - Response object returned by
57 WebService::Northern911 API calls
58
59 =head1 METHODS
60
61 =over 4
62
63 =item is_success
64
65 1 if the API method was a success, 0 if not.  If it's 0, C<error_message>
66 will contain any error returned.  Note that we report the C<QueryCustomer>
67 method as a success if it returns a negative result; in that case, 
68 C<customer> will be undef.
69
70 =item error_message
71
72 Any error messages returned from the web service, separated by newlines.
73
74 =item customer
75
76 For C<QueryCustomer> calls, returns a hashref of the customer information
77 per the field names used by C<AddorUpdateCustomer>.
78
79 =item url
80
81 For C<GetVendorDumpURL> calls, returns the URL where the data dump can be
82 downloaded.
83
84 =back
85
86 =cut
87
88 1;