File Coverage

blib/lib/WebService/Strava/Athlete.pm
Criterion Covered Total %
statement 45 99 45.4
branch 0 22 0.0
condition n/a
subroutine 17 21 80.9
pod 0 1 0.0
total 62 143 43.3


line stmt bran cond sub pod time code
1             package WebService::Strava::Athlete;
2              
3 4     4   100 use v5.010;
  4         11  
  4         230  
4 4     4   19 use strict;
  4         5  
  4         118  
5 4     4   14 use warnings;
  4         9  
  4         137  
6 4     4   18 use Moo;
  4         6  
  4         26  
7 4     4   1368 use Method::Signatures;
  4         7  
  4         34  
8 4     4   1558 use Scalar::Util qw(looks_like_number);
  4         7  
  4         243  
9 4     4   2475 use Scalar::Util::Reftype;
  4         18474  
  4         281  
10 4     4   29 use Carp qw(croak);
  4         5  
  4         172  
11 4     4   19 use experimental 'switch';
  4         6  
  4         32  
12 4     4   651 use Data::Dumper;
  4         11  
  4         2662  
13              
14             # ABSTRACT: A Strava Athlete Object
15              
16             our $VERSION = '0.04'; # VERSION: Generated by DZP::OurPkg:Version
17              
18              
19             # Validation functions
20              
21             my $Id = sub {
22             if ($_[0]) {
23             croak "$_[0] isn't a valid id" unless looks_like_number $_[0];
24             }
25             };
26              
27             my $Ref = sub {
28             croak "auth isn't a 'WebService::Strava::Auth' object!" unless reftype( $_[0] )->class eq "WebService::Strava::Auth";
29             };
30              
31             my $Bool = sub {
32             croak "$_[0] must be 0|1" unless $_[0] =~ /^[01]$/;
33             };
34              
35             # Debugging hooks in case things go weird. (Thanks @pjf)
36              
37             around BUILDARGS => sub {
38             my $orig = shift;
39             my $class = shift;
40            
41             if ($WebService::Strava::DEBUG) {
42             warn "Building task with:\n";
43             warn Dumper(\@_), "\n";
44             }
45            
46             return $class->$orig(@_);
47             };
48              
49             # Authentication Object
50             has 'auth' => ( is => 'ro', required => 1, isa => $Ref );
51              
52             # Defaults + Required
53             has 'id' => ( is => 'ro', isa => $Id );
54             has '_build' => ( is => 'ro', default => sub { 1 }, isa => $Bool );
55              
56             # Athlete API
57             has 'name' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
58             has 'resource_state' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
59             has 'firstname' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
60             has 'lastname' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
61             has 'profile_medium' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
62             has 'profile' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
63             has 'city' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
64             has 'state' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
65             has 'country' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
66             has 'sex' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
67             has 'friend' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
68             has 'follower' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
69             has 'premium' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
70             has 'created_at' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
71             has 'updated_at' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
72             has 'approve_followers' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
73             has 'friend_count' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
74             has 'mutual_friend_count' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
75             has 'date_preference' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
76             has 'measurement_preference' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
77             has 'email' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
78             has 'ftp' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
79             has 'clubs' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
80             has 'bikes' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
81             has 'shoes' => ( is => 'ro', lazy => 1, builder => '_build_athlete' );
82              
83             sub BUILD {
84 0     0 0   my $self = shift;
85              
86 0 0         if ($self->{_build}) {
87 0           $self->_build_athlete();
88             }
89 0           return;
90             }
91              
92 4 0   4   7807 method _build_athlete() {
  0     0      
  0            
93 0           my $athlete;
94 0 0         if ($self->id) {
95 0           $athlete = $self->auth->get_api("/athletes/$self->{id}");
96             } else {
97 0           $athlete = $self->auth->get_api("/athlete");
98             }
99            
100 0           foreach my $key (keys %{ $athlete }) {
  0            
101 0           given ( $key ) {
102 0           when (/bikes/) { $self->_instantiate("Athlete::Gear::Bike", $key, $athlete->{$key}); }
  0            
103 0           when (/shoes/) { $self->_instantiate("Athlete::Gear::Shoe", $key, $athlete->{$key}); }
  0            
104 0           when (/clubs/) { $self->_instantiate("Club", $key, $athlete->{$key}); }
  0            
105 0           default { $self->{$key} = $athlete->{$key}; }
  0            
106             }
107             }
108            
109 0           return;
110             }
111              
112 4     4   3217 use WebService::Strava::Athlete::Gear::Bike;
  4         11  
  4         181  
113 4     4   1881 use WebService::Strava::Athlete::Gear::Shoe;
  4         11  
  4         122  
114 4     4   1624 use WebService::Strava::Club;
  4         14  
  4         160  
115              
116 4 0   4   18201 method _instantiate($type, $key, $data) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
117 0           my $index = 0;
118 0           foreach my $item (@{$data}) {
  0            
119 0           @{$data}[$index] = "WebService::Strava::$type"->new(auth => $self->auth, id => $item->{id}, _build => 0);
  0            
120 0           $index++;
121             }
122 0           $self->{$key} = $data;
123 0           return;
124             }
125              
126              
127 4     4   4000 use WebService::Strava::Athlete::Segment_Effort;
  4         12  
  4         163  
128              
129 4 0   4   29190 method list_records(:$efforts = 25,:$page = 1) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
130             # TODO: Handle pagination better use #4's solution when found.
131 0           my $records = $self->auth->get_api("/athletes/$self->{id}/koms?per_page=$efforts&page=$page");
132 0           my $index = 0;
133 0           foreach my $record (@{$records}) {
  0            
134 0           @{$records}[$index] = WebService::Strava::Athlete::Segment_Effort->new(id => $record->{id}, auth => $self->auth, _build => 0);
  0            
135 0           $index++;
136             }
137 0           return $records;
138             };
139              
140             1;
141              
142             __END__
143              
144             =pod
145              
146             =encoding UTF-8
147              
148             =head1 NAME
149              
150             WebService::Strava::Athlete - A Strava Athlete Object
151              
152             =head1 VERSION
153              
154             version 0.04
155              
156             =head1 SYNOPSIS
157              
158             my $athlete = WebService::Strava::Athelete->new( auth => $auth, [id => '229781'] );
159              
160             =head1 DESCRIPTION
161              
162             Upon instantiation will retrieve the athlete matching the id.
163             Requires a pre-authenticated WebService::Strava::Auth object.
164              
165             =head1 METHODS
166              
167             =head2 list_records()
168              
169             $athlete->list_records([page => 2], [efforts => 100])'
170              
171             Returns an arrayRef K|Q of Mountain + Course record Segment effort objects for the instantiated athlete. Takes 2 optional
172             parameters of 'page' and 'efforts'.
173              
174             The results are paginated and a maximum of 200 results can be returned
175             per page.
176              
177             =head1 AUTHOR
178              
179             Leon Wright < techman@cpan.org >
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             This software is copyright (c) 2014 by Leon Wright.
184              
185             This is free software; you can redistribute it and/or modify it under
186             the same terms as the Perl 5 programming language system itself.
187              
188             =cut