File Coverage

blib/lib/WebService/Blekko/QueryResultSet.pm
Criterion Covered Total %
statement 45 52 86.5
branch 9 14 64.2
condition 1 3 33.3
subroutine 11 13 84.6
pod 7 9 77.7
total 73 91 80.2


line stmt bran cond sub pod time code
1             #
2              
3             package WebService::Blekko::QueryResultSet;
4              
5 3     3   18 use strict;
  3         7  
  3         118  
6 3     3   16 use warnings;
  3         6  
  3         118  
7 3     3   19 no warnings qw( uninitialized );
  3         6  
  3         193  
8              
9             =head1 NAME
10              
11             WebService::Blekko::QueryResultSet -- query result from WebService::Blekko
12              
13             =cut
14              
15             our $VERSION = '1.00';
16              
17 3     3   1861 use WebService::Blekko::QueryResult;
  3         8  
  3         1931  
18              
19             sub new
20             {
21 8     8 0 409 my $class = shift;
22 8         32 my $self = bless {}, $class;
23              
24 8         23 my ( $json, $http_code ) = @_;
25              
26 8         40 $self->{http_code} = $http_code;
27              
28 8 100       67 if ( $http_code !~ /^2/ )
29             {
30 3         16 $self->{error} = "Remote webserver returned $http_code";
31 3         10 $self->{total_num} = 0;
32 3         82 return $self;
33             }
34              
35 5         30 my $answer = WebService::Blekko::my_decode_json( $json );
36              
37 5         20 $self->{raw} = $answer;
38              
39 5 100       23 if ( defined $answer->{ERROR} )
40             {
41             # {suggesttag} doesn't ever seem to be useful
42 1         6 $self->{error} = $answer->{ERROR}->{errstring};
43             }
44              
45 5         13 foreach my $f ( qw( total_num RESULT sug_slash query_rewritten ) )
46             {
47 20         57 $self->{$f} = $answer->{$f};
48             }
49              
50 5 100       24 if ( $self->{total_num} )
51             {
52 4 50 33     23 if ( ! ref $self->{RESULT} eq 'ARRAY' ||
  4         88  
53             scalar @{$self->{RESULT}} != $self->{total_num} )
54             {
55 0         0 $self->{error} = "Internal error: total_num did not equal actual result count";
56 0         0 $self->{total_num} = 0;
57 0         0 return $self;
58             }
59             }
60              
61 5         18 $self->{next} = 0;
62              
63 5         113 return $self;
64             }
65              
66             =head1 METHODS
67              
68             =head2 next
69              
70             Retrieves the next WebService::Blekko::QueryResult in the list.
71              
72             =head2 error
73              
74             Set to a non-zero string if there is an error.
75              
76             =head2 http_code
77              
78             The HTTP response code. If it is not 2XX, there was a problem.
79              
80             =head2 total_num
81              
82             The total number of possible results.
83              
84             =head2 sug_slash
85              
86             A list of suggested slashtags. For example, a search for Linus
87             Torvalds will return a list of suggestions such as /linux and
88             /tech. It is a good idea to show these suggestions to your users.
89              
90             =head2 auto_slashtag
91              
92             Under certain circumstances, blekko will add a slashtag to your query
93             term. For example, q=cure+for+headaches will auto-fire the /health
94             slashtag, and this will cause auto_slashtag to be set to the string '/health'.
95              
96             If you wish to avoid auto-slashtag firing, add /web to the query,
97             i.e. q=cure+for+headaches+/web
98              
99             =head2 auto_slashtag_query
100              
101             When an auto slashtag is fired, sometimes we also change the query by
102             adding or dropping words. If so, auto_slashtag_query will be set to
103             a string with the final query terms.
104              
105             =cut
106              
107             sub next
108             {
109 25     25 1 1319 my ( $self ) = @_;
110              
111 25 100       98 return if $self->{next} >= $self->{total_num};
112              
113 20         38 return WebService::Blekko::QueryResult->new( @{$self->{RESULT}}[$self->{next}++] );
  20         103  
114             }
115              
116             # accessors
117              
118             sub error
119             {
120 7     7 1 162 my ( $self ) = @_;
121              
122 7         65 return $self->{error};
123             }
124              
125             sub http_code
126             {
127 10     10 1 21 my ( $self ) = @_;
128              
129 10         59 return $self->{http_code};
130             }
131              
132             sub total_num
133             {
134 4     4 1 30 my ( $self ) = @_;
135              
136 4         23 return $self->{total_num};
137             }
138              
139             sub sug_slash
140             {
141 3     3 1 49 my ( $self ) = @_;
142              
143 3         684 return $self->{sug_slash};
144             }
145              
146             sub auto_slashtag
147             {
148 0     0 1 0 my ( $self ) = @_;
149              
150 0 0       0 return defined $self->{query_rewritten} ? $self->{query_rewritten}->{slashtag} : undef;
151             }
152              
153             sub auto_slashtag_query
154             {
155 0     0 1 0 my ( $self ) = @_;
156              
157 0 0       0 return defined $self->{query_rewritten} ? $self->{query_rewritten}->{new_q_str} : undef;
158             }
159              
160             sub raw
161             {
162 14     14 0 129 my ( $self ) = @_;
163              
164 14         58 return $self->{raw};
165             }
166              
167             1;
168