MelbourneBikeShare.pm

filter: Statementstmt 20.4% 17 / 83 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 20.0% 5 / 25 Podpod 100% 11 / 11 totaltotal 22.6% 33 / 146 CC 12 SCAR 20.9
CC
12
Cov
16%
__grep
30.0
__cache_is_valid
12.0
by_distance
12.0
CRAP 8.1
↑ Summary MelbourneBikeShare/Terminal.pm »
1 package WWW::MelbourneBikeShare;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use WWW::MelbourneBikeShare::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = 0.01;
11  
12 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
13 0 my ( $class, %args ) = @_;
14  
15 0 my $self = bless {}, $class;
16  
17 0 $self->__init( %args );
18  
19 0 return $self
20 }
21  
22 0 ▶sub __init {
Subroutine __init: not called CC=2 SCAR=17.9
23 0 my ( $self, %args ) = @_;
24  
25 0 $self->{ __ua } = LWP::UserAgent->new();
26 0 $self->{ __uri } = 'https://data.melbourne.vic.gov.au/resource/qnjw-wgaj.json';
27 0 ▶ $self->{ __cache }->{ timeout } = $args{ cache } ||= 900;
Condition0%0/2
$args{"cache"} ||= 900
l!l
00
MC/DC0%0/1
$args{"cache"} ||= 900
$args{"cache"}
Truth table0%0/2
A
A= $args{"cache"}
Aresult
11
00
28 0 $self->{ __cache }->{ present } = 0;
29 0 $self->{ __cache }->{ last_update } = 0;
30  
31 0 $self->__get_service_data;
32 }
33  
34 0 ▶sub __cache_is_valid {
Subroutine __cache_is_valid: not called CC=3 SCAR=24.8
35 0 my $self = shift;
36  
37 0 ▶ $self->{ __cache }->{ present } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"__cache"}{"present"} or return 0 0 0 0
38  
39 ( ( time - $self->{ __cache }->{ last_update } ) < $self->{ __cache }->{ timeout } )
40 0 ▶ or return 0
Branch: 1 branch
BranchTrueFalseTotal
time - $self->{"__cache"}{"last_update"} < $self->{"__cache"}{"timeout"} or return 0 0 0 0
41 }
42  
43 0 ▶sub __get_service_data {
Subroutine __get_service_data: not called CC=2 SCAR=17.9
44 0 my $self = shift;
45  
46 0 ▶ return if $self->__cache_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->__cache_is_valid 0 0 0
47  
48 0 $self->__process_service_data(
49 $self->__get(
50 $self->__get_service_uri
51 )
52 );
53 }
54  
55 0 ▶sub __process_service_data {
Subroutine __process_service_data: not called CC=1 SCAR=6.9
56 0 my ( $self, $d ) = @_;
57  
58 0 @{ $self->{ __cache }->{ data } } =
59 0 map { WWW::MelbourneBikeShare::Terminal->new( $_ ) } @{ $d };
60  
61 0 $self->{ __cache }->{ present } = 1;
62 0 $self->{ __cache }->{ last_update } = time;
63 }
64  
65 0 ▶sub __get {
Subroutine __get: not called CC=2 SCAR=17.9
66 0 my ( $self, $url ) = @_;
67  
68 0 my $r = $self->{ __ua }->get( $url );
69  
70 0 ▶ if ( $r->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_success) { } 0 0 0
71 0 $self->{ error } = '';
72 0 return from_json( $r->content );
73 }
74 else {
75 0 $self->{ error } = "Unable to retrieve $url: "
76 . $r->status_line . "\n";
77 0 return 0
78 }
79 }
80  
81 0 ▶sub __get_service_uri {
Subroutine __get_service_uri: not called CC=1 SCAR=6.9
82 0 my $self = shift;
83  
84 0 return 'https://data.melbourne.vic.gov.au/resource/qnjw-wgaj.json';
85 }
86  
87 0 ▶sub __data {
Subroutine __data: not called CC=1 SCAR=6.9
88 0 my $self = shift;
89  
90 0 $self->__get_service_data;
91  
92 0 return @{ $self->{ __cache }->{ data } }
93 }
94  
95 0 ▶sub __sort_by {
Subroutine __sort_by: not called CC=1 SCAR=6.9
96 0 my ( $self, $attr, @args ) = @_;
97  
98 0 my @t = map { $_->[0] }
99 0 sort { $a->[1] <=> $b->[1] }
100 0 map { [ $_, $_->$attr( @args ) ] }
101 $self->terminals;
102  
103 return @t
104 0 }
105  
106 0 ▶sub __grep {
Subroutine __grep: not called CC=5 SCAR=34.0
107 0 my ( $self, $var, $val ) = @_;
108  
109 0 ▶ $val and $var or return;
Branch: 1 branch
BranchTrueFalseTotal
$val and $var or return 0 0 0
Condition0%0/3
$val and $var
!ll&&!rl&&r
000
MC/DC0%0/2
$val and $var
$val $var
Truth table0%0/3
A and B
A= $valB= $var
ABresult
0X0
100
111
110  
111 0 for ( $self->terminals ) {
112 0 ▶ return $_ if $_->$var eq $val
Branch: 1 branch
BranchTrueFalseTotal
if $_->$var eq $val 0 0 0
113 }
114 }
115  
116 0 ▶sub refresh {
Subroutine refresh: not called CC=1 SCAR=6.9
Pod documented
117 0 $_->{ cache }->{ present } = 0
118 }
119  
120 0 ▶sub terminals {
Subroutine terminals: not called CC=1 SCAR=6.9
Pod documented
121 0 return $_[0]->__data
122 }
123  
124 0 ▶sub id {
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
125 0 my ( $self, $id ) = @_;
126  
127 0 return $self->__grep( 'id', $id )
128 }
129  
130 0 ▶sub terminal {
Subroutine terminal: not called CC=1 SCAR=6.9
Pod documented
131 0 my ( $self, $terminal ) = @_;
132  
133 0 return $self->__grep( 'terminal', $terminal )
134 }
135  
136 0 ▶sub by_distance {
Subroutine by_distance: not called CC=3 SCAR=24.8
Pod documented
137 0 my ( $self, $lat, $lon ) = @_;
138  
139 0 ▶ return unless $lat and $lon;
Branch: 1 branch
BranchTrueFalseTotal
unless $lat and $lon 0 0 0
Condition0%0/3
$lat and $lon
!ll&&!rl&&r
000
MC/DC0%0/2
$lat and $lon
$lat $lon
Truth table0%0/3
A and B
A= $latB= $lon
ABresult
0X0
100
111
140  
141 0 return $self->__sort_by( 'distance', $lat, $lon );
142 }
143  
144 0 ▶sub by_id {
Subroutine by_id: not called CC=1 SCAR=6.9
Pod documented
145 0 my $self = shift;
146  
147 0 return $self->__sort_by( 'id' )
148 }
149  
150 0 ▶sub by_terminal {
Subroutine by_terminal: not called CC=1 SCAR=6.9
Pod documented
151 0 my $self = shift;
152  
153 0 return $self->__sort_by( 'terminal' )
154 }
155  
156 0 ▶sub by_available_bikes {
Subroutine by_available_bikes: not called CC=1 SCAR=6.9
Pod documented
157 0 my $self = shift;
158  
159 0 return reverse $self->__sort_by( 'bikes' )
160 }
161  
162 0 ▶sub by_available_docks {
Subroutine by_available_docks: not called CC=1 SCAR=6.9
Pod documented
163 0 my $self = shift;
164  
165 0 return reverse $self->__sort_by( 'empty' )
166 }
167  
168 0 ▶sub closest {
Subroutine closest: not called CC=1 SCAR=6.9
Pod documented
169 0 my ( $self, $lat, $lon ) = @_;
170  
171 0 return ( $self->by_distance( $lat, $lon ) )[0]
172 }
173  
174 2 1;
175  
176 __END__
↑ Summary MelbourneBikeShare/Terminal.pm »