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