blib/lib/IMAP/Query.pm

filter: Statementstmt 30.3% 24 / 79 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 58.3% 7 / 12 Podpod 100% 1 / 1 totaltotal 21.7% 32 / 147 CC 25 SCAR 45.8
CC
25
Cov
19%
_build_search_string_recurse
154.4
_build_search_string_ARRAYREF
132.0
_maybe_add_space
12.0
CRAP 97.4
↑ Summary
1 package IMAP::Query;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 IMAP::Query - Build IMAP search queries!
13  
14 =head1 VERSION
15  
16 Version 0.02
17  
18 =cut
19  
20 2 our $VERSION = '0.02';
21  
22 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 Readonly our @EXPORT_OK => qw(build_search_string);
24  
25 2 Readonly my @KEYWORDS => qw(
26 ALL
27 ANSWERED
28 BCC
29 BEFORE
30 BODY
31 CC
32 DELETED
33 DRAFT
34 FLAGGED
35 FROM
36 HEADER
37 KEYWORD
38 LARGER
39 NEW
40 NOT
41 OLD
42 ON
43 OR
44 RECENT
45 SEEN
46 SENTBEFORE
47 SENTON
48 SENTSINCE
49 SINCE
50 SMALLER
51 SUBJECT
52 TEXT
53 TO
54 UID
55 UNANSWERED
56 UNDELETED
57 UNDRAFT
58 UNFLAGGED
59 UNKEYWORD
60 UNSEEN
61 );
62  
63 =head1 SYNOPSIS
64  
65 This module is for those of us that can't create Polish notation queries by
66 hand, L<IMAP::Query> can help you create them using a syntax inspired by other
67 query builder modules such as L<SQL::Abstract>.
68  
69 use IMAP::Query qw(build_search_string);
70  
71 my $query = build_search_string(
72 BEFORE => strftime('%d-%b-%Y', localtime(parsedate('yesterday'))),
73 NOT => 'DELETED',
74 -or => [
75 FROM => 'test@example.com',
76 -and => [
77 FROM => 'other@example.com',
78 SUBJECT => 'TESTING',
79 ],
80 ],
81 );
82  
83 ... # Do something interesting with our $query
84  
85 =head1 EXPORT
86  
87 A list of functions that can be exported. You can delete this section if you
88 don't export anything, such as for a purely object-oriented module.
89  
90 =head1 SUBROUTINES/METHODS
91  
92 =head2 build_search_string()
93  
94 This method can be exported via your use statement.
95  
96 use IMAP::Query qw(build_search_string);
97  
98 It takes two arguments.
99  
100 my $query = build_search_string($query, $operator);
101  
102 =over
103  
104 =item $query
105  
106 This option is requried. It is a HASHREF that contains all the items you want
107 to include in your search string.
108  
109 =item $operator
110  
111 This argument is optional and defaults to 'AND'. You can use either 'AND' or
112 'OR'.
113  
114 =back
115  
116 =cut
117  
118 sub build_search_string
119 0 ▶{
Subroutine build_search_string: not called CC=1 SCAR=6.9
Pod documented
120 0 my @options = @_;
121  
122 0 return _build_search_string_recurse(\@options);
123 }
124  
125 sub _build_search_string_recurse
126 0 ▶{
Subroutine _build_search_string_recurse: not called CC=13 SCAR=50.4
127 0 my $opts = shift;
128 0 ▶ my $op = shift || 'AND';
Condition0%0/2
shift() || 'AND'
l!l
00
MC/DC0%0/1
shift() || 'AND'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
129  
130 0 my @options = @$opts;
131  
132 0 my $rv = '';
133  
134 0 ▶ while (@options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options) 0 0 0
135 0 my $item = shift @options;
136  
137 0 ▶ if (ref $item eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 0 0 0
elsif (ref $item eq 'HASH') { } 0 0 0
elsif (defined $item and $item =~ /^-/) { } 0 0 0
elsif (defined $item and length $item) { } 0 0 0
138 0 my $value = _build_search_string_ARRAYREF($item, $op);
139 0 ▶ if (length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value) 0 0 0
140 0 _maybe_add_space($rv);
141 0 $rv .= $value;
142 }
143 }
144 elsif (ref $item eq 'HASH') {
145 0 _maybe_add_space($rv);
146 0 $rv .= _build_search_string_recurse([ %$item ], $op);
147 }
148 ▶ elsif (defined $item && $item =~ /^-/) {
Condition0%0/3
defined $item and $item =~ /^-/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $item and $item =~ /^-/
defined $item $item =~ /^-/
Truth table0%0/3
A and B
A= defined $itemB= $item =~ /^-/
ABresult
0X0
100
111
149 0 ▶ if ($item =~ /^-and$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^-and$/) { } 0 0 0
elsif ($item =~ /^-or$/) { } 0 0 0
150 0 $op = 'AND';
151 }
152 elsif ($item =~ /^-or$/) {
153 0 $op = 'OR';
154 }
155 else {
156 0 die "Unknown operator $item.\n";
157 }
158 }
159 ▶ elsif (defined $item && length $item) {
Condition0%0/3
defined $item and length $item
!ll&&!rl&&r
000
MC/DC0%0/2
defined $item and length $item
defined $item length $item
Truth table0%0/3
A and B
A= defined $itemB= length $item
ABresult
0X0
100
111
160 0 _maybe_add_space($rv);
161 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 0 ▶ if (any{ uc($item) eq $_ } @KEYWORDS) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { uc $item eq $_; } , @KEYWORDS)) { } 0 0 0
163 0 $rv .= uc($item);
164 }
165 else {
166 0 $rv .= qq/"$item"/;
167 }
168 }
169 }
170  
171 0 return ($op, $rv);
172 }
173  
174 sub _build_search_string_ARRAYREF
175 0 ▶{
Subroutine _build_search_string_ARRAYREF: not called CC=11 SCAR=48.8
176 0 my $array = shift;
177 0 ▶ my $op = shift || 'AND';
Condition0%0/2
shift() || 'AND'
l!l
00
MC/DC0%0/1
shift() || 'AND'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
178 0 my $local_op = $op;
179  
180 0 my $rv = '';
181 0 ▶ return $rv unless @$array;
Branch: 1 branch
BranchTrueFalseTotal
unless @$array 0 0 0
182  
183 0 my @items;
184 0 foreach my $item (@$array) {
185 0 my $value = '';
186 0 ($local_op, $value) = _build_search_string_recurse([$item], $local_op);
187 0 ▶ if (length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value) 0 0 0
188 0 push(@items, $value);
189 }
190 }
191 0 ▶ return $rv unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 0 0 0
192  
193 0 ▶ if ($op eq 'AND') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "AND") 0 0 0
194 0 _maybe_add_space($rv);
195 0 $rv .= '(';
196 0 foreach my $item (@items) {
197 0 _maybe_add_space($rv);
198 0 $rv .= $item;
199 }
200 0 $rv .= ')';
201 0 return $rv;
202 }
203  
204 0 ▶ if (@items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items > 1) 0 0 0
205 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@items >= 2 and do { do { my $or = '(OR ' . shift(@items) . ' ' . shift(@items) . ')'; push @items, $or } } 0 0 0
206 0 my $or = '(OR '.shift(@items).' '.shift(@items).')';
207 0 push(@items, $or);
208 } while (@items >= 2);
209 }
210 0 ▶ if (@items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 0 0 0
211 0 _maybe_add_space($rv);
212 0 $rv .= $items[0];
213 }
214  
215 0 return $rv;
216 }
217  
218 sub _maybe_add_space
219 0 ▶{
Subroutine _maybe_add_space: not called CC=3 SCAR=24.8
220 0 ▶ if (length $_[0] && substr($_[0], -1, 1) !~ /^[\(\s]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0] and not substr($_[0], -1, 1) =~ /^[\(\s]$/) 0 0 0
Condition0%0/3
length $_[0] and not substr($_[0], -1, 1) =~ /^[\(\s]$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $_[0] and not substr($_[0], -1, 1) =~ /^[\(\s]$/
length $_[0] not substr($_[0], -1, 1) =~ /^[\(\s]$/
Truth table0%0/3
A and not(B)
A= length $_[0]B= not substr($_[0], -1, 1) =~ /^[\(\s]$/
ABresult
0X0
100
111
221 0 $_[0] .= ' ';
222 }
223 }
224  
225 =head1 AUTHOR
226  
227 Adam R. Schobelock, C<< <schobes at gmail.com> >>
228  
229 =head1 BUGS
230  
231 Please report any bugs or feature requests to C<bug-imap-query at rt.cpan.org>, or through
232 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IMAP-Query>. I will be notified, and then you'll
233 automatically be notified of progress on your bug as I make changes.
234  
235 =head1 SUPPORT
236  
237 You can find documentation for this module with the perldoc command.
238  
239 perldoc IMAP::Query
240  
241 You can also look for information at:
242  
243 =over 4
244  
245 =item * RT: CPAN's request tracker (report bugs here)
246  
247 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IMAP-Query>
248  
249 =item * AnnoCPAN: Annotated CPAN documentation
250  
251 L<http://annocpan.org/dist/IMAP-Query>
252  
253 =item * CPAN Ratings
254  
255 L<http://cpanratings.perl.org/d/IMAP-Query>
256  
257 =item * Search CPAN
258  
259 L<http://search.cpan.org/dist/IMAP-Query/>
260  
261 =item * Code Repository
262  
263 L<https://github.com/schobes/IMAP-Query>
264  
265 =back
266  
267 =head1 ACKNOWLEDGEMENTS
268  
269 =head1 LICENSE AND COPYRIGHT
270  
271 Copyright 2015 Adam R. Schobelock.
272  
273 This program is free software; you can redistribute it and/or modify it
274 under the terms of the the Artistic License (2.0). You may obtain a
275 copy of the full license at:
276  
277 L<http://www.perlfoundation.org/artistic_license_2_0>
278  
279 Any use, modification, and distribution of the Standard or Modified
280 Versions is governed by this Artistic License. By using, modifying or
281 distributing the Package, you accept this license. Do not use, modify,
282 or distribute the Package, if you do not accept this license.
283  
284 If your Modified Version has been derived from a Modified Version made
285 by someone other than you, you are nevertheless required to ensure that
286 your Modified Version complies with the requirements of this license.
287  
288 This license does not grant you the right to use any trademark, service
289 mark, tradename, or logo of the Copyright Holder.
290  
291 This license includes the non-exclusive, worldwide, free-of-charge
292 patent license to make, have made, use, offer to sell, sell, import and
293 otherwise transfer the Package with respect to any patent claims
294 licensable by the Copyright Holder that are necessarily infringed by the
295 Package. If you institute patent litigation (including a cross-claim or
296 counterclaim) against any party alleging that the Package constitutes
297 direct or contributory patent infringement, then this Artistic License
298 to you shall terminate on the date that such litigation is filed.
299  
300 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
301 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
302 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
303 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
304 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
305 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
306 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
307 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
308  
309 =cut
310  
311 2 1; # End of IMAP::Query
↑ Summary