File Coverage

blib/lib/WebService/Strava.pm
Criterion Covered Total %
statement 51 186 27.4
branch 0 62 0.0
condition n/a
subroutine 25 37 67.5
pod n/a
total 76 285 26.6


line stmt bran cond sub pod time code
1             package WebService::Strava;
2 4     4   54454 use v5.010;
  4         12  
  4         144  
3 4     4   17 use strict;
  4         5  
  4         102  
4 4     4   14 use warnings;
  4         5  
  4         92  
5 4     4   1950 use autodie;
  4         63420  
  4         19  
6 4     4   95075 use Moo;
  4         55220  
  4         25  
7 4     4   10355 use Method::Signatures;
  4         217093  
  4         23  
8 4     4   6533 use Data::Dumper;
  4         29168  
  4         267  
9 4     4   27 use Carp qw(croak);
  4         5  
  4         280  
10              
11             our $DEBUG = $ENV{STRAVA_DEBUG} || 0;
12              
13             # ABSTRACT: Access Strava via version 3 of the API
14              
15             our $VERSION = '0.05'; # VERSION: Generated by DZP::OurPkg:Version
16              
17              
18 4     4   1750 use WebService::Strava::Auth;
  4         12  
  4         311  
19              
20             has 'auth' => (
21             is => 'ro',
22             isa => sub { "WebService::Strava::Auth" },
23             lazy => 1,
24             builder => 1,
25             handles => [ qw( get post ) ],
26             );
27              
28 4 0   4   2715 method _build_auth() {
  0     0      
  0            
29 0           return WebService::Strava::Auth->new();
30             }
31              
32              
33 4     4   2402 use WebService::Strava::Athlete;
  4         12  
  4         145  
34              
35 4 0   4   11780 method athlete($id?) {
  0     0      
  0            
  0            
36 0           return WebService::Strava::Athlete->new(id =>$id, auth => $self->auth);
37             }
38              
39              
40 4 0   4   14439 method clubs($build = 0) {
  0 0   0      
  0            
  0            
41 0           my $data = $self->auth->get_api("/athlete/clubs");
42 0           my $index = 0;
43 0           foreach my $club (@{$data}) {
  0            
44 0           @{$data}[$index] = WebService::Strava::Club->new(id => $club->{id}, auth => $self->auth, _build => $build);
  0            
45 0           $index++;
46             }
47 0           return $data;
48             }
49              
50              
51 4     4   817 use WebService::Strava::Segment;
  4         7  
  4         81  
52              
53 4 0   4   7571 method segment($id) {
  0 0   0      
  0            
  0            
  0            
54 0           return WebService::Strava::Segment->new(id =>$id, auth => $self->auth);
55             }
56              
57              
58 4 0   4   25278 method list_starred_segments(:$activities = 25, :$page = 1) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
59             # TODO: Handle pagination better use #4's solution when found.
60 0           my $data = $self->auth->get_api("/segments/starred?per_page=$activities&page=$page");
61 0           my $index = 0;
62 0           foreach my $segment (@{$data}) {
  0            
63 0           @{$data}[$index] = WebService::Strava::Segment->new(id => $segment->{id}, auth => $self->auth, _build => 0);
  0            
64 0           $index++;
65             }
66 0           return $data;
67             }
68              
69              
70 4     4   1191 use WebService::Strava::Athlete::Segment_Effort;
  4         6  
  4         84  
71              
72 4 0   4   8300 method effort($id) {
  0 0   0      
  0            
  0            
  0            
73 0           return WebService::Strava::Athlete::Segment_Effort->new(id =>$id, auth => $self->auth);
74             }
75              
76              
77 4     4   3126 use WebService::Strava::Athlete::Activity;
  4         10  
  4         162  
78              
79 4 0   4   9872 method activity($id) {
  0 0   0      
  0            
  0            
  0            
80 0           return WebService::Strava::Athlete::Activity->new(id =>$id, auth => $self->auth);
81             }
82              
83              
84 4 0   4   28600 method list_activities(:$activities = 25, :$page = 1, :$before?, :$after?) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
85             # TODO: Handle pagination better use #4's solution when found.
86 0           my $url = "/athlete/activities?per_page=$activities&page=$page";
87 0 0         $url .= "&before=$before" if $before;
88 0 0         $url .= "&after=$after" if $after;
89 0           my $data = $self->auth->get_api("$url");
90 0           my $index = 0;
91 0           foreach my $activity (@{$data}) {
  0            
92 0           @{$data}[$index] = WebService::Strava::Athlete::Activity->new(id => $activity->{id}, auth => $self->auth, _build => 0);
  0            
93 0           $index++;
94             }
95 0           return $data;
96             }
97              
98              
99 4 0   4   22458 method list_friends_activities(:$activities = 25, :$page = 1) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
  0            
100             # TODO: Handle pagination better use #4's solution when found.
101 0           my $data = $self->auth->get_api("/activities/following?per_page=$activities&page=$page");
102 0           my $index = 0;
103 0           foreach my $activity (@{$data}) {
  0            
104 0           @{$data}[$index] = WebService::Strava::Athlete::Activity->new(id => $activity->{id}, auth => $self->auth, _build => 0);
  0            
105 0           $index++;
106             }
107 0           return $data;
108             }
109              
110              
111 4     4   44497 method upload_activity(
  0     0      
  0            
  0            
112 0           :$file,
  0            
  0            
113 0 0         :$type = 'gpx',
  0            
  0            
114 0           :$activity_type?,
  0            
  0            
115 0           :$name?,
  0            
  0            
116 0           :$description?,
  0            
  0            
117 0           :$private?,
  0            
  0            
118 0           :$trainer?,
  0            
  0            
119 0 0         :$external_id?,
  0 0          
  0            
  0            
  0            
120             ) {
121 0           my $data = $self->auth->uploads_api(
122             file => $file,
123             type => $type,
124             activity_type => $activity_type,
125             name => $name,
126             description => $description,
127             private => $private,
128             trainer => $trainer,
129             external_id => $external_id,
130             );
131 0           return $data;
132             }
133              
134              
135 4 0   4   9283 method upload_status(:$id) {
  0 0   0      
  0            
  0            
  0            
  0            
  0            
136 0           my $data = $self->auth->get_api("/uploads/$id");
137 0           return $data;
138             }
139              
140              
141 4 0   4   7630 method delete_activity(:$id) {
  0 0   0      
  0            
  0            
  0            
  0            
  0            
142 0           my $data = $self->auth->delete_api("/activities/$id");
143 0           return $data;
144             }
145              
146              
147             1;
148              
149             __END__
150              
151             =pod
152              
153             =encoding UTF-8
154              
155             =head1 NAME
156              
157             WebService::Strava - Access Strava via version 3 of the API
158              
159             =head1 VERSION
160              
161             version 0.05
162              
163             =head1 SYNOPSIS
164              
165             use WebService::Strava;
166              
167             my $strava = WebService::Strava->new();
168              
169             =head1 DESCRIPTION
170              
171             Provides an abstraction layer to version 3 of the Strava API. L<http://strava.github.io/api/v3/>.
172              
173             Attempts to provide a few logical shortcut methods and provide simple OAuth2 abstraction to take
174             the hassle out of accessing it in a scripted manner.
175              
176             You can use the cli client to provide an easy setup after configuring api access in you strava profile
177             L<https://www.strava.com/settings/api>
178              
179             strava --setup
180              
181             Which can also be called within your script via
182              
183             $strava->auth->setup();
184              
185             =head1 METHODS
186              
187             =head2 athlete
188              
189             $strava->athlete([$id]);
190              
191             Takes an optional id and will retrieve a L<WebService::Strava::Athlete>
192             with details Athlete retrieved. Currently authenticated user will be
193             returned unless an ID is provided.
194              
195             =head2 clubs
196              
197             $strava->clubs([1]);
198              
199             Returns an arrayRef of L<WebService::Strava::Club> for the currently
200             authenticated user. Takes an optional 1 or 0 (default 0) that will retrieve
201             all club details.
202              
203             After instantiation it is possible to retrieve members associated with the club.
204              
205             my $club = @{$strava->clubs()}[0];
206             $club->list_members([page => 2], [activities => 100]);
207              
208             Returns an arrayRef athletes for the Club. Takes 2 optional
209             parameters of 'page' and 'members' (per page).
210              
211             The results are paginated and a maximum of 200 results can be returned
212             per page.
213              
214             =head2 segment
215              
216             $strava->segment($id);
217              
218             Takes an mandatory id and will retrieve a
219             L<WebService::Strava::Segment> with details about the Segment ID retrieved.
220              
221             After instantiation it is possible to retrieve efforts listed for that segment. It
222             takes 3 optional named parameters of 'athlete_id', 'page' and 'efforts'.
223              
224             $segment->list_efforts([athlete_id => 123456], [page => 2], [efforts => 100], [raw => 1])'
225              
226             Returns the Segment efforts for a particular segment. Takes 4 optional
227             parameters of 'athlete_id', 'page', 'efforts' and 'raw'. Raw will return the
228             an array segment_effort data instead of L<WebService::Strava::Athlete::Segment_Effort>
229             objects.
230              
231             * 'athelete_id' will return the segment efforts (if any) for the athelete
232             in question.
233              
234             The results are paginated and a maximum of 200 results can be returned
235             per page.
236              
237             =head2 list_starred_segments
238              
239             $segment->list_starred_segments([page => 2], [activities => 100])
240              
241             Returns an arrayRef of starred L<WebService::Strava::Segment> objects for the current authenticated user. Takes 2 optional
242             parameters of 'page' and 'activities' (per page).
243              
244             The results are paginated and a maximum of 200 results can be returned
245             per page.
246              
247             =head2 effort
248              
249             $strava->effort($id);
250              
251             Takes an mandatory id and will retrieve a
252             L<WebService::Strava::Athlete::Segment_Effort> with details about the Segment Effort ID retrieved.
253              
254             =head2 activity
255              
256             $strava->activity($id);
257              
258             Takes an mandatory id and will retrieve a
259             L<WebService::Strava::Athlete::Activity> with details about the Activity ID retrieved.
260              
261             =head2 list_activities
262              
263             $athlete->list_activities([page => 2], [activities => 100], [before => 1407665853], [after => 1407665853]);
264              
265             Returns an arrayRef of L<WebService::Strava::Athlete::Activity> objects
266             for the current authenticated user. Takes 4 optional parameters of 'page',
267             'activities' (per page), 'before' (activities before unix epoch),
268             and 'after' (activities after unix epoch).
269              
270             The results are paginated and a maximum of 200 results can be returned
271             per page.
272              
273             =head2 list_friends_activities
274              
275             $athlete->list_activities([page => 2], [activities => 100])
276              
277             Returns an arrayRef activities for friends of the current authenticated user. Takes 2 optional
278             parameters of 'page' and 'activities' (per page).
279              
280             The results are paginated and a maximum of 200 results can be returned
281             per page.
282              
283             =head2 upload_activity
284              
285             $strava->upload_activity(
286             file => '/path/to/sample.gpx',
287             type => 'gpx'
288             );
289              
290             Uploads an activity to Strava. Returns an upload status hash. Takes
291             the following named arguments:
292              
293             =over
294              
295             =item file
296              
297             Expected to be a path to the file being uploaded.
298              
299             =item type
300              
301             The Strava api accepts following file types: fit, fit.gz, tcx,
302             tcx.gz, gpx and gpx.gz. There is no current logic to detect what
303             sort is being uploaded (patches welcome), so you will need to set
304             it which ever file your uploading. ie 'gpx' for a GPX file.
305              
306             =item activity_type
307              
308             Optional, case insensitive string of following types (list may be
309             out of date check L<http://strava.github.io/api/v3/uploads/#post-file>
310             for up to date info): ride, run, swim, workout, hike, walk,
311             nordicski, alpineski, backcountryski, iceskate, inlineskate, kitesurf,
312             rollerski, windsurf, workout, snowboard, snowshoe. Type detected from
313             file overrides, uses athlete’s default type if not specified.
314              
315             =item name
316              
317             Optional string, if not provided, will be populated using start date
318             and location, if available.
319              
320             =item description
321              
322             Optional. Left blank if not provided.
323              
324             =item private
325              
326             Sets the Activity to Private.
327              
328             =item trainer
329              
330             Optional integer, activities without lat/lng info in the file are
331             auto marked as stationary, set to 1 to force.
332              
333             =item external_id
334              
335             Optional string, data filename will be used by default but should
336             be a unique identifier.
337              
338             =back
339              
340             =head2 upload_status
341              
342             $strava->upload_status(id => '12345678');
343              
344             Given an upload id (returned by uploading an activity) you can check
345             the status of the request. Takes between 5 and 10 seconds for an
346             to be processed so keep in mind there isn't any point in checking
347             more than once per second.
348              
349             =head2 delete_activity
350              
351             $strava->delete_activity(id => '12345678');
352              
353             Will delete a given activity. Returns true on success and false
354             upon failure
355              
356             =head1 ACKNOWLEDGEMENTS
357              
358             Fred Moyer <fred@redhotpenguin.com> - Giving me Co-Maint on WebService::Strava
359              
360             Paul Fenwick <pjf@cpan.org> - For being generally awesome, providing inspiration,
361             assistance and a lot of boiler plate for this library.
362              
363             =head1 BUGS/Feature Requests
364              
365             Please submit any bugs, feature requests to
366             L<https://github.com/techamn83/WebService-Strava3/issues> .
367              
368             Contributions are more than welcome! I am aware that Dist::Zilla comes with quite a dependency chain, so feel free to submit pull request with code + explanation of what you are trying to achieve and I will test and likely implement them.
369              
370             =head1 AUTHOR
371              
372             Leon Wright < techman@cpan.org >
373              
374             =head1 COPYRIGHT AND LICENSE
375              
376             This software is copyright (c) 2014 by Leon Wright.
377              
378             This is free software; you can redistribute it and/or modify it under
379             the same terms as the Perl 5 programming language system itself.
380              
381             =cut