lib/WebService/SmartRow.pm

filter: Statementstmt 49.2% 31 / 63 Branchbran 0.0% 0 / 8 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 69.2% 9 / 13 Podpod 100% 4 / 4 totaltotal 47.5% 48 / 101 CC 9 SCAR 18.5
CC
9
Cov
44%
get_leaderboard
20.0
get_challenges
6.0
get_profile
6.0
CRAP 6.3
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use v5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package WebService::SmartRow;
7 3 $WebService::SmartRow::VERSION = '0.008';
8 # ABSTRACT: Connect and get data from SmartRow API
9  
10 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 has username => ( is => 'ro', required => 0 );
17 3 has password => ( is => 'ro', required => 0 );
18  
19 has http => (
20 is => 'ro',
21 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 3 return HTTP::Tiny->new();
23 },
24 3 );
25  
26 # https://smartrow.fit/api/challenge
27 0 ▶sub get_challenges {
Subroutine get_challenges: not called CC=2 SCAR=17.9
Pod documented
28 0 my $self = shift;
29  
30 0 my ( $user, $pass ) = $self->_credentials_via_env;
31  
32 0 my $response = $self->http->request( 'GET',
33 'https://'
34 . $user . ':'
35 . $pass . '@'
36 . 'smartrow.fit/api/challenge' );
37  
38 0 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
39 0 return 'Response error';
40 }
41  
42 0 my $json = decode_json $response->{content};
43  
44 0 return $json;
45 }
46  
47 # https://smartrow.fit/api/account
48 0 ▶sub get_profile {
Subroutine get_profile: not called CC=2 SCAR=17.9
Pod documented
49 0 my $self = shift;
50  
51 0 my ( $user, $pass ) = $self->_credentials_via_env;
52  
53 0 my $response = $self->http->request( 'GET',
54 'https://' . $user . ':' . $pass . '@' . 'smartrow.fit/api/account' );
55  
56 0 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
57 0 return 'Response error';
58 }
59  
60 0 my $json = decode_json $response->{content};
61  
62 0 return $json->[0];
63 }
64  
65 # https://smartrow.fit/api/public-game
66 0 ▶sub get_workouts {
Subroutine get_workouts: not called CC=2 SCAR=17.9
Pod documented
67 0 my $self = shift;
68  
69 0 my ( $user, $pass ) = $self->_credentials_via_env;
70  
71 0 my $response = $self->http->request( 'GET',
72 'https://'
73 . $user . ':'
74 . $pass . '@'
75 . 'smartrow.fit/api/public-game' );
76  
77 0 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
78 0 return 'Response error';
79 }
80  
81 0 my $json = decode_json $response->{content};
82  
83 0 return $json;
84 }
85  
86 0 ▶sub get_leaderboard {
Subroutine get_leaderboard: not called CC=4 SCAR=30.0
Pod documented
87 0 my ( $self, %args ) = @_;
88  
89 0 ▶ $args{distance} //= 2000;
Condition0%0/2
$args{"distance"} //= 2000
l!l
00
MC/DC0%0/1
$args{"distance"} //= 2000
$args{"distance"}
Truth table0%0/2
A
A= $args{"distance"}
Aresult
11
00
90  
91 0 my $params_string = '';
92 0 for my $key ( keys %args ) {
93 0 $params_string .= sprintf( "%s=%s&", $key, $args{$key} );
94 }
95  
96 0 my ( $user, $pass ) = $self->_credentials_via_env;
97  
98 0 my $response = $self->http->request( 'GET',
99 'https://'
100 . $user . ':'
101 . $pass . '@'
102 . 'smartrow.fit/api/leaderboard?'
103 . $params_string );
104  
105 0 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
106 0 return 'Response error';
107 }
108  
109 0 my $json = decode_json $response->{content};
110  
111 0 return $json->[0];
112 }
113  
114 2 ▶sub _credentials_via_env {
Subroutine _credentials_via_env: called CC=3 SCAR=11.2
115 2 my $self = shift;
116  
117 2 ▶ my $user = $self->username || $ENV{SMARTROW_USERNAME};
Condition66%2/3
$self->username || $ENV{'SMARTROW_USERNAME'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->username || $ENV{'SMARTROW_USERNAME'}
$self->username $ENV{'SMARTROW_USERNAME'}
Truth table66%2/3
A || B
A= $self->usernameB= $ENV{'SMARTROW_USERNAME'}
ABresult
1X1
011
000
118 # Escape the "@" as perl basic auth requirement
119 2 $user =~ s/@/%40/g;
120  
121 2 ▶ my $pass = $self->password || $ENV{SMARTROW_PASSWORD};
Condition66%2/3
$self->password || $ENV{'SMARTROW_PASSWORD'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->password || $ENV{'SMARTROW_PASSWORD'}
$self->password $ENV{'SMARTROW_PASSWORD'}
Truth table66%2/3
A || B
A= $self->passwordB= $ENV{'SMARTROW_PASSWORD'}
ABresult
1X1
011
000
122  
123 2 return ( $user, $pass ),;
124 }
125  
126  
127 3 1;
128  
129 __END__
↑ Summary