File Coverage

blib/lib/WWW/StatsMix.pm
Criterion Covered Total %
statement 87 168 51.7
branch 36 88 40.9
condition 15 84 17.8
subroutine 20 25 80.0
pod 10 10 100.0
total 168 375 44.8


line stmt bran cond sub pod time code
1             package WWW::StatsMix;
2              
3             $WWW::StatsMix::VERSION = '0.05';
4              
5             =head1 NAME
6              
7             WWW::StatsMix - Interface to StatsMix API.
8              
9             =head1 VERSION
10              
11             Version 0.05
12              
13             =cut
14              
15 12     12   30362 use 5.006;
  12         32  
  12         460  
16 12     12   8472 use JSON;
  12         154036  
  12         55  
17 12     12   10370 use Data::Dumper;
  12         74784  
  12         1000  
18              
19 12     12   5236 use WWW::StatsMix::Stat;
  12         37  
  12         478  
20 12     12   6243 use WWW::StatsMix::Metric;
  12         32  
  12         434  
21 12     12   5590 use WWW::StatsMix::UserAgent;
  12         35  
  12         468  
22 12     12   5984 use WWW::StatsMix::Params qw(validate);
  12         29  
  12         812  
23              
24 12     12   71 use Moo;
  12         18  
  12         72  
25 12     12   3474 use namespace::clean;
  12         22  
  12         115  
26             extends 'WWW::StatsMix::UserAgent';
27              
28             has format => (is => 'ro', default => sub { return 'json' });
29             has metrics_url => (is => 'ro', default => sub { return 'http://api.statsmix.com/api/v2/metrics' });
30             has stats_url => (is => 'ro', default => sub { return 'http://api.statsmix.com/api/v2/stats' });
31             has track_url => (is => 'ro', default => sub { return 'http://api.statsmix.com/api/v2/track' });
32              
33             =head1 DESCRIPTION
34              
35             StatsMix provides an API that can be used to create, retrieve, update, and delete
36             metrics and stats resources. The API is built using RESTful principles.
37              
38             The current version is 2.0.
39              
40             To get an API key, you can sign up for a free Developer account L.
41              
42             If you go over the number of API requests available to your account, the API will
43             return a 403 Forbidden error and an explanation. The number of API requests and
44             profiles you can create is based on the type of account you have. For example,
45             Standard plans are limited to 300,000 API requests per month.
46              
47             =head1 SYNOPSIS
48              
49             Use Strict; use warnings;
50             use WWW::StatsMix;
51              
52             my $API_KEY = "Your API Key";
53             my $api = WWW::StatsMix->new(api_key => $API_KEY);
54              
55             my $metric_1 = $api->create_metric({ name => "Testing - 1" });
56             my $metric_2 = $api->create_metric({ name => "Testing - 2", include_in_email => 0 });
57             $api->update_metric($metric_2->id, { name => "Testing - 3", include_in_email => 1 });
58              
59             my $metrics = $api->get_metrics;
60             my $only_2 = $api->get_metrics({ limit => 2 });
61              
62             =head1 METHODS
63              
64             =head2 create_metric(\%params)
65              
66             It creates new metric and returns the object of type L.The
67             possible parameters for the method are as below:
68              
69             +------------------+-----------------------------------------------------------------------+
70             | Key | Description |
71             +------------------+-----------------------------------------------------------------------+
72             | name | The name of the metric. Metric names must be unique within a profile. |
73             | (required) | |
74             | | |
75             | profile_id | The profile the metric belongs in. |
76             | (optional) | |
77             | | |
78             | sharing | Sharing status for the metric. Either "public" (unauthenticated users |
79             | (optional) | can view the metric at the specific URL) or "none" (default). |
80             | | |
81             | include_in_email | This specifies whether to include the metric in the daily |
82             | (optional) | StatsMix email sent to users. |
83             | | |
84             | url | Publicly accessible URL for the metric (only if sharing is set |
85             | (optional) | to "public"). |
86             +------------------+-----------------------------------------------------------------------+
87              
88             use strict; use warnings;
89             use WWW::StatsMix;
90              
91             my $API_KEY = "Your_API_Key";
92             my $api = WWW::StatsMix->new(api_key => $API_KEY);
93             my $metric = $api->create_metric({ name => "Testing API - 2" });
94              
95             print "Id: ", $metric->id, "\n";
96              
97             =cut
98              
99             sub create_metric {
100 11     11 1 6716 my ($self, $params) = @_;
101              
102 11         73 my $data = [
103             { key => 'name' , required => 1 },
104             { key => 'profile_id' , required => 0 },
105             { key => 'url' , required => 0 },
106             { key => 'sharing' , required => 0 },
107             { key => 'include_in_email' , required => 0 },
108             { key => 'format' , required => 0 },
109             ];
110 11         28 validate($data, $params);
111              
112 2 50 66     16 if (exists $params->{sharing}
      66        
113             && defined $params->{sharing}
114             && ($params->{sharing} =~ /\bpublic\b/i)) {
115 1 50       10 die "ERROR: Missing key 'url' since 'sharing' is provided."
116             unless (exists $params->{url});
117             }
118 1 50 33     6 if (exists $params->{url} && defined $params->{url}) {
119 1 50       10 die "ERROR: Missing key 'sharing' since 'url' is provided."
120             unless (exists $params->{sharing});
121             }
122              
123 0 0 0     0 $params->{format} = $self->format
124             unless (exists $params->{format} && defined $params->{format});
125              
126 0         0 my $response = $self->post($self->metrics_url, [ %$params ]);
127 0         0 my $content = from_json($response->content);
128              
129 0         0 return WWW::StatsMix::Metric->new($content->{metric});
130             }
131              
132             =head2 update_metric($metric_id, \%params)
133              
134             It updates the metric & returns the object of type L. This
135             requires mandatory param 'metric_id' and atleast one of the following key as ref
136             to hash format data. Possible parameters are as below:
137              
138             +------------------+-----------------------------------------------------------------------+
139             | Key | Description |
140             +------------------+-----------------------------------------------------------------------+
141             | name | The name of the metric. Metric names must be unique within a profile. |
142             | | |
143             | sharing | Sharing status for the metric. Either "public" (unauthenticated users |
144             | | can view the metric at the specific URL) or "none" (default). |
145             | | |
146             | include_in_email | This specifies whether to include the metric in the daily |
147             | | StatsMix email sent to users. |
148             | | |
149             | url | Publicly accessible URL for the metric (only if sharing is set |
150             | | to "public"). |
151             +------------------+-----------------------------------------------------------------------+
152              
153             use strict; use warnings;
154             use WWW::StatsMix;
155              
156             my $API_KEY = "Your_API_Key";
157             my $api = WWW::StatsMix->new(api_key => $API_KEY);
158             my $metric_id = ;
159             my $metric = $api->update_metric($metric_id, { name => "Testing API - new" });
160              
161             print "Name: ", $metric->name, "\n";
162              
163             =cut
164              
165             sub update_metric {
166 8     8 1 5738 my ($self, $id, $params) = @_;
167              
168 8 100       28 die "ERROR: Missing the required metric id." unless defined $id;
169 7 100       39 die "ERROR: Invalid metric id [$id]." unless ($id =~ /^\d+$/);
170              
171 6 100 66     33 if (defined $params && ref($params) eq 'HASH') {
172 5         28 my $data = [
173             { key => 'name' , required => 0 },
174             { key => 'url' , required => 0 },
175             { key => 'sharing' , required => 0 },
176             { key => 'include_in_email' , required => 0 },
177             ];
178              
179 5         15 validate($data, $params);
180              
181 0 0       0 die "ERROR: Missing keys to update." unless (scalar(keys %$params));
182              
183 0 0 0     0 if (exists $params->{sharing}
      0        
184             && defined $params->{sharing}
185             && ($params->{sharing} =~ /\bpublic\b/i)) {
186 0 0 0     0 die "ERROR: Invalid data for key 'url'."
187             unless (exists $params->{url} && defined $params->{url});
188             }
189 0 0 0     0 if (exists $params->{url} && defined $params->{url}) {
190 0 0 0     0 die "ERROR: Invalid data for key 'sharing'."
      0        
191             unless (exists $params->{sharing}
192             && defined $params->{sharing}
193             && ($params->{sharing} =~ /\bpublic\b/i));
194             }
195             }
196             else {
197 1         7 die "ERROR: Parameters have to be hash ref.";
198             }
199              
200 0         0 my $url = sprintf("%s/%d.json", $self->metrics_url, $id);
201 0         0 my $response = $self->put($url, [ %$params ]);
202 0         0 my $content = from_json($response->content);
203              
204 0         0 return WWW::StatsMix::Metric->new($content->{metric});
205             }
206              
207             =head2 delete_metric($metric_id)
208              
209             It deletes the metric and returns the object of type L. It
210             requires mandatory 'metric_id'.
211              
212             use strict; use warnings;
213             use WWW::StatsMix;
214              
215             my $API_KEY = "Your_API_Key";
216             my $api = WWW::StatsMix->new(api_key => $API_KEY);
217             my $metric_id = ;
218             my $metric = $api->delete_metric($metric_id);
219              
220             print "Name: ", $metric->name, "\n";
221              
222             =cut
223              
224             sub delete_metric {
225 2     2 1 3190 my ($self, $id) = @_;
226              
227 2 100       18 die "ERROR: Missing the required key metric id." unless defined $id;
228 1 50       12 die "ERROR: Invalid the required key metric id [$id]." unless ($id =~ /^\d+$/);
229              
230 0         0 my $url = sprintf("%s/%d.json", $self->metrics_url, $id);
231 0         0 my $response = $self->delete($url);
232 0         0 my $content = from_json($response->content);
233              
234 0         0 return WWW::StatsMix::Metric->new($content->{metric});
235             }
236              
237             =head2 get_metrics(\%params)
238              
239             The method get_metrics() will return a default of up to 50 records. The parameter
240             limit can be passed to specify the number of records to return. The parameter
241             profile_id can also be used to scope records to a particular profile. Parameters
242             start_date & end_date can be used to limit the date range based on the timestamp
243             in a stat's generated_at.
244             The result of the call is reference to list of L objects.
245              
246             +------------+---------------------------------------------------------------+
247             | Key | Description |
248             +------------+---------------------------------------------------------------+
249             | limit | Limit the number of metrics. Default is 50. |
250             | (optional) | |
251             | | |
252             | profile_id | Scope the search to a particular profile. |
253             | (optional) | |
254             | | |
255             | start_date | Limit the searh in date range against stats generated_at key. |
256             | / end_date | Valid format is YYYY-MM-DD. |
257             | (optional) | |
258             +------------+---------------------------------------------------------------+
259              
260             use strict; use warnings;
261             use WWW::StatsMix;
262              
263             my $API_KEY = "Your_API_Key";
264             my $api = WWW::StatsMix->new(api_key => $API_KEY);
265             my $profile_id = ;
266             my $limit = ;
267              
268             my $metrics_all = $api->get_metrics;
269             my $metrics_by_limit = $api->get_metrics({ limit => $limit });
270             my $metrics_by_profile = $api->get_metrics({ profile_id => $profile_id });
271              
272             =cut
273              
274             sub get_metrics {
275 7     7 1 5356 my ($self, $params) = @_;
276              
277 7         39 my $url = sprintf("%s?format=%s", $self->metrics_url, $self->format);
278 7 50       18 if (defined $params) {
279 7         35 my $data = [
280             { key => 'limit' , required => 0 },
281             { key => 'profile_id', required => 0 },
282             { key => 'start_date', required => 0 },
283             { key => 'end_date' , required => 0 },
284             ];
285 7         18 validate($data, $params);
286              
287 1 50 33     11 if (exists $params->{start_date} && defined $params->{start_date}) {
    0 0        
288 1 50 33     17 die "ERROR: Missing param key 'end_date'."
289             unless (exists $params->{end_date} && defined $params->{end_date});
290 0 0       0 die "ERROR: Invalid param key 'start_date'."
291             unless _is_valid_date($params->{start_date});
292 0 0       0 die "ERROR: Invalid param key 'end_date'."
293             unless _is_valid_date($params->{end_date});
294             }
295             elsif (exists $params->{end_date} && defined $params->{end_date}) {
296 0 0 0     0 die "ERROR: Missing param key 'start_date'."
297             unless (exists $params->{start_date} && defined $params->{start_date});
298             }
299              
300 0         0 foreach (qw(limit profile_id start_date end_date)) {
301 0 0 0     0 if (exists $params->{$_} && defined $params->{$_}) {
302 0         0 $url .= sprintf("&%s=%s", $_, $params->{$_});
303             }
304             }
305             }
306              
307 0         0 my $response = $self->get($url);
308 0         0 my $content = from_json($response->content);
309              
310 0         0 return _get_metrics($content);
311             }
312              
313             =head2 create_stat(\%params)
314              
315             The method create_stat() creates stat for the given metric. You can also create
316             stat with ref_id. It returns an object of type L.
317              
318             +--------------+----------------------------------------------------------------------------+
319             | Key | Description |
320             +--------------+----------------------------------------------------------------------------+
321             | metric_id | The metric id for which the stat would be created. |
322             | (required) | |
323             | | |
324             | value | The numeric value of the stat with a decimal precision of two. Decimal (up |
325             | (required) | to 11 digits on the left side of the decimal point, two on the right). |
326             | | |
327             | generated_at | Datetime for the stat. If not set, defaults to the current timestamp. This |
328             | (optional) | is the datetime to be used in the charts. Valid format is YYYY-MM-DD. |
329             | | |
330             | meta | hash ref data (key,value pair) about anything associated with the stat. |
331             | (optional) | |
332             | | |
333             | ref_id | Optional reference id for a stat. If a stat already exists for the named |
334             | (optional) | metric and the given ref_id, the value (and optionally generated_at and |
335             | | meta) will be updated instead of created. |
336             | | |
337             +--------------+----------------------------------------------------------------------------+
338              
339             use strict; use warnings;
340             use WWW::StatsMix;
341              
342             my $API_KEY = "Your_API_Key";
343             my $api = WWW::StatsMix->new(api_key => $API_KEY);
344             my $metric_id = ;
345             my $value = ;
346             my $params = { metric_id => $metric_id, value => $value };
347             my $stat = $api->create_stat($params);
348              
349             print "Id: ", $stat->id, "\n";
350              
351             =cut
352              
353             sub create_stat {
354 12     12 1 6310 my ($self, $params) = @_;
355              
356 12         76 my $data = [
357             { key => 'metric_id' , required => 1 },
358             { key => 'value' , required => 1 },
359             { key => 'generated_at', required => 0 },
360             { key => 'meta' , required => 0 },
361             { key => 'ref_id' , required => 0 }
362             ];
363 12         38 validate($data, $params);
364              
365 2 50 33     11 if (exists $params->{meta} && defined $params->{meta}) {
366 2 50       19 die "ERROR: Invalid data format for key 'meta'."
367             unless (ref($params->{meta}) eq 'HASH');
368 0         0 $params->{meta} = to_json($params->{meta});
369             }
370              
371 0         0 $params->{format} = $self->format;
372 0         0 my $response = $self->post($self->stats_url, [ %$params ]);
373 0         0 my $content = from_json($response->content);
374              
375 0         0 return WWW::StatsMix::Stat->new($content->{stat});
376             }
377              
378             =head2 get_stat($metric_id, \%params)
379              
380             Returns the stat details of the given stat of the metric. The stat can be either
381             search by stat id or ref id. The return data is of type L.It
382             requires mandatory key 'metric_id'. If both 'id' and 'ref_id' are defined then
383             'id' takes the precedence.
384              
385             +--------+------------------------------------------------------------------+
386             | Key | Description |
387             +--------+------------------------------------------------------------------+
388             | id | The stat id of the stat. Required only if 'ref_id' is undefined. |
389             | | |
390             | ref_id | Ref id of the stat. Required only if 'id' is undefined. |
391             +--------+------------------------------------------------------------------+
392              
393             use strict; use warnings;
394             use WWW::StatsMix;
395              
396             my $API_KEY = "Your_API_Key";
397             my $api = WWW::StatsMix->new(api_key => $API_KEY);
398             my $metric_id = ;
399             my $stat_id = ;
400             my $stat_ref_id = ;
401              
402             my $stat_by_id = $api->get_stat($metric_id, { id => $stat_id });
403             my $stat_by_ref_id = $api->get_stat($metric_id, { ref_id => $stat_ref_id });
404              
405             print "Stat by Id (name) : ", $stat_by_id->name, "\n";
406             print "Stat by Ref Id (name): ", $stat_by_ref_id->name, "\n";
407              
408             =cut
409              
410             sub get_stat {
411 8     8 1 4599 my ($self, $metric, $params) = @_;
412              
413 8 100       27 die "ERROR: Missing the required key metric id." unless defined $metric;
414 7 100       36 die "ERROR: Invalid the required key metric id [$metric]." unless ($metric =~ /^\d+$/);
415              
416 6         21 my $data = [
417             { key => 'id' , required => 0 },
418             { key => 'ref_id', required => 0 }
419             ];
420 6         16 validate($data, $params);
421              
422 0         0 my $id = _get_id($params);
423 0         0 my $url = sprintf("%s/%d.json?metric_id=%d", $self->stats_url, $id, $metric);
424 0         0 my $response = $self->get($url);
425 0         0 my $content = from_json($response->content);
426              
427 0         0 return WWW::StatsMix::Stat->new($content->{stat});
428             }
429              
430             =head2 update_stat($metric_id, \%params)
431              
432             Update the stat of the metric.Stat can be located by stat id or ref id.Parameters
433             for the method are as below. The return data is of type L.It
434             requires mandatory key 'metric_id' and params as hash ref. Following keys can be
435             passed in hash ref. If both 'id' and 'ref_id' are defined then 'id' takes the
436             precedence.
437              
438             +------------+----------------------------------------------------------------------------+
439             | Key | Description |
440             +------------+----------------------------------------------------------------------------+
441             | value | The numeric value of the stat with a decimal precision of two. Decimal (up |
442             | (required) | to 11 digits on the left side of the decimal point, two on the right). |
443             | | |
444             | id | The stat id of the stat. Required only if 'ref_id' is undefined. |
445             | | |
446             | ref_id | Ref id of the stat. Required only if 'id' is undefined. |
447             +------------+----------------------------------------------------------------------------+
448              
449             use strict; use warnings;
450             use WWW::StatsMix;
451              
452             my $API_KEY = "Your_API_Key";
453             my $api = WWW::StatsMix->new(api_key => $API_KEY);
454             my $metric_id = ;
455             my $value = ;
456             my $stat_id = ;
457             my $stat_ref_id = ;
458              
459             my $stat_by_id = $api->update_stat($metric_id, { id => $stat_id, value => $value });
460             my $stat_by_ref_id = $api->update_stat($metric_id, { ref_id => $stat_ref_id, value => $value });
461              
462             print "Stat by Id (value) : ", $stat_by_id->value, "\n";
463             print "Stat by Ref Id (value): ", $stat_by_ref_id->value, "\n";
464              
465             =cut
466              
467             sub update_stat {
468 10     10 1 9195 my ($self, $metric, $params) = @_;
469              
470 10 100       42 die "ERROR: Missing the required key metric id." unless defined $metric;
471 9 100       65 die "ERROR: Invalid the required key metric id [$metric]." unless ($metric =~ /^\d+$/);
472              
473 8         52 my $data = [
474             { key => 'value' , required => 1 },
475             { key => 'id' , required => 0 },
476             { key => 'ref_id', required => 0 }
477             ];
478 8         30 validate($data, $params);
479              
480 1         6 my $id = _get_id($params);
481 0         0 my $value = _get_value($params);
482 0         0 my $_data = { metric_id => $metric, value => $value };
483 0         0 my $url = sprintf("%s/%d.json", $self->stats_url, $id);
484 0         0 my $response = $self->put($url, [ %$_data ]);
485 0         0 my $content = from_json($response->content);
486              
487 0         0 return WWW::StatsMix::Stat->new($content->{stat});
488             }
489              
490             =head2 delete_stat($metric_id, \%params)
491              
492             Delete the stat of the metric.Stat can be located by stat id or ref id.Parameters
493             for the method are as below. The return data is of type L.It
494             requires mandatory key 'metric_id' and params as hash ref. The hash ref can have
495             either 'id' or 'ref_id'. If both specified then 'id' takes the precedence.
496              
497             +--------+------------------------------------------------------------------+
498             | Key | Description |
499             +--------+------------------------------------------------------------------+
500             | id | The stat id of the stat. Required only if 'ref_id' is undefined. |
501             | | |
502             | ref_id | Ref id of the stat. Required only if 'id' is undefined. |
503             +--------+------------------------------------------------------------------+
504              
505             use strict; use warnings;
506             use WWW::StatsMix;
507              
508             my $API_KEY = "Your_API_Key";
509             my $api = WWW::StatsMix->new(api_key => $API_KEY);
510             my $metric_id = ;
511             my $stat_id = ;
512             my $stat_ref_id = ;
513              
514             my $stat_by_id = $api->delete_stat($metric_id, { id => $stat_id });
515             my $stat_by_ref_id = $api->delete_stat($metric_id, { ref_id => $stat_ref_id });
516              
517             =cut
518              
519             sub delete_stat {
520 9     9 1 5816 my ($self, $metric, $params) = @_;
521              
522 9 100       29 die "ERROR: Missing the required key metric id." unless defined $metric;
523 8 100       40 die "ERROR: Invalid the required key metric id [$metric]." unless ($metric =~ /^\d+$/);
524              
525 7         27 my $data = [
526             { key => 'id' , required => 0 },
527             { key => 'ref_id', required => 0 }
528             ];
529 7         20 validate($data, $params);
530              
531 1         4 my $id = _get_id($params);
532 0         0 my $url = sprintf("%s/%d.json?metric_id=%d", $self->stats_url, $id, $metric);
533 0         0 my $response = $self->delete($url);
534 0         0 my $content = from_json($response->content);
535              
536 0         0 return WWW::StatsMix::Stat->new($content->{stat});
537             }
538              
539             =head2 get_stats(\%params)
540              
541             The method get_stats() will return a default of up to 50 records. The parameter
542             limit can be passed to specify the number of records to return. The parameter
543             metric_id can also be used to scope records to a particular profile. Parameters
544             start_date & end_date can be used to limit the date range based on the timestamp
545             in a stat's generated_at.
546              
547             +------------+---------------------------------------------------------------+
548             | Key | Description |
549             +------------+---------------------------------------------------------------+
550             | limit | Limit the number of metrics. Default is 50. |
551             | (optional) | |
552             | | |
553             | metric_id | Scope the search to a particular metric. |
554             | (optional) | |
555             | | |
556             | start_date | Limit the searh in date range against stats generated_at key. |
557             | / end_date | Valid format is YYYY-MM-DD. |
558             | (optional) | |
559             +------------+---------------------------------------------------------------+
560              
561             use strict; use warnings;
562             use WWW::StatsMix;
563              
564             my $API_KEY = "Your_API_Key";
565             my $api = WWW::StatsMix->new(api_key => $API_KEY);
566             my $metric_id = ;
567             my $limit = ;
568              
569             my $stats = $api->get_stats();
570             my $stats_by_metric = $api->get_stats({ metric_id => $metric_id });
571             my $stats_by_metric_by_limit = $api->get_stats({ metric_id => $metric_id, limit => $limit });
572              
573             =cut
574              
575             sub get_stats {
576 7     7 1 4938 my ($self, $params) = @_;
577              
578 7         42 my $url = sprintf("%s?format=%s", $self->stats_url, $self->format);
579 7 50       16 if (defined $params) {
580 7         37 my $data = [
581             { key => 'limit' , required => 0 },
582             { key => 'metric_id' , required => 0 },
583             { key => 'start_date', required => 0 },
584             { key => 'end_date' , required => 0 },
585             ];
586 7         25 validate($data, $params);
587              
588 1 50 33     6 if (exists $params->{start_date} && defined $params->{start_date}) {
    0 0        
589 1 50 33     16 die "ERROR: Missing param key 'end_date'."
590             unless (exists $params->{end_date} && defined $params->{end_date});
591 0 0       0 die "ERROR: Invalid param key 'start_date'."
592             unless _is_valid_date($params->{start_date});
593 0 0       0 die "ERROR: Invalid param key 'end_date'."
594             unless _is_valid_date($params->{end_date});
595             }
596             elsif (exists $params->{end_date} && defined $params->{end_date}) {
597 0 0 0     0 die "ERROR: Missing param key 'start_date'."
598             unless (exists $params->{start_date} && defined $params->{start_date});
599             }
600              
601 0         0 foreach (qw(limit metric_id start_date end_date)) {
602 0 0 0     0 if (exists $params->{$_} && defined $params->{$_}) {
603 0         0 $url .= sprintf("&%s=%s", $_, $params->{$_});
604             }
605             }
606             }
607              
608 0         0 my $response = $self->get($url);
609 0         0 my $content = from_json($response->content);
610              
611 0         0 return _get_stats($content);
612             }
613              
614             =head2 track(\%params)
615              
616             It combines the functions create_stat() and create_metric() (if necessary) into a
617             single method call. If no value is passed, the default of 1 is returned. Returns
618             an object of type L.
619              
620             +--------------+----------------------------------------------------------------------------+
621             | Key | Description |
622             +--------------+----------------------------------------------------------------------------+
623             | name | The name of the metric you are tracking. If a metric with that name does |
624             | (required) | not exist in your account, one will be created automatically. |
625             | | |
626             | value | The numeric value of the stat with a decimal precision of two. Decimal (up |
627             | (optional) | to 11 digits on the left side of the decimal point, two on the right). If |
628             | | missing default value 1 is assigned. |
629             | | |
630             | generated_at | Datetime for the stat. If not set, defaults to the current timestamp. This |
631             | (optional) | is the datetime to be used in the charts. Valid format is YYYY-MM-DD. |
632             | | |
633             | meta | hashref data (key,value pair) about anything associated with the stat. |
634             | (optional) | |
635             | | |
636             | ref_id | Optional reference id for a stat. If a stat already exists for the named |
637             | (optional) | metric and the given ref_id, the value (and optionally generated_at and |
638             | | meta) will be updated instead of created. |
639             | | |
640             | profile_id | The unique id of the profile this stat belongs to. If not set, the metric |
641             | (optional) | will use the first profile_id created in your account. (Developer, Basic, |
642             | | and Standard plans only have one profile.) |
643             +--------------+----------------------------------------------------------------------------+
644              
645             use strict; use warnings;
646             use WWW::StatsMix;
647              
648             my $API_KEY = "Your_API_Key";
649             my $api = WWW::StatsMix->new(api_key => $API_KEY);
650             my $name = ;
651             my $params = { name => $metric_name };
652             my $stat = $api->track($params);
653              
654             print "Id: ", $stat->id, "\n";
655              
656             =cut
657              
658             sub track {
659 8     8 1 5932 my ($self, $params) = @_;
660              
661 8         80 my $data = [
662             { key => 'name' , required => 1 },
663             { key => 'value' , required => 0 },
664             { key => 'generated_at', required => 0 },
665             { key => 'meta' , required => 0 },
666             { key => 'ref_id' , required => 0 },
667             { key => 'profile_id' , required => 0 }
668             ];
669 8         25 validate($data, $params);
670              
671 0 0 0     0 $params->{meta} = to_json($params->{meta})
672             if (exists $params->{meta} && defined $params->{meta});
673              
674 0         0 $params->{format} = $self->format;
675 0         0 my $response = $self->post($self->track_url, [ %$params ]);
676 0         0 my $content = from_json($response->content);
677              
678 0         0 return WWW::StatsMix::Stat->new($content->{stat});
679             }
680              
681             # PRIVATE METHODS
682             #
683             #
684              
685             sub _get_id {
686 2     2   5 my ($params) = @_;
687              
688 2 50 33     17 if (defined $params && (ref($params) eq 'HASH')) {
689 2 50 33     59 if (exists $params->{id} && defined $params->{id}) {
    50 33        
690 0         0 return $params->{id};
691             }
692             elsif (exists $params->{ref_id} && defined $params->{ref_id}) {
693 0         0 return $params->{ref_id};
694             }
695             }
696              
697 2         20 die "ERROR: Missing required key id/ref_id";
698             }
699              
700             sub _get_value {
701 0     0     my ($params) = @_;
702              
703 0 0 0       return $params->{value}
      0        
704             if (defined $params && exists $params->{value} && defined $params->{value});
705              
706 0           die "ERROR: Missing required key 'value'.";
707             }
708              
709             sub _get_metrics {
710 0     0     my ($content) = @_;
711              
712 0           my $metrics = [];
713 0           foreach (@{$content->{metrics}->{metric}}) {
  0            
714 0           push @$metrics, WWW::StatsMix::Metric->new($_);
715             }
716              
717 0           return $metrics;
718             }
719              
720             sub _get_stats {
721 0     0     my ($content) = @_;
722              
723 0           my $stats = [];
724 0           foreach (@{$content->{stats}->{stat}}) {
  0            
725 0           push @$stats, WWW::StatsMix::Stat->new($_);
726             }
727              
728 0           return $stats;
729             }
730              
731             sub _now_yyyy_mm_dd_hh_mi_ss {
732 0     0     my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
733 0           return sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year+=1900, ++$mon, $mday, $hour, $min, $sec);
734             }
735              
736             sub _now_yyyy_mm_dd {
737 0     0     my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
738 0           return sprintf("%04d-%02d-%02d", $year+=1900, ++$mon, $mday);
739             }
740              
741             =head1 AUTHOR
742              
743             Mohammad S Anwar, C<< >>
744              
745             =head1 REPOSITORY
746              
747             L
748              
749             =head1 BUGS
750              
751             Please report any bugs or feature requests to C,
752             or through the web interface at L.
753             I will be notified, and then you'll automatically be notified of progress on your
754             bug as I make changes.
755              
756             =head1 SUPPORT
757              
758             You can find documentation for this module with the perldoc command.
759              
760             perldoc WWW::StatsMix
761              
762             You can also look for information at:
763              
764             =over 4
765              
766             =item * RT: CPAN's request tracker (report bugs here)
767              
768             L
769              
770             =item * AnnoCPAN: Annotated CPAN documentation
771              
772             L
773              
774             =item * CPAN Ratings
775              
776             L
777              
778             =item * Search CPAN
779              
780             L
781              
782             =back
783              
784             =head1 LICENSE AND COPYRIGHT
785              
786             Copyright (C) 2014 - 2015 Mohammad S Anwar.
787              
788             This program is free software; you can redistribute it and/or modify it under
789             the terms of the the Artistic License (2.0). You may obtain a copy of the full
790             license at:
791              
792             L
793              
794             Any use, modification, and distribution of the Standard or Modified Versions is
795             governed by this Artistic License.By using, modifying or distributing the Package,
796             you accept this license. Do not use, modify, or distribute the Package, if you do
797             not accept this license.
798              
799             If your Modified Version has been derived from a Modified Version made by someone
800             other than you,you are nevertheless required to ensure that your Modified Version
801             complies with the requirements of this license.
802              
803             This license does not grant you the right to use any trademark, service mark,
804             tradename, or logo of the Copyright Holder.
805              
806             This license includes the non-exclusive, worldwide, free-of-charge patent license
807             to make, have made, use, offer to sell, sell, import and otherwise transfer the
808             Package with respect to any patent claims licensable by the Copyright Holder that
809             are necessarily infringed by the Package. If you institute patent litigation
810             (including a cross-claim or counterclaim) against any party alleging that the
811             Package constitutes direct or contributory patent infringement,then this Artistic
812             License to you shall terminate on the date that such litigation is filed.
813              
814             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
815             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
816             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
817             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
818             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
819             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
820             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
821              
822             =cut
823              
824             1; # End of WWW::StatsMix