blib/lib/WWW/KeenIO.pm

filter: Statementstmt 45.3% 53 / 117 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 60.0% 12 / 20 Podpod 75.0% 3 / 4 totaltotal 32.6% 68 / 208 SCAR 54.3
CC
25
Cov
31%
_process_response
132.0
_transaction
42.0
select
42.0
CRAP 228.7
↑ Summary
1 package WWW::KeenIO;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 WWW::KeenIO - Perl API for Keen.IO L<< http://keen.io >> event storage and analytics
10  
11 =head1 VERSION
12  
13 Version 0.01
14  
15 =cut
16  
17 5 our $VERSION = '0.01';
18  
19 5 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #** @attr public api_key $api_key API access key
31 #*
32 5 has api_key => ( isa => 'Str', is => 'rw', required => 1 );
33  
34 #** @attr public CodeRef $read_key API key for writing data (if different from api_key
35 #*
36 5 has write_key => ( isa => 'Maybe[Str]', is => 'rw' );
37  
38 #** @attr public Int $project_id ID of the project
39 #*
40 5 has project => ( isa => 'Str', is => 'rw', required => 1 );
41  
42 #** @attr protected String $base_url Base REST URL
43 #*
44 5 has base_url => (
45 isa => 'Str',
46 is => 'rw',
47 default => 'https://api.keen.io/3.0/'
48 );
49  
50 5 Readonly my $REST_data => {
51 put => {
52 path => 'projects/$project/events/$collection',
53 write => 1
54 },
55 batch_put => {
56 path => 'projects/$project/events',
57 write => 1
58 },
59 select => {
60 path => 'projects/$project/queries/extraction'
61 }
62 };
63  
64 5 Readonly our $KEEN_OP_EQ => 'eq';
65 5 Readonly our $KEEN_OP_NE => 'ne';
66 5 Readonly our $KEEN_OP_EXISTS => 'exists';
67 5 Readonly our $KEEN_OP_IN => 'exists';
68 5 Readonly our $KEEN_OP_CONTAINS => 'contains';
69  
70 5 my @operators = qw($KEEN_OP_EQ $KEEN_OP_NE $KEEN_OP_EXISTS $KEEN_OP_IN
71 $KEEN_OP_CONTAINS);
72 5 our @EXPORT_OK = (@operators);
73 5 our %EXPORT_TAGS = ( 'operators' => [@operators] );
74  
75 #** @attr protected CodeRef $ua Reference to the REST UA
76 #*
77 has ua => (
78 isa => 'Object',
79 is => 'rw',
80 lazy => 1,
81 init_arg => undef,
82 default => sub {
83 0 ▶ return REST::Client->new();
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 }
85 5 );
86  
87 #** @attr public String $error_message Error message regarding the last failed operation
88 #*
89 5 has error_message =>
90 ( isa => 'Str', is => 'rw', init_arg => undef, default => '' );
91  
92 sub _url {
93 0 ▶ my ( $self, $path, $url_params, $query_params ) = @_;
Subroutine _url: not called CC=5 SCAR=34.0
94  
95 0 ▶ $url_params //= {};
Condition0%0/2
$url_params //= {}
l!l
00
MC/DC0%0/1
$url_params //= {}
$url_params
Truth table0%0/2
A
A= $url_params
Aresult
11
00
96 $url_params->{project} = $self->project
97 0 ▶ unless defined( $url_params->{project} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url_params->{"project"} 0 0 0
98 0 ▶ $query_params //= {};
Condition0%0/2
$query_params //= {}
l!l
00
MC/DC0%0/1
$query_params //= {}
$query_params
Truth table0%0/2
A
A= $query_params
Aresult
11
00
99 0 my $url = $self->base_url . $path;
100 0 ▶ $url =~ s^\$([\w\d\_]+)^$url_params->{$1} // ''^eg;
Condition0%0/2
$url_params->{$1} // ""
l!l
00
MC/DC0%0/1
$url_params->{$1} // ""
$url_params->{$1}
Truth table0%0/2
A
A= $url_params->{$1}
Aresult
11
00
101 0 my $uri = URI->new( $url, 'http' );
102 0 $uri->query_form($query_params);
103 #print "URL=".$uri->as_string;
104 0 return $uri->as_string;
105 }
106  
107 sub _process_response {
108 0 ▶ my ( $self, $response ) = @_;
Subroutine _process_response: not called CC=11 SCAR=48.8
109  
110 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not builtin::blessed($response)) { } 0 0 0
111 0 $self->error_message("Error $@");
112 0 return undef;
113 } elsif ( !blessed($response) ) {
114 0 $self->error_message(
115 "Unknown response $response from the REST client instead of object"
116 );
117 0 return undef;
118 }
119 print "Got response:"
120 . Dumper( $response->responseCode() ) . "/"
121 0 ▶ . Dumper( $response->responseContent() ) . "\n" if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
122 0 my $code = $response->responseCode();
123 0 my $parsed_content = eval { decode_json( $response->responseContent() ) };
124 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
125 0 cluck( "Cannot parse response content "
126 . $response->responseContent()
127 . ", error msg: $@. Is this JSON?" );
128 0 $parsed_content = {};
129 }
130 0 ▶ print "parsed ".Dumper($parsed_content) if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
131 0 ▶ if ( $code ne '200' && $code ne '201' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne 200 and $code ne 201) 0 0 0
Condition0%0/3
$code ne 200 and $code ne 201
!ll&&!rl&&r
000
MC/DC0%0/2
$code ne 200 and $code ne 201
$code ne 200 $code ne 201
Truth table0%0/3
A and B
A= $code ne 200B= $code ne 201
ABresult
0X0
100
111
132 0 my $err = "Received error code $code from the server instead of "
133 . 'expected 200/201';
134 0 ▶ if ( reftype($parsed_content) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($parsed_content) eq "HASH" and $parsed_content->{"message"}) 0 0 0
Condition0%0/3
builtin::reftype($parsed_content) eq "HASH" and $parsed_content->{"message"}
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($parsed_content) eq "HASH" and $parsed_content->{"message"}
builtin::reftype($parsed_content) eq "HASH" $parsed_content->{"message"}
Truth table0%0/3
A and B
A= builtin::reftype($parsed_content) eq "HASH"B= $parsed_content->{"message"}
ABresult
0X0
100
111
135 && $parsed_content->{message} )
136 {
137 $err .=
138 "\nError message from KeenIO: "
139 . $parsed_content->{message}
140 . ( $parsed_content->{error_code}
141 0 ▶ ? ' (' . $parsed_content->{error_code} . ')'
Branch: 1 branch
BranchTrueFalseTotal
$parsed_content->{'error_code'} ? : 0 0 0
142 : q{} );
143  
144 0 $self->error_message($err);
145 }
146 0 return undef;
147 }
148  
149 0 $self->error_message(q{});
150 0 return $parsed_content;
151 }
152  
153 sub _transaction {
154 0 ▶ my ( $self, $query_params, $data ) = @_;
Subroutine _transaction: not called CC=6 SCAR=37.4
155  
156 0 my $caller_sub = ( split( '::', ( caller(1) )[3] ) )[-1];
157 0 my $rest_data = $REST_data->{$caller_sub};
158  
159 0 ▶ $data //= {};
Condition0%0/2
$data //= {}
l!l
00
MC/DC0%0/1
$data //= {}
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
160 my $key =
161 $rest_data->{write}
162 0 ▶ ? ( $self->write_key // $self->api_key )
Branch: 1 branch
BranchTrueFalseTotal
$rest_data->{'write'} ? : 0 0 0
Condition0%0/3
$self->write_key // $self->api_key
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->write_key // $self->api_key
$self->write_key $self->api_key
Truth table0%0/3
A // B
A= $self->write_keyB= $self->api_key
ABresult
1X1
011
000
163 : $self->api_key;
164 0 my $method_path = $rest_data->{path};
165 0 ▶ confess("No URL path defined for method $caller_sub") unless $method_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $method_path 0 0 0
166  
167 0 my $url = $self->_url( $method_path, $query_params );
168 0 my $headers = {
169 'Content-Type' => 'application/json',
170 Authorization => $key
171 };
172 my $response =
173 0 eval { $self->ua->POST( $url, encode_json($data), $headers ); };
174 0 ▶ cluck($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
175 0 return $self->_process_response($response);
176 }
177  
178 =head1 SYNOPSIS
179  
180 use WWW::KeenIO qw(:operators);
181 use Text::CSV_XS;
182 use Data::Dumper;
183  
184 my $csv = Text::CSV_XS->new;
185 my $keen = WWW::KeenIO->new( {
186 project => '123',
187 api_key => '456',
188 write_key => '789'
189 }) or die 'Cannot create KeenIO object';
190  
191 # process a CSV file with 3 columns: name, in|out, date-time
192 # import them as keenIO events
193 while(<>) {
194 chomp;
195 my $status = $csv->parse($_);
196 unless ($status) {
197 warn qq{Cannot parse '$_':}.$csv->error_diag();
198 next;
199 }
200 my @fields = $csv->fields();
201 my $data = {
202 keen => {
203 timestamp => $fields[2]
204 },
205 name => $fields[0],
206 type => $fields[1]
207 };
208 my $res = $keen->put('in_out_log', $data);
209 unless ($res) {
210 warn "Unable to store the data in keenIO";
211 }
212 }
213
214 # now read the data
215 my $data = $keen->select('in_out_log', 'this_7_days',
216 [ $keen->filter('name', $KEEN_OP_EQ, 'John Doe') ] );
217 print Dumper($data);
218  
219 =head1 CONSTRUCTOR
220  
221 =head2 new( hashref )
222  
223 Creates a new object, acceptable parameters are:
224  
225 =over 16
226  
227 =item C<api_key> - (required) the key to be used for read operations
228  
229 =item C<project> - (required) the ID of KeenIO project
230  
231 =item C<write_key> - the key to be used for write operations (if different from api_key)
232  
233 =item C<base_url> - L<< https://api.keen.io/3.0/ >> by default; in case if you are using KeenIO-compatible API on some other server you can specify your own URL here
234  
235 =back
236  
237 =head1 METHODS
238  
239 =head2 put( $collection_name, $data )
240  
241 Inserts an event (C<$data> is a hashref) into the collection. Returns a
242 reference to a hash, which contains the response
243 received from the server (typically there is a key C<created> with
244 C<true> value). Returns C<undef> on failure, application then may call
245 C<error_message()> method to get the detailed info about the error.
246  
247 my $res = $keen->put('in_out_log', $data);
248 unless ($res) {
249 warn 'Something went wrong '.$keen->error_message();
250 }
251  
252 =cut
253  
254 sub put {
255 0 ▶ my ( $self, $collection, $record ) = @_;
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
256 0 return $self->_transaction(
257 {
258 collection => $collection
259 },
260 $record
261 );
262 }
263  
264 =head2 batch_put( $data )
265  
266 Inserts multiple events into Keen. C<$data> is a hashref, where every key
267 represents a collection name, where data should be inserted. Value of
268 the key is a reference to an array, which contains references to
269 individual event data (hashes).
270  
271 Returns C<undef> on total failure (e.g. unable to access the servers). Otherwise
272 returns a reference to a hash; each key represents a collection name and the
273 value is a reference to an array of statuses for individual events.
274  
275 my $res = $keen->batch_put( {
276 payments => [
277 {
278 name => 'John Doe',
279 customer_id => 123,
280 amount => 35.00
281 },
282 {
283 name => 'Peter Smith',
284 customer_id => 125,
285 amount => '10.00'
286 }
287 ],
288 purchases => [
289 {
290 name => 'John Doe',
291 customer_id => 123,
292 product_id => 567,
293 quantity => 1,
294 date => '2015-11-01 15:06:34'
295 }
296 ]
297 });
298 unless ($res) {
299 warn 'Something went wrong '.$keen->error_message();
300 }
301  
302 =cut
303  
304 sub batch_put {
305 0 ▶ my ( $self, $data ) = @_;
Subroutine batch_put: not called CC=1 SCAR=6.9
Pod documented
306 0 return $self->_transaction( { }, $data );
307 }
308  
309 =head2 get($collection_name, $interval [, $filters ] )
310  
311 Retrieves a list of events from the collection. C<$collection_name> is
312 self-explanatory. C<$interval> is a string, which describes the time period
313 we are interested in (see L<< https://keen.io/docs/api/#timeframe) >>).
314 C<$filters> is optional. If provided - should be an arrayref, each element
315 is an additional condition according to L<< https://keen.io/docs/api/#query-parameters >>.
316  
317 Returns a reference to an array on hashrefs; each element is a reference
318 to an actual events. Upon failure returns C<undef>.
319  
320 my $data = $keen->select('in_out_log', 'this_7_days',
321 [ $keen->filter('name', $KEEN_OP_EQ, 'John Doe') ]);
322 print Dumper($data);
323  
324 =cut
325  
326 sub select {
327 0 ▶ my ( $self, $collection, $timeframe, $filters ) = @_;
Subroutine select: not called CC=6 SCAR=37.4
Pod undocumented
328  
329 0 ▶ unless ( defined($collection) && defined($timeframe) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $collection and defined $timeframe) 0 0 0
Condition0%0/3
defined $collection and defined $timeframe
!ll&&!rl&&r
000
MC/DC0%0/2
defined $collection and defined $timeframe
defined $collection defined $timeframe
Truth table0%0/3
A and B
A= defined $collectionB= defined $timeframe
ABresult
0X0
100
111
330 0 $self->error_message("Must provide collection name and timeframe");
331 0 return undef;
332 }
333  
334 0 my $params = {};
335 0 ▶ $params->{filters} = $filters if $filters;
Branch: 1 branch
BranchTrueFalseTotal
if $filters 0 0 0
336 0 $params->{event_collection} = $collection;
337 0 $params->{timeframe} = $timeframe;
338 0 my $x = $self->_transaction( {}, $params );
339 0 ▶ unless ( reftype($x) eq 'HASH' && reftype( $x->{result} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($x) eq "HASH" and builtin::reftype($x->{"result"}) eq "ARRAY") 0 0 0
Condition0%0/3
builtin::reftype($x) eq "HASH" and builtin::reftype($x->{"result"}) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($x) eq "HASH" and builtin::reftype($x->{"result"}) eq "ARRAY"
builtin::reftype($x) eq "HASH" builtin::reftype($x->{"result"}) eq "ARRAY"
Truth table0%0/3
A and B
A= builtin::reftype($x) eq "HASH"B= builtin::reftype($x->{"result"}) eq "ARRAY"
ABresult
0X0
100
111
340 0 return undef;
341 }
342 0 return $x->{result};
343 }
344  
345 =head2 filter($field, $operator, $value)
346  
347 Creates a filter for retrieving events via select() method.
348  
349 use WWW::KeenIO qw(:operators);
350 my $res = $keen->select('tests', 'this_10_years', [
351 $keen->filter('Author', $KEEN_OP_CONTAINS, 'Andrew'),
352 $keen->filter('Status', $KEEN_OP_EQ, 'resolved')
353 ] );
354  
355 Please refer to Keen API documentation regarding all available operators and
356 their usage. For convenience constants for most frequently used operators are exported via :operators tag:
357 $KEEN_OP_EQ, $KEEN_OP_NE, $KEEN_OP_EXISTS, $KEEN_OP_IN, $KEEN_OP_CONTAINS
358  
359 =cut
360  
361 sub filter {
362 0 ▶ my ( $self, $field, $operator, $value ) = @_;
Subroutine filter: not called CC=1 SCAR=6.9
Pod documented
363 return {
364 0 property_name => $field,
365 operator => $operator,
366 property_value => $value
367 };
368 }
369  
370 =head2 error_message()
371  
372 Returns the detailed explanation of the last error. Empty string if
373 everything went fine.
374  
375 my $res = $keen->put('in_out_log', $data);
376 unless ($res) {
377 warn 'Something went wrong '.$keen->error_message();
378 }
379  
380 =cut
381  
382 =head1 AUTHOR
383  
384 Andrew Zhilenko, C<< <perl at putinhuylo.org> >>
385 (c) Putin Huylo LLC, 2015
386  
387 =head1 BUGS
388  
389 Please report any bugs or feature requests to C<bug-www-keenio at rt.cpan.org>, or through
390 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-KeenIO>.
391 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
392  
393 =head1 SUPPORT
394  
395 You can find documentation for this module with the perldoc command.
396  
397 perldoc WWW::KeenIO
398  
399  
400 You can also look for information at:
401  
402 =over 4
403  
404 =item * RT: CPAN's request tracker (report bugs here)
405  
406 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-KeenIO>
407  
408 =item * AnnoCPAN: Annotated CPAN documentation
409  
410 L<http://annocpan.org/dist/WWW-KeenIO>
411  
412 =item * CPAN Ratings
413  
414 L<http://cpanratings.perl.org/d/WWW-KeenIO>
415  
416 =item * Search CPAN
417  
418 L<http://search.cpan.org/dist/WWW-KeenIO/>
419  
420 =back
421  
422 =head1 LICENSE AND COPYRIGHT
423  
424 Copyright 2015 Putin Huylo LLC
425  
426 This program is free software; you can redistribute it and/or modify it
427 under the terms of the the Artistic License (2.0). You may obtain a
428 copy of the full license at:
429  
430 L<http://www.perlfoundation.org/artistic_license_2_0>
431  
432 Any use, modification, and distribution of the Standard or Modified
433 Versions is governed by this Artistic License. By using, modifying or
434 distributing the Package, you accept this license. Do not use, modify,
435 or distribute the Package, if you do not accept this license.
436  
437 If your Modified Version has been derived from a Modified Version made
438 by someone other than you, you are nevertheless required to ensure that
439 your Modified Version complies with the requirements of this license.
440  
441 This license does not grant you the right to use any trademark, service
442 mark, tradename, or logo of the Copyright Holder.
443  
444 This license includes the non-exclusive, worldwide, free-of-charge
445 patent license to make, have made, use, offer to sell, sell, import and
446 otherwise transfer the Package with respect to any patent claims
447 licensable by the Copyright Holder that are necessarily infringed by the
448 Package. If you institute patent litigation (including a cross-claim or
449 counterclaim) against any party alleging that the Package constitutes
450 direct or contributory patent infringement, then this Artistic License
451 to you shall terminate on the date that such litigation is filed.
452  
453 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
454 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
455 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
456 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
457 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
458 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
459 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
460 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
461  
462  
463 =cut
464  
465 5 __PACKAGE__->meta->make_immutable;
466  
467 5 1; # End of WWW::KeenIO
↑ Summary