File Coverage

blib/lib/Mail/Karmasphere/Response.pm
Criterion Covered Total %
statement 9 88 10.2
branch 0 46 0.0
condition n/a
subroutine 3 17 17.6
pod 6 13 46.1
total 18 164 10.9


line stmt bran cond sub pod time code
1             package Mail::Karmasphere::Response;
2              
3 5     5   24 use strict;
  5         9  
  5         154  
4 5     5   24 use warnings;
  5         10  
  5         112  
5 5     5   20 use Exporter;
  5         7  
  5         4981  
6              
7             sub new {
8 0     0 0   my $class = shift;
9 0 0         my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
  0            
10 0           return bless $self, $class;
11             }
12              
13             sub id {
14 0     0 1   my ($self) = @_;
15 0 0         $self->{_} = '???' unless defined $self->{_};
16 0           return $self->{_};
17             }
18              
19             sub time {
20 0     0 1   my $self = shift;
21 0 0         return $self->{t} if defined $self->{t};
22 0 0         return $self->{'time'} if defined $self->{'time'};
23 0           return '???';
24             }
25              
26             # deprecated
27             sub facts {
28 0     0 1   my $self = shift;
29 0 0         return $self->{f} if exists $self->{f};
30 0 0         return $self->{facts} if exists $self->{facts};
31 0           $self->{f} = [];
32 0           return $self->{f};
33             }
34              
35             sub attributes {
36 0     0 0   my $self = shift;
37 0 0         return $self->{f} if exists $self->{f};
38 0 0         return $self->{facts} if exists $self->{facts};
39 0           $self->{f} = [];
40 0           return $self->{f};
41             }
42              
43             sub combinations {
44 0     0 0   my $self = shift;
45 0 0         return $self->{c} if exists $self->{c};
46 0 0         return $self->{combiners} if exists $self->{combiners};
47 0           $self->{c} = {};
48 0           return $self->{c};
49             }
50              
51             sub combination {
52 0     0 1   my ($self, $combiner) = @_;
53 0 0         return () unless $self->combinations;
54 0           return $self->combinations->{$combiner};
55             }
56              
57             sub combiner_names {
58 0     0 0   my ($self) = @_;
59 0 0         return () unless $self->combinations;
60 0           return keys %{ $self->combinations };
  0            
61             }
62              
63             sub _combination_get {
64 0     0     my ($self, $key, $combiner) = @_;
65 0 0         return undef unless $self->combinations;
66 0 0         if ($combiner) {
67 0           my $combination = $self->combination($combiner);
68 0 0         return undef unless $combination;
69 0           return $combination->{$key};
70             }
71 0           my $combination = $self->combination('default');
72 0 0         if ($combination) {
73 0           return $combination->{$key};
74             }
75 0           foreach (values %{ $self->combinations }) {
  0            
76 0 0         return $_->{$key} if exists $_->{$key};
77             }
78 0           return undef;
79             }
80              
81             sub value {
82 0     0 1   my $self = shift;
83 0           return $self->_combination_get('v', @_);
84             }
85              
86             sub data {
87 0     0 1   my $self = shift;
88 0           return $self->_combination_get('d', @_);
89             }
90              
91             sub error {
92 0     0 0   return $_[0]->{error};
93             }
94              
95             sub message {
96 0     0 0   return $_[0]->{message};
97             }
98              
99             sub as_string {
100 0     0 0   my ($self) = @_;
101 0           my $out = "Response id '" . $self->id . "': ";
102 0           $out = $out . $self->time . "ms, ";
103 0           my @names = $self->combiner_names;
104 0           $out = $out . scalar(@names) . " verdicts, ";
105 0           $out = $out . scalar(@{ $self->facts }) . " attributes\n";
  0            
106 0 0         if ($self->error) {
107 0           $out .= "Error: " . $self->message . "\n";
108             }
109             else {
110 0 0         if ($self->message) {
111 0           $out .= "Warning: " . $self->message . "\n";
112             }
113 0           foreach (sort @names) {
114 0           my $value = $self->value($_);
115 0           my $data = $self->data($_);
116 0 0         $value = 0 unless defined $value; # Might happen
117 0 0         $data = '(undef)' unless defined $data;
118 0           $out .= "Combiner '$_': verdict $value ($data)\n";
119             }
120 0           my @facts = sort { $a->{f} cmp $b->{f} } @{$self->facts};
  0            
  0            
121 0           foreach (@facts) {
122 0           my $d = $_->{d};
123 0 0         $d = "null data" unless defined $d;
124 0           $out .= "Attribute '$_->{f}':";
125 0 0         $out .= " identity '$_->{i}'" if exists $_->{i};
126 0           $out .= " value $_->{v} ($d)\n";
127             }
128             }
129 0           return $out;
130             }
131              
132             =head1 NAME
133              
134             Mail::Karmasphere::Response - Karmasphere Response Object
135              
136             =head1 SYNOPSIS
137              
138             See Mail::Karmasphere::Client
139              
140             =head1 DESCRIPTION
141              
142             The Perl Karma Client API consists of three objects: The Query, the
143             Response and the Client. The user constructs a Query and passes it to
144             a Client, which returns a Response. See L
145             for more information.
146              
147             =head1 METHODS
148              
149             =over 4
150              
151             =item $response->facts()
152              
153             Returns a list of fact data.
154              
155             =item $response->combination($name)
156              
157             Returns the named combination as a hash reference.
158              
159             =item $response->value($name)
160              
161             Returns the value of the named combination.
162              
163             If no combiner name is given, this method looks for a combination
164             called 'default', if present, otherwise searches for the first
165             available combination with a value.
166              
167             If $name is given, this is equivalent (but preferable) to
168             $response->combination($name)->{v}.
169              
170             =item $response->data($name)
171              
172             Returns the data of the named combination.
173              
174             The rules for choosing a combination are the same as those for
175             $response->value($name).
176              
177             =item $response->id()
178              
179             Returns the id of this response. It will match the id passed in the
180             query, which was either specified by the user or generated by the
181             client.
182              
183             =item $response->time()
184              
185             Returns the time in milliseconds taken by this request.
186              
187             =back
188              
189             =head1 BUGS
190              
191             This document is incomplete.
192              
193             =head1 SEE ALSO
194              
195             L
196             L
197             http://www.karmasphere.com/
198              
199             =head1 COPYRIGHT
200              
201             Copyright (c) 2005 Shevek, Karmasphere. All rights reserved.
202              
203             This program is free software; you can redistribute it and/or modify
204             it under the same terms as Perl itself.
205              
206             =cut
207              
208             1;