Strava/Auth.pm

filter: Statementstmt 33.8% 68 / 201 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 65.7% 23 / 35 Podpod n/a totaltotal 28.7% 91 / 317 CC 40 SCAR 34.6
CC
40
Cov
24%
uploads_api
121.5
prompt
47.4
post_api
34.0
CRAP 31.7
« Strava/Segment.pm ↑ Summary Strava.pm »
1 package WebService::Strava::Auth;
2  
3 5 ▶use v5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use LWP::Authen::OAuth2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use JSON::Parse 'valid_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::MimeInfo::Magic;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Method::Signatures 20140224;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use experimental 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # ABSTRACT: A Strava Segment Object
20  
21 5 our $VERSION = '0.06'; # VERSION: Generated by DZP::OurPkg:Version
22  
23  
24 # Debugging hooks in case things go weird. (Thanks @pjf)
25  
26 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
27 0 my $orig = shift;
28 0 my $class = shift;
29
30 0 ▶ if ($WebService::Strava::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebService::Strava::DEBUG) 0 0 0
31 0 warn "Building task with:\n";
32 0 warn Dumper(\@_), "\n";
33 }
34
35 0 return $class->$orig(@_);
36 5 };
37  
38 0 ▶has 'api_base' => (is => 'ro', default => sub { 'https://www.strava.com/api/v3' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
39 0 ▶has 'config_file' => ( is => 'ro', default => sub { "$ENV{HOME}/.stravarc" } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
40 5 has 'config' => ( is => 'rw', lazy => 1, builder => 1 );
41 0 ▶has 'scope' => ( is => 'ro', default => sub { "view_private,write" } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 5 has 'auth' => ( is => 'rw', lazy => 1, builder => 1, handles => [ qw( get post ) ] );
43  
44 # TODO: Potentially allow the config to be passed through instead of loaded.
45 #has 'client_id' => ( is => 'ro' );
46 #has 'client_secret' => ( is => 'ro' );
47 #has 'token_string' => ( is => 'rw' );
48  
49  
50 0 ▶method setup() {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine setup: not called CC=4 SCAR=27.7
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
51 # Request Client details if non existent
52 0 ▶ if (! $self->config->{auth}{client_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->{"auth"}{"client_id"}) 0 0 0
53 0 $self->config->{auth}{client_id} = $self->prompt("Paste enter your client_id");
54 }
55
56 0 ▶ if (! $self->config->{auth}{client_secret} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->{"auth"}{"client_secret"}) 0 0 0
57 0 $self->config->{auth}{client_secret} = $self->prompt("Paste enter your client_secret");
58 }
59
60 # Build auth object - TODO: Write a strava authentication provider! Issue #1
61 my $oauth2 = LWP::Authen::OAuth2->new(
62 client_id => $self->{config}{auth}{client_id},
63 client_secret => $self->{config}{auth}{client_secret},
64 service_provider => "Strava",
65 redirect_uri => "http://127.0.0.1",
66 scope => $self->{scope},
67 0 );
68  
69 # Get authentican token string
70 0 say "Log into the Strava account and browse the following url";
71 0 my $url = $oauth2->authorization_url();
72 0 say $url;
73 0 my $code = $self->prompt("Paste code result here");
74 0 $oauth2->request_tokens(code => $code);
75 0 $self->config->{auth}{token_string} = $oauth2->token_string;
76 0 $self->config->write($self->{config_file});
77 }
78  
79 0 ▶method _build_config() {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _build_config: not called CC=5 SCAR=31.5
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
80 0 my $config;
81 0 ▶ if ( -e $self->{config_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->{'config_file'}) { } 0 0 0
82 0 $config = Config::Tiny->read( $self->{config_file} );
83 0 ▶ unless ($config->{auth}{client_id}
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"auth"}{"client_id"} and $config->{"auth"}{"client_secret"}) 0 0 0
Condition0%0/3
$config->{"auth"}{"client_id"} and $config->{"auth"}{"client_secret"}
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"auth"}{"client_id"} and $config->{"auth"}{"client_secret"}
$config->{"auth"}{"client_id"} $config->{"auth"}{"client_secret"}
Truth table0%0/3
A and B
A= $config->{"auth"}{"client_id"}B= $config->{"auth"}{"client_secret"}
ABresult
0X0
100
111
84 && $config->{auth}{client_secret}) {
85 0 die <<"END_DIE";
86 Cannot find user credentials in $self->{config_file}
87  
88 You'll need to have a $self->{config_file} file that looks like
89 the following:
90  
91 [auth]
92 client_id = xxxxx
93 client_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
94  
95 You can get these values by going to https://www.strava.com/settings/api
96  
97 Running 'strava --setup' or \$strava->auth->setup will run you through
98 setting up Oauth2.
99  
100 END_DIE
101 }
102 } else {
103 0 $config = Config::Tiny->new();
104 }
105 0 return $config;
106 }
107  
108 0 ▶method _build_auth() {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _build_auth: not called CC=3 SCAR=21.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
109 0 $self->config;
110 0 ▶ croak "Missing token, please re-run setup" unless $self->config->{auth}{token_string};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"auth"}{"token_string"} 0 0 0
111  
112 my $oauth2 = LWP::Authen::OAuth2->new(
113 client_id => $self->{config}{auth}{client_id},
114 client_secret => $self->{config}{auth}{client_secret},
115 service_provider => "Strava",
116 token_string => $self->config->{auth}{token_string},
117 0 );
118 0 return $oauth2;
119 }
120  
121  
122 0 ▶method get_api($api_path) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine get_api: not called CC=5 SCAR=31.6
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
123 0 my $response = $self->auth->get($self->{api_base}.$api_path);
124 0 my $json = $response->decoded_content;
125 0 ▶ if (! valid_json($json) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (valid_json($json)) 0 0 0
126 0 croak("Something went wrong, a JSON string wasn't returned");
127 }
128 0 ▶ if ($ENV{STRAVA_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"STRAVA_DEBUG"}) 0 0 0
129 0 say Dumper($json);
130 }
131 0 return decode_json($json);
132 }
133  
134  
135 0 ▶method delete_api($api_path) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine delete_api: not called CC=5 SCAR=31.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
136 0 my $response = $self->auth->delete($self->{api_base}.$api_path);
137 0 ▶ if ($ENV{STRAVA_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"STRAVA_DEBUG"}) 0 0 0
138 0 say Dumper($response);
139 }
140 0 ▶ return $response->code == 204 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$response->code == 204 ? : 0 0 0
141 }
142  
143  
144 0 ▶method post_api($api_path,$content) {
Subroutine BEGIN: called CC=1 SCAR=4.9
Subroutine post_api: not called CC=6 SCAR=35.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
if scalar @_ > 2 0 0 0
145 my $response = $self->auth->post(
146 0 $self->{api_base}.$api_path,
147 Content => encode_json($content),
148 );
149  
150 0 my $json = $response->decoded_content;
151 0 ▶ if (! valid_json($json) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (valid_json($json)) 0 0 0
152 0 croak("Something went wrong, a JSON string wasn't returned");
153 }
154 0 ▶ if ($ENV{STRAVA_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"STRAVA_DEBUG"}) 0 0 0
155 0 say Dumper($json);
156 }
157 0 return decode_json($json);
158 }
159  
160  
161 0 ▶method uploads_api(
Subroutine BEGIN: called CC=1 SCAR=2.0
Subroutine uploads_api: not called CC=11 SCAR=48.0
162 0 :$file,
163 0 :$type,
164 0 :$activity_type?,
165 0 :$name?,
166 0 :$description?,
167 0 :$private?,
168 0 :$trainer?,
169 0 ▶ :$external_id?,
Branch: 2 branches
BranchTrueFalseTotal
if keys %args 0 0 0
if scalar @_ > 16 0 0 0
170 ) {
171 0 my $filename = basename($file);
172 0 my $mimetype = mimetype($file);
173  
174 0 my $content = {
175 file => [
176 $file,
177 $filename,
178 Content_Type => $mimetype,
179 'Content-Transfer-Encoding' => 'binary',
180 ],
181 data_type => lc($type),
182 };
183  
184 0 ▶ $content->{activity_type} = lc($activity_type) if $activity_type;
Branch: 1 branch
BranchTrueFalseTotal
if $activity_type 0 0 0
185 0 ▶ $content->{name} = $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
186 0 ▶ $content->{description} = $description if $description;
Branch: 1 branch
BranchTrueFalseTotal
if $description 0 0 0
187 0 ▶ $content->{private} = $private if $private;
Branch: 1 branch
BranchTrueFalseTotal
if $private 0 0 0
188 0 ▶ $content->{trainer} = $trainer if $trainer;
Branch: 1 branch
BranchTrueFalseTotal
if $trainer 0 0 0
189 0 ▶ $content->{external_id} = $external_id if $external_id;
Branch: 1 branch
BranchTrueFalseTotal
if $external_id 0 0 0
190  
191 my $response = $self->auth->post(
192 0 $self->{api_base}.'/uploads',
193 Content_Type => 'multipart/form-data',
194 Content => $content,
195 );
196  
197 0 my $json = $response->decoded_content;
198 0 ▶ if (! valid_json($json) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (valid_json($json)) 0 0 0
199 0 croak("Something went wrong, a JSON string wasn't returned");
200 }
201 0 ▶ if ($ENV{STRAVA_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"STRAVA_DEBUG"}) 0 0 0
202 0 say Dumper($json);
203 }
204 0 return decode_json($json);
205 }
206  
207 0 ▶method prompt($question,:$default) { # inspired from here: http://alvinalexander.com/perl/edu/articles/pl010005
Subroutine BEGIN: called CC=1 SCAR=5.1
Subroutine prompt: not called CC=7 SCAR=38.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if keys %args 0 0 0
if scalar @_ > 3 0 0 0
208 0 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) { } 0 0 0
209 0 say $question, "[", $default, "]: ";
210 } else {
211 0 say $question, ": ";
212 0 $default = "";
213 }
214  
215 0 $| = 1; # flush
216 0 $_ = <STDIN>; # get input
217  
218 0 chomp;
219 0 ▶ if ("$default") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$default") { } 0 0 0
220 0 ▶ return $_ ? $_ : $default; # return $_ if it has a value
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
221 } else {
222 0 return $_;
223 }
224 }
225  
226 5 1;
227  
228 __END__
« Strava/Segment.pm ↑ Summary Strava.pm »