File Coverage

blib/lib/WebService/Strava/Segment.pm
Criterion Covered Total %
statement 38 118 32.2
branch 0 48 0.0
condition n/a
subroutine 16 22 72.7
pod 0 1 0.0
total 54 189 28.5


line stmt bran cond sub pod time code
1             package WebService::Strava::Segment;
2              
3 4     4   38 use v5.010;
  4         10  
  4         166  
4 4     4   19 use strict;
  4         4  
  4         113  
5 4     4   16 use warnings;
  4         5  
  4         99  
6 4     4   17 use Moo;
  4         6  
  4         18  
7 4     4   1099 use Method::Signatures;
  4         9  
  4         27  
8 4     4   1437 use Scalar::Util qw(looks_like_number);
  4         7  
  4         186  
9 4     4   21 use Scalar::Util::Reftype;
  4         11  
  4         166  
10 4     4   18 use Carp qw(croak);
  4         4  
  4         152  
11 4     4   16 use experimental 'switch';
  4         5  
  4         19  
12 4     4   393 use Data::Dumper;
  4         6  
  4         2405  
13              
14             # ABSTRACT: A Strava Segment Object
15              
16             our $VERSION = '0.05'; # VERSION: Generated by DZP::OurPkg:Version
17              
18              
19             # Validation functions
20              
21             my $Num = sub {
22             croak "$_[0] isn't a number" unless looks_like_number $_[0];
23             };
24              
25             my $Ref = sub {
26             croak "auth isn't a 'WebService::Strava::Auth' object!" unless reftype( $_[0] )->class eq "WebService::Strava::Auth";
27             };
28              
29             my $Bool = sub {
30             croak "$_[0] must be 0|1" unless $_[0] =~ /^[01]$/;
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 API
55             has 'name' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
56             has 'activity_type' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
57             has 'distance' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
58             has 'average_grade' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
59             has 'maximum_grade' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
60             has 'elevation_high' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
61             has 'elevation_low' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
62             has 'start_latlng' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
63             has 'end_latlng' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
64             has 'climb_category' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
65             has 'city' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
66             has 'state' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
67             has 'country' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
68             has 'private' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
69             has 'starred' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
70             has 'map' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
71             has 'athlete_count' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
72             has 'resource_state' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
73             has 'effort_count' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
74             has 'total_elevation_gain' => ( is => 'ro', lazy => 1, builder => '_build_segment' );
75              
76             sub BUILD {
77 0     0 0   my $self = shift;
78              
79 0 0         if ($self->{_build}) {
80 0           $self->_build_segment();
81             }
82 0           return;
83             }
84              
85 4 0   4   2144 method _build_segment() {
  0     0      
  0            
86 0           my $segment = $self->auth->get_api("/segments/$self->{id}");
87            
88 0           foreach my $key (keys %{ $segment }) {
  0            
89 0           given ( $key ) {
90 0           when ("athlete") { $self->_instantiate("Athlete", $key, $segment->{$key}); }
  0            
91 0           default { $self->{$key} = $segment->{$key}; }
  0            
92             }
93             }
94              
95 0           return;
96             }
97              
98 4     4   878 use WebService::Strava::Athlete;
  4         7  
  4         63  
99              
100 4 0   4   13234 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   2983 method retrieve() {
  0     0      
  0            
107 0           $self->_build_segment();
108             }
109              
110              
111 4 0   4   33434 method list_efforts(:$efforts = 25,:$page = 1,:$athlete_id, :$raw = 0) {
  0 0   0      
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
112             # TODO: Handle pagination better #4
113 0           my $data;
114 0 0         if ($athlete_id) {
115 0           $data = $self->auth->get_api("/segments/$self->{id}/all_efforts?per_page=$efforts&page=$page&athlete_id=$athlete_id");
116             } else {
117 0           $data = $self->auth->get_api("/segments/$self->{id}/all_efforts?per_page=$efforts&page=$page");
118             }
119            
120 0 0         if (! $raw) {
121 0           my $index = 0;
122 0           foreach my $effort (@{$data}) {
  0            
123 0           @{$data}[$index] = WebService::Strava::Athlete::Segment_Effort->new(id => $effort->{id}, auth => $self->auth, _build => 0);
  0            
124 0           $index++;
125             }
126             }
127              
128 0           return $data;
129             };
130              
131              
132 4 0   4   45076 method leaderboard(:$activities = 25, :$page = 1, :$gender?, :$age_group?, :$weight_class?, :$following?, :$club?, :$date_range?, ) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
133             # TODO: Handle pagination better use #4's solution when found.
134 0           my $url = "/segments/$self->{id}/leaderboard?per_page=$activities&page=$page";
135 0 0         $url .= "&age_group=$age_group" if $age_group;
136 0 0         $url .= "&gender=$gender" if $gender;
137 0 0         $url .= "&weight_class=$weight_class" if $weight_class;
138 0 0         $url .= "&following=$following" if $following;
139 0 0         $url .= "&club=$club" if $club;
140 0 0         $url .= "&date_range=$date_range" if $date_range;
141 0           return $self->auth->get_api("$url")->{entries};
142             }
143              
144             1;
145              
146             __END__
147              
148             =pod
149              
150             =encoding UTF-8
151              
152             =head1 NAME
153              
154             WebService::Strava::Segment - A Strava Segment Object
155              
156             =head1 VERSION
157              
158             version 0.05
159              
160             =head1 SYNOPSIS
161              
162             my $segment = WebService::Strava::Segment->new( auth => $auth, id => '229781' );
163              
164             =head1 DESCRIPTION
165              
166             Upon instantiation will retrieve the segment matching the id.
167             Requires a pre-authenticated WebService::Strava::Auth object.
168              
169             =head1 METHODS
170              
171             =head2 retrieve()
172              
173             $segment->retrieve();
174              
175             When a Segment object is lazy loaded, you can call retrieve it by calling
176             this method.
177              
178             =head2 list_efforts()
179              
180             $segment->list_efforts([athlete_id => 123456], [page => 2], [efforts => 100], [raw => 1])'
181              
182             Returns the Segment efforts for a particular segment. Takes 4 optional
183             parameters of 'athlete_id', 'page', 'efforts' and 'raw'. Raw will return the
184             an array segment_effort data instead of L<WebService::Strava::Athlete::Segment_Effort>
185             objects.
186              
187             * 'athelete_id' will return the segment efforts (if any) for the athelete
188             in question.
189              
190             The results are paginated and a maximum of 200 results can be returned
191             per page.
192              
193             =head2 leaderboard
194              
195             $segment->leaderboard(
196             [page => 2],
197             [activities => 100],
198             [gender => M|F],
199             [following => 1|0],
200             [clubid => 123456],
201             [date_range => 'this_year'|'this_month'|'this_week'|'today'],
202             [age_group => '0_24'|'25_34'|'35_44'|'45_54'|'55_64'|'65_plus'],
203             [weight_class => |'0_124'|'125_149'|'150_164'|'165_179'|'180_199'|'200_plus'|'0_54'|'55_64'|'65_74'|'75_84'|'85_94'|'95_plus']);
204              
205             Returns the leaderboard for the current segment. Takes a number of optional parameters
206             including 'page' and 'activities' (per page). For more information regarding the leaderboard
207             information visit the api documentation L<http://strava.github.io/api/v3/segments/#leaderboard>
208              
209             The results are paginated and a maximum of 200 results can be returned
210             per page.
211              
212             =head1 AUTHOR
213              
214             Leon Wright < techman@cpan.org >
215              
216             =head1 COPYRIGHT AND LICENSE
217              
218             This software is copyright (c) 2014 by Leon Wright.
219              
220             This is free software; you can redistribute it and/or modify it under
221             the same terms as the Perl 5 programming language system itself.
222              
223             =cut