| 1 | package WebService::Strava::Athlete; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 4 | ▶ | use v5.010; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 4 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 4 | ▶ | use Scalar::Util::Reftype; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 4 | ▶ | use Data::Dumper; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 4 | ▶ | use Method::Signatures 20140224; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 4 | ▶ | use Moo; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 4 | ▶ | use experimental 'switch'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | # ABSTRACT: A Strava Athlete Object | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 4 | our $VERSION = '0.06'; # VERSION: Generated by DZP::OurPkg:Version | |||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | # Validation functions | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 0 | ▶ | my $Id = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 23 | 0 | ▶ | if ($_[0]) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 24 | 0 | ▶ | croak "$_[0] isn't a valid id" unless looks_like_number $_[0]; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||
| 26 | 4 | }; | |||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 0 | ▶ | my $Ref = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 29 | 0 | ▶ | croak "auth isn't a 'WebService::Strava::Auth' object!" unless reftype( $_[0] )->class eq "WebService::Strava::Auth"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 30 | 4 | }; | |||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 0 | ▶ | my $Bool = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 33 | 0 | ▶ | croak "$_[0] must be 0|1" unless $_[0] =~ /^[01]$/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 34 | 4 | }; | |||||||||||||||||||||
| 35 | |||||||||||||||||||||||
| 36 | # Debugging hooks in case things go weird. (Thanks @pjf) | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 0 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 39 | 0 | my $orig = shift; | |||||||||||||||||||||
| 40 | 0 | my $class = shift; | |||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | 0 | ▶ | if ($WebService::Strava::DEBUG) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 43 | 0 | warn "Building task with:\n"; | |||||||||||||||||||||
| 44 | 0 | warn Dumper(\@_), "\n"; | |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | 0 | return $class->$orig(@_); | |||||||||||||||||||||
| 48 | 4 | }; | |||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | # Authentication Object | ||||||||||||||||||||||
| 51 | 4 | has 'auth' => ( is => 'ro', required => 1, isa => $Ref ); | |||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | # Defaults + Required | ||||||||||||||||||||||
| 54 | 4 | has 'id' => ( is => 'ro', isa => $Id ); | |||||||||||||||||||||
| 55 | 0 | ▶ | has '_build' => ( is => 'ro', default => sub { 1 }, isa => $Bool ); | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | # Athlete API | ||||||||||||||||||||||
| 58 | 4 | has 'name' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 59 | 4 | has 'resource_state' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 60 | 4 | has 'firstname' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 61 | 4 | has 'lastname' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 62 | 4 | has 'profile_medium' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 63 | 4 | has 'profile' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 64 | 4 | has 'city' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 65 | 4 | has 'state' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 66 | 4 | has 'country' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 67 | 4 | has 'sex' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 68 | 4 | has 'friend' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 69 | 4 | has 'follower' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 70 | 4 | has 'premium' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 71 | 4 | has 'created_at' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 72 | 4 | has 'updated_at' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 73 | 4 | has 'approve_followers' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 74 | 4 | has 'friend_count' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 75 | 4 | has 'mutual_friend_count' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 76 | 4 | has 'date_preference' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 77 | 4 | has 'measurement_preference' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 78 | 4 | has 'email' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 79 | 4 | has 'ftp' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 80 | 4 | has 'clubs' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 81 | 4 | has 'bikes' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 82 | 4 | has 'shoes' => ( is => 'ro', lazy => 1, builder => '_build_athlete' ); | |||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||
| 85 | 0 | my $self = shift; | |||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | 0 | ▶ | if ($self->{_build}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 88 | 0 | $self->_build_athlete(); | |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 0 | ▶ | method _build_athlete() { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
_build_athlete: not called CC=4 SCAR=27.9
Branch: 1 branch
| |||||||||||||||||||||||
| 94 | 0 | my $athlete; | |||||||||||||||||||||
| 95 | 0 | ▶ | if ($self->id) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | 0 | $athlete = $self->auth->get_api("/athletes/$self->{id}"); | |||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||
| 98 | 0 | $athlete = $self->auth->get_api("/athlete"); | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 0 | foreach my $key (keys %{ $athlete }) { | |||||||||||||||||||||
| 102 | 0 | given ( $key ) { | |||||||||||||||||||||
| 103 | 0 | when (/bikes/) { $self->_instantiate("Athlete::Gear::Bike", $key, $athlete->{$key}); } | |||||||||||||||||||||
| 104 | 0 | when (/shoes/) { $self->_instantiate("Athlete::Gear::Shoe", $key, $athlete->{$key}); } | |||||||||||||||||||||
| 105 | 0 | when (/clubs/) { $self->_instantiate("Club", $key, $athlete->{$key}); } | |||||||||||||||||||||
| 106 | 0 | default { $self->{$key} = $athlete->{$key}; } | |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | 0 | return; | |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | 4 | ▶ | use WebService::Strava::Athlete::Gear::Bike; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 114 | 4 | ▶ | use WebService::Strava::Athlete::Gear::Shoe; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 115 | 4 | ▶ | use WebService::Strava::Club; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | 0 | ▶ | method _instantiate($type, $key, $data) { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=5.3
Subroutine
_instantiate: not called CC=6 SCAR=35.4
Branch: 4 branches
| |||||||||||||||||||||||
| 118 | 0 | my $index = 0; | |||||||||||||||||||||
| 119 | 0 | foreach my $item (@{$data}) { | |||||||||||||||||||||
| 120 | 0 | @{$data}[$index] = "WebService::Strava::$type"->new(auth => $self->auth, id => $item->{id}, _build => 0); | |||||||||||||||||||||
| 121 | 0 | $index++; | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | 0 | $self->{$key} = $data; | |||||||||||||||||||||
| 124 | 0 | return; | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | 4 | ▶ | use WebService::Strava::Athlete::Segment_Effort; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | 0 | ▶ | method list_records(:$efforts = 25,:$page = 1) { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=5.3
Subroutine
list_records: not called CC=6 SCAR=35.4
Branch: 4 branches
| |||||||||||||||||||||||
| 131 | # TODO: Handle pagination better use #4's solution when found. | ||||||||||||||||||||||
| 132 | 0 | my $records = $self->auth->get_api("/athletes/$self->{id}/koms?per_page=$efforts&page=$page"); | |||||||||||||||||||||
| 133 | 0 | my $index = 0; | |||||||||||||||||||||
| 134 | 0 | foreach my $record (@{$records}) { | |||||||||||||||||||||
| 135 | 0 | @{$records}[$index] = WebService::Strava::Athlete::Segment_Effort->new(id => $record->{id}, auth => $self->auth, _build => 0); | |||||||||||||||||||||
| 136 | 0 | $index++; | |||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||
| 138 | 0 | return $records; | |||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | 4 | 1; | |||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||