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   61680 use v5.010;
  4         15  
  4         174  
3 4     4   19 use strict;
  4         6  
  4         127  
4 4     4   17 use warnings;
  4         6  
  4         119  
5 4     4   2065 use autodie;
  4         76152  
  4         26  
6 4     4   25584 use Moo;
  4         71235  
  4         23  
7 4     4   12100 use Method::Signatures;
  4         293398  
  4         30  
8 4     4   5158 use Data::Dumper;
  4         36338  
  4         377  
9 4     4   37 use Carp qw(croak);
  4         6  
  4         389  
10              
11             our $DEBUG = $ENV{STRAVA_DEBUG} || 0;
12              
13             # ABSTRACT: Access Strava via version 3 of the API
14              
15             our $VERSION = '0.04'; # VERSION: Generated by DZP::OurPkg:Version
16              
17              
18 4     4   2383 use WebService::Strava::Auth;
  4         10  
  4         368  
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   3181 method _build_auth() {
  0     0      
  0            
29 0           return WebService::Strava::Auth->new();
30             }
31              
32              
33 4     4   2657 use WebService::Strava::Athlete;
  4         14  
  4         177  
34              
35 4 0   4   12017 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   14908 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   873 use WebService::Strava::Segment;
  4         6  
  4         83  
52              
53 4 0   4   9803 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   32776 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   1506 use WebService::Strava::Athlete::Segment_Effort;
  4         7  
  4         99  
71              
72 4 0   4   10749 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   3979 use WebService::Strava::Athlete::Activity;
  4         12  
  4         213  
78              
79 4 0   4   14210 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   34678 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   26843 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   63959 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   16029 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   11919 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.04
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             Expected to be a path to the file being uploaded.
297              
298             =item 'type'
299             The Strava api accepts following file types: fit, fit.gz, tcx,
300             tcx.gz, gpx and gpx.gz. There is no current logic to detect what
301             sort is being uploaded (patches welcome), so you will need to set
302             it which ever file your uploading. ie 'gpx' for a GPX file.
303              
304             =item 'activity_type'
305             Optional, case insensitive string of following types (list may be
306             out of date check L<http://strava.github.io/api/v3/uploads/#post-file>
307             for up to date info): ride, run, swim, workout, hike, walk,
308             nordicski, alpineski, backcountryski, iceskate, inlineskate, kitesurf,
309             rollerski, windsurf, workout, snowboard, snowshoe. Type detected from
310             file overrides, uses athlete’s default type if not specified.
311              
312             =item 'name'
313             Optional string, if not provided, will be populated using start date
314             and location, if available.
315              
316             =item 'description'
317             Optional. Left blank if not provided.
318              
319             =item 'private'
320             Sets the Activity to Private.
321              
322             =item 'trainer'
323             Optional integer, activities without lat/lng info in the file are
324             auto marked as stationary, set to 1 to force.
325              
326             =item 'external_id'
327             Optional string, data filename will be used by default but should
328             be a unique identifier.
329              
330             =back
331              
332             =head2 upload_status
333              
334             $strava->upload_status(id => '12345678');
335              
336             Given an upload id (returned by uploading an activity) you can check
337             the status of the request. Takes between 5 and 10 seconds for an
338             to be processed so keep in mind there isn't any point in checking
339             more than once per second.
340              
341             =head2 delete_activity
342              
343             $strava->delete_activity(id => '12345678');
344              
345             Will delete a given activity. Returns true on success and false
346             upon failure
347              
348             =head1 ACKNOWLEDGEMENTS
349              
350             Fred Moyer <fred@redhotpenguin.com> - Giving me Co-Maint on WebService::Strava
351              
352             Paul Fenwick <pjf@cpan.org> - For being generally awesome, providing inspiration,
353             assistance and a lot of boiler plate for this library.
354              
355             =head1 BUGS/Feature Requests
356              
357             Please submit any bugs, feature requests to
358             L<https://github.com/techamn83/WebService-Strava3/issues> .
359              
360             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.
361              
362             =head1 AUTHOR
363              
364             Leon Wright < techman@cpan.org >
365              
366             =head1 COPYRIGHT AND LICENSE
367              
368             This software is copyright (c) 2014 by Leon Wright.
369              
370             This is free software; you can redistribute it and/or modify it under
371             the same terms as the Perl 5 programming language system itself.
372              
373             =cut