| 1 | package WebService::Strava::Segment; | ||||||||||||||||||||||||||
| 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 Segment Object | ||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||
| 17 | 4 | our $VERSION = '0.06'; # VERSION: Generated by DZP::OurPkg:Version | |||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||
| 20 | # Validation functions | ||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $Num = sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||
| 23 | 0 | ▶ | croak "$_[0] isn't a number" unless looks_like_number $_[0]; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 24 | 4 | }; | |||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $Ref = sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||
| 27 | 0 | ▶ | croak "auth isn't a 'WebService::Strava::Auth' object!" unless reftype( $_[0] )->class eq "WebService::Strava::Auth"; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 28 | 4 | }; | |||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $Bool = sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||
| 31 | 0 | ▶ | croak "$_[0] must be 0|1" unless $_[0] =~ /^[01]$/; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 32 | 4 | }; | |||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||
| 34 | # Debugging hooks in case things go weird. (Thanks @pjf) | ||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||
| 37 | 0 | my $orig = shift; | |||||||||||||||||||||||||
| 38 | 0 | my $class = shift; | |||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($WebService::Strava::DEBUG) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 41 | 0 | warn "Building task with:\n"; | |||||||||||||||||||||||||
| 42 | 0 | warn Dumper(\@_), "\n"; | |||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||
| 45 | 0 | return $class->$orig(@_); | |||||||||||||||||||||||||
| 46 | 4 | }; | |||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||
| 48 | # Authentication Object | ||||||||||||||||||||||||||
| 49 | 4 | has 'auth' => ( is => 'ro', required => 1, isa => $Ref ); | |||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||
| 51 | # Defaults + Required | ||||||||||||||||||||||||||
| 52 | 4 | has 'id' => ( is => 'ro', required => 1, isa => $Num ); | |||||||||||||||||||||||||
| 53 | 0 | ▶ | has '_build' => ( is => 'ro', default => sub { 1 }, isa => $Bool ); | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||
| 55 | # Segment API | ||||||||||||||||||||||||||
| 56 | 4 | has 'name' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 57 | 4 | has 'activity_type' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 58 | 4 | has 'distance' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 59 | 4 | has 'average_grade' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 60 | 4 | has 'maximum_grade' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 61 | 4 | has 'elevation_high' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 62 | 4 | has 'elevation_low' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 63 | 4 | has 'start_latlng' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 64 | 4 | has 'end_latlng' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 65 | 4 | has 'climb_category' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 66 | 4 | has 'city' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 67 | 4 | has 'state' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 68 | 4 | has 'country' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 69 | 4 | has 'private' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 70 | 4 | has 'starred' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 71 | 4 | has 'map' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 72 | 4 | has 'athlete_count' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 73 | 4 | has 'resource_state' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 74 | 4 | has 'effort_count' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 75 | 4 | has 'total_elevation_gain' => ( is => 'ro', lazy => 1, builder => '_build_segment' ); | |||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($self->{_build}) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 81 | 0 | $self->_build_segment(); | |||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||
| 83 | 0 | return; | |||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | method _build_segment() { | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
_build_segment: not called CC=3 SCAR=21.8
Branch: 1 branch
| |||||||||||||||||||||||||||
| 87 | 0 | my $segment = $self->auth->get_api("/segments/$self->{id}"); | |||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||
| 89 | 0 | foreach my $key (keys %{ $segment }) { | |||||||||||||||||||||||||
| 90 | 0 | given ( $key ) { | |||||||||||||||||||||||||
| 91 | 0 | when ("athlete") { $self->_instantiate("Athlete", $key, $segment->{$key}); } | |||||||||||||||||||||||||
| 92 | 0 | default { $self->{$key} = $segment->{$key}; } | |||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||
| 96 | 0 | return; | |||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||
| 99 | 4 | ▶ | use WebService::Strava::Athlete; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | method _instantiate($type, $key, $data) { | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=5.3
Subroutine
_instantiate: not called CC=5 SCAR=31.5
Branch: 4 branches
| |||||||||||||||||||||||||||
| 102 | 0 | $self->{$key} = "WebService::Strava::$type"->new(auth => $self->auth, id => $data->{id}, _build => 0); | |||||||||||||||||||||||||
| 103 | 0 | return; | |||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | method retrieve() { | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
retrieve: not called CC=2 SCAR=12.4
Branch: 1 branch
| |||||||||||||||||||||||||||
| 108 | 0 | $self->_build_segment(); | |||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | method list_efforts(:$efforts = 25,:$page = 1,:$athlete_id, :$raw = 0) { | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=5.7
Subroutine
list_efforts: not called CC=9 SCAR=43.6
Branch: 5 branches
| |||||||||||||||||||||||||||
| 113 | # TODO: Handle pagination better #4 | ||||||||||||||||||||||||||
| 114 | 0 | my $data; | |||||||||||||||||||||||||
| 115 | 0 | ▶ | if ($athlete_id) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 116 | 0 | $data = $self->auth->get_api("/segments/$self->{id}/all_efforts?per_page=$efforts&page=$page&athlete_id=$athlete_id"); | |||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||
| 118 | 0 | $data = $self->auth->get_api("/segments/$self->{id}/all_efforts?per_page=$efforts&page=$page"); | |||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (! $raw) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 122 | 0 | my $index = 0; | |||||||||||||||||||||||||
| 123 | 0 | foreach my $effort (@{$data}) { | |||||||||||||||||||||||||
| 124 | 0 | @{$data}[$index] = WebService::Strava::Athlete::Segment_Effort->new(id => $effort->{id}, auth => $self->auth, _build => 0); | |||||||||||||||||||||||||
| 125 | 0 | $index++; | |||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||
| 129 | 0 | return $data; | |||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | method leaderboard(:$activities = 25, :$page = 1, :$gender?, :$age_group?, :$weight_class?, :$following?, :$club?, :$date_range?, ) { | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=6.0
Subroutine
leaderboard: not called CC=11 SCAR=47.7
Branch: 4 branches
| |||||||||||||||||||||||||||
| 134 | # TODO: Handle pagination better use #4's solution when found. | ||||||||||||||||||||||||||
| 135 | 0 | my $url = "/segments/$self->{id}/leaderboard?per_page=$activities&page=$page"; | |||||||||||||||||||||||||
| 136 | 0 | ▶ | $url .= "&age_group=$age_group" if $age_group; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 137 | 0 | ▶ | $url .= "&gender=$gender" if $gender; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 138 | 0 | ▶ | $url .= "&weight_class=$weight_class" if $weight_class; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 139 | 0 | ▶ | $url .= "&following=$following" if $following; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 140 | 0 | ▶ | $url .= "&club=$club" if $club; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 141 | 0 | ▶ | $url .= "&date_range=$date_range" if $date_range; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 142 | 0 | return $self->auth->get_api("$url")->{entries}; | |||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||
| 145 | 4 | 1; | |||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||