File Coverage

blib/lib/WebService/Strava/Athlete/Segment_Effort.pm
Criterion Covered Total %
statement 39 69 56.5
branch 0 14 0.0
condition n/a
subroutine 15 19 78.9
pod 0 1 0.0
total 54 103 52.4


line stmt bran cond sub pod time code
1             package WebService::Strava::Athlete::Segment_Effort;
2              
3 4     4   56 use v5.010;
  4         23  
  4         212  
4 4     4   22 use strict;
  4         7  
  4         170  
5 4     4   18 use warnings;
  4         6  
  4         156  
6 4     4   21 use Moo;
  4         7  
  4         31  
7 4     4   1425 use Method::Signatures;
  4         11  
  4         34  
8 4     4   1650 use Scalar::Util qw(looks_like_number);
  4         7  
  4         312  
9 4     4   21 use Carp qw(croak);
  4         5  
  4         196  
10 4     4   18 use Scalar::Util::Reftype;
  4         6  
  4         203  
11 4     4   23 use experimental 'switch';
  4         5  
  4         32  
12 4     4   609 use Data::Dumper;
  4         8  
  4         2616  
13              
14             # ABSTRACT: A Strava Segment Effort Object
15              
16             our $VERSION = '0.04'; # VERSION: Generated by DZP::OurPkg:Version
17              
18              
19             # Validation functions
20              
21             my $Ref = sub {
22             croak "auth isn't a 'WebService::Strava::Auth' object!" unless reftype( $_[0] )->class eq "WebService::Strava::Auth";
23             };
24              
25             my $Bool = sub {
26             croak "$_[0] must be 0|1" unless $_[0] =~ /^[01]$/;
27             };
28              
29             my $Num = sub {
30             croak "$_[0] isn't a valid id" unless looks_like_number $_[0];
31             };
32              
33             # Debugging hooks in case things go weird. (Thanks @pjf)
34              
35             around BUILDARGS => sub {
36             my $orig = shift;
37             my $class = shift;
38            
39             if ($WebService::Strava::DEBUG) {
40             warn "Building task with:\n";
41             warn Dumper(\@_), "\n";
42             }
43            
44             return $class->$orig(@_);
45             };
46              
47             # Authentication Object
48             has 'auth' => ( is => 'ro', required => 1, isa => $Ref );
49              
50             # Defaults + Required
51             has 'id' => ( is => 'ro', required => 1, isa => $Num);
52             has '_build' => ( is => 'ro', default => sub { 1 }, isa => $Bool );
53              
54             # Segment Effort
55             has 'name' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
56             has 'resource_state' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
57             has 'activity' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
58             has 'elapsed_time' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
59             has 'moving_time' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
60             has 'start_date' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
61             has 'start_date_local' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
62             has 'distance' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
63             has 'start_index' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
64             has 'end_index' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
65             has 'average_cadence' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
66             has 'average_watts' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
67             has 'average_heartrate' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
68             has 'max_heartrate' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
69             has 'segment' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
70             has 'kom_rank' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
71             has 'pr_rank' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
72             has 'hidden' => ( is => 'ro', lazy => 1, builder => '_build_effort' );
73              
74             sub BUILD {
75 0     0 0   my $self = shift;
76              
77 0 0         if ($self->{_build}) {
78 0           $self->_build_effort();
79             }
80 0           return;
81             }
82              
83 4 0   4   3080 method _build_effort() {
  0     0      
  0            
84 0           my $effort = $self->auth->get_api("/segment_efforts/$self->{id}");
85            
86 0           foreach my $key (keys %{ $effort }) {
  0            
87 0           given ( $key ) {
88 0           when (/athlete/) { $self->_instantiate("Athlete", $key, $effort->{$key}); }
  0            
89 0           when (/segment/) { $self->_instantiate("Segment", $key, $effort->{$key}); }
  0            
90 0           default { $self->{$key} = $effort->{$key}; }
  0            
91             }
92             }
93              
94 0           return;
95             }
96              
97 4     4   2062 use WebService::Strava::Athlete;
  4         8  
  4         129  
98 4     4   2133 use WebService::Strava::Segment;
  4         12  
  4         190  
99              
100 4 0   4   17845 method _instantiate($type, $key, $data) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
101 0           $self->{$key} = "WebService::Strava::$type"->new(auth => $self->auth, id => $data->{id}, _build => 0);
102 0           return;
103             }
104              
105              
106 4 0   4   3602 method retrieve() {
  0     0      
  0            
107 0           $self->_build_effort();
108             }
109              
110             1;
111              
112             __END__
113              
114             =pod
115              
116             =encoding UTF-8
117              
118             =head1 NAME
119              
120             WebService::Strava::Athlete::Segment_Effort - A Strava Segment Effort Object
121              
122             =head1 VERSION
123              
124             version 0.04
125              
126             =head1 SYNOPSIS
127              
128             my $effort = WebService::Strava::Athlete::Segment_Effort->new( auth => $auth, id => '229781' );
129              
130             =head1 DESCRIPTION
131              
132             Upon instantiation will retrieve the segment effort matching
133             the id. Requires a pre-authenticated WebService::Strava::Auth
134             object.
135              
136             =head1 METHODS
137              
138             =head2 retrieve()
139              
140             $effort->retrieve();
141              
142             When a Effort object is lazy loaded, you can call retrieve it by calling
143             this method.
144              
145             =head1 AUTHOR
146              
147             Leon Wright < techman@cpan.org >
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is copyright (c) 2014 by Leon Wright.
152              
153             This is free software; you can redistribute it and/or modify it under
154             the same terms as the Perl 5 programming language system itself.
155              
156             =cut