blib/lib/Webservice/Rosary/API.pm

filter: Statementstmt 98.4% 123 / 125 Branchbran 78.1% 50 / 64 Conditioncond 54.5% 24 / 44 MC/DCmcdc 29.6% 8 / 27 Subroutinesub 100% 19 / 19 Podpod 100% 5 / 5 totaltotal 80.6% 229 / 284 CC 50 SCAR 19.7
CC
50
Cov
85%
mp3Link
14.1
day
9.9
_make_call
9.0
CRAP 7.1
« blib/lib/Webservice/Rosary/Stream.pm ↑ Summary blib/lib/Webservice/Rosary/Tradition.pm »
1 package Webservice::Rosary::API;
2  
3 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Util::H2O::More qw/baptise d2o/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use HTTP::Tiny qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = "0.1.10";
13  
14 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 BASEURL => "https://the-rosary-api.vercel.app/v1",
16 DAILYURL => "https://dailyrosary.catholicdevotion.org",
17 3 };
18  
19 12 ▶sub new {
Subroutine new: called CC=6 SCAR=18.2
Pod documented
20 12 my $pkg = shift;
21 12 my %params = @_;
22  
23 12 ▶ $params{timeout} = 10 if not exists $params{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"timeout"} 12 0 12
24 12 ▶ $params{cache_ttl} = 300 if not exists $params{cache_ttl};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"cache_ttl"} 1 11 12
25 12 ▶ $params{stale_if_error} = 1 if not exists $params{stale_if_error};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"stale_if_error"} 12 0 12
26 12 ▶ $params{cache} = {} if not exists $params{cache};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"cache"} 12 0 12
27 $params{ua} = _new_default_ua($params{timeout})
28 12 ▶ if not exists $params{ua};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"ua"} 1 11 12
29  
30 12 return baptise \%params, $pkg,
31 qw/ua timeout cache cache_ttl stale_if_error/;
32 }
33  
34  
35 1 ▶sub _new_default_ua {
Subroutine _new_default_ua: called CC=4 SCAR=14.9
36 1 my $timeout = shift;
37  
38 # HTTP::Tiny honors proxy environment variables. Some environments leave
39 # those variables defined but empty; older HTTP::Tiny releases treat that
40 # as an invalid proxy URL. Ignore only empty proxy values locally.
41 1 local %ENV = %ENV;
42 1 foreach my $name (qw/http_proxy https_proxy all_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY/) {
43 6 ▶ delete $ENV{$name} if exists $ENV{$name} and not length $ENV{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{$name} and not length $ENV{$name} 0 6 6
Condition33%1/3
exists $ENV{$name} and not length $ENV{$name}
!ll&&!rl&&r
600
MC/DC0%0/2
exists $ENV{$name} and not length $ENV{$name}
exists $ENV{$name} not length $ENV{$name}
Truth table33%1/3
A and not(B)
A= exists $ENV{$name}B= not length $ENV{$name}
ABresult
0X0
100
111
44 }
45  
46 1 return HTTP::Tiny->new(timeout => $timeout);
47 }
48  
49 1 ▶sub clear_cache {
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod documented
50 1 my $self = shift;
51 1 %{ $self->cache } = ();
52 1 return $self;
53 }
54  
55 11 ▶sub _decode_content {
Subroutine _decode_content: called CC=6 SCAR=18.7
56 11 my ($self, $content, $URL) = @_;
57  
58 11 ▶ die "Rosary API returned an empty response from $URL\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and $content =~ /\S/ 0 11 11
Condition33%1/3
defined $content and $content =~ /\S/
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $content and $content =~ /\S/
defined $content $content =~ /\S/
Truth table33%1/3
A and B
A= defined $contentB= $content =~ /\S/
ABresult
0X0
100
111
59 if not defined $content or $content !~ /\S/;
60  
61 11 my $decoded = eval { decode_json($content) };
62 11 ▶ if (not defined $decoded or $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $decoded or $@) 1 10 11
Condition66%2/3
not defined $decoded or $@
l!l&&r!l&&!r
1010
MC/DC50%1/2
not defined $decoded or $@
not defined $decoded $@
Truth table66%2/3
not(A) or B
A= not defined $decodedB= $@
ABresult
1X1
011
000
63 1 ▶ my $error = $@ || q{unknown JSON decoding error};
Condition50%1/2
$@ || 'unknown JSON decoding error'
l!l
10
MC/DC0%0/1
$@ || 'unknown JSON decoding error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
64 1 chomp $error;
65 1 die "Rosary API returned invalid JSON from $URL: $error\n";
66 }
67  
68 10 return d2o -autoundef, $decoded;
69 }
70  
71 17 ▶sub _cached_content {
Subroutine _cached_content: called CC=6 SCAR=17.9
72 17 my ($self, $URL, $fresh_only) = @_;
73 17 ▶ return if not $self->cache_ttl or $self->cache_ttl <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->cache_ttl or $self->cache_ttl <= 0 12 5 17
Condition66%2/3
not $self->cache_ttl or $self->cache_ttl <= 0
l!l&&r!l&&!r
1205
MC/DC50%1/2
not $self->cache_ttl or $self->cache_ttl <= 0
not $self->cache_ttl $self->cache_ttl <= 0
Truth table66%2/3
not(A) or B
A= not $self->cache_ttlB= $self->cache_ttl <= 0
ABresult
1X1
011
000
74  
75 5 ▶ my $entry = $self->cache->{$URL} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->cache->{$URL} or return 3 2 5
76 3 ▶ return if $fresh_only and (time - $entry->{stored_at}) > $self->cache_ttl;
Branch: 1 branch
BranchTrueFalseTotal
if $fresh_only and time - $entry->{"stored_at"} > $self->cache_ttl 1 2 3
Condition100%3/3
$fresh_only and time - $entry->{"stored_at"} > $self->cache_ttl
!ll&&!rl&&r
111
MC/DC100%2/2
$fresh_only and time - $entry->{"stored_at"} > $self->cache_ttl
$fresh_only time - $entry->{"stored_at"} > $self->cache_ttl
Truth table100%3/3
A and B
A= $fresh_onlyB= time - $entry->{"stored_at"} > $self->cache_ttl
ABresult
0X0
100
111
77 2 return $entry->{content};
78 }
79  
80 8 ▶sub _cache_content {
Subroutine _cache_content: called CC=3 SCAR=11.0
81 8 my ($self, $URL, $content) = @_;
82 8 ▶ return if not $self->cache_ttl or $self->cache_ttl <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->cache_ttl or $self->cache_ttl <= 0 4 4 8
Condition66%2/3
not $self->cache_ttl or $self->cache_ttl <= 0
l!l&&r!l&&!r
404
MC/DC50%1/2
not $self->cache_ttl or $self->cache_ttl <= 0
not $self->cache_ttl $self->cache_ttl <= 0
Truth table66%2/3
not(A) or B
A= not $self->cache_ttlB= $self->cache_ttl <= 0
ABresult
1X1
011
000
83  
84 4 $self->cache->{$URL} = {
85 content => $content,
86 stored_at => time,
87 };
88 4 return;
89 }
90  
91 5 ▶sub _fallback_or_die {
Subroutine _fallback_or_die: called CC=3 SCAR=11.0
92 5 my ($self, $URL, $error) = @_;
93 5 chomp $error;
94  
95 5 ▶ if ($self->stale_if_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stale_if_error) 5 0 5
96 5 my $cached = $self->_cached_content($URL, 0);
97 5 ▶ if (defined $cached) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached) 1 4 5
98 1 warn "$error; using cached Rosary API response\n";
99 1 return $self->_decode_content($cached, $URL);
100 }
101 }
102  
103 4 die "$error\n";
104 }
105  
106 14 ▶sub _make_call {
Subroutine _make_call: called CC=9 SCAR=22.0
107 14 my ($self, $when) = @_;
108 14 $when = lc $when;
109 14 my $URL = sprintf "%s/%s", BASEURL, $when;
110  
111 # Repeated requests are cheap locally, but preserve the semantics of
112 # /random by never satisfying it from a fresh cache entry.
113 14 ▶ if (lc($when) ne q{random}) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $when ne "random") 12 2 14
114 12 my $cached = $self->_cached_content($URL, 1);
115 12 ▶ return $self->_decode_content($cached, $URL) if defined $cached;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cached 1 11 12
116 }
117  
118 13 my $response = eval { $self->ua->get($URL) };
119 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 12 13
120 1 my $error = $@;
121 1 chomp $error;
122 1 return $self->_fallback_or_die(
123 $URL,
124 "Rosary API request to $URL failed: $error",
125 );
126 }
127  
128 12 ▶ if (ref($response) ne q{HASH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response ne "HASH") 1 11 12
129 1 return $self->_fallback_or_die(
130 $URL,
131 "Rosary API request to $URL returned an invalid HTTP response",
132 );
133 }
134  
135 11 ▶ if (not $response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 2 9 11
136 2 ▶ my $status = defined $response->{status} ? $response->{status} : q{unknown};
Branch: 1 branch
BranchTrueFalseTotal
defined $response->{'status'} ? : 2 0 2
137 2 ▶ my $reason = defined $response->{reason} ? $response->{reason} : q{HTTP error};
Branch: 1 branch
BranchTrueFalseTotal
defined $response->{'reason'} ? : 2 0 2
138 2 return $self->_fallback_or_die(
139 $URL,
140 "Rosary API request to $URL failed: HTTP $status $reason",
141 );
142 }
143  
144 9 my $content = $response->{content};
145 9 my $decoded = eval { $self->_decode_content($content, $URL) };
146 9 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 8 9
147 1 my $error = $@;
148 1 chomp $error;
149 1 return $self->_fallback_or_die($URL, $error);
150 }
151  
152 8 $self->_cache_content($URL, $content);
153 8 return $decoded;
154 }
155  
156 5 ▶sub mp3Link {
Subroutine mp3Link: called CC=12 SCAR=26.5
Pod documented
157 5 my ($self, $when) = @_;
158 5 my @mp3 = qw/today yesterday tomorrow random/;
159 5 ▶ return "" if not defined $when or not grep { m/^\Q$when\E$/i } @mp3;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $when and grep {/^\Q$when\E$/i;} @mp3 0 5 5
Condition33%1/3
defined $when and grep {/^\Q$when\E$/i;} @mp3
!ll&&!rl&&r
005
MC/DC0%0/2
defined $when and grep {/^\Q$when\E$/i;} @mp3
defined $when grep {/^\Q$when\E$/i;} @mp3
Truth table33%1/3
A and B
A= defined $whenB= grep {/^\Q$when\E$/i;} @mp3
ABresult
0X0
100
111
160  
161 5 my $resp = $self->_make_call($when);
162 5 ▶ my $type = reftype($resp) || q{};
Condition50%1/2
builtin::reftype($resp) || ''
l!l
50
MC/DC0%0/1
builtin::reftype($resp) || ''
builtin::reftype($resp)
Truth table50%1/2
A
A= builtin::reftype($resp)
Aresult
11
00
163 5 my $item;
164  
165 5 ▶ if ($type eq q{ARRAY}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 4 1 5
elsif ($type eq 'HASH') { } 1 0 1
166 4 ▶ $item = $resp->[0] if @$resp;
Branch: 1 branch
BranchTrueFalseTotal
if @$resp 3 1 4
167 }
168 elsif ($type eq q{HASH}) {
169 1 $item = $resp;
170 }
171  
172 5 ▶ if ($item and (reftype($item) || q{}) eq q{HASH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item and (builtin::reftype($item) || "") eq "HASH") 4 1 5
Condition60%3/5
$item and (builtin::reftype($item) || "") eq "HASH"
!ll&&!rl&&r
104
builtin::reftype($item) || ''
l!l
40
MC/DC50%1/2
$item and (builtin::reftype($item) || "") eq "HASH"
$item builtin::reftype($item)
Truth table66%2/3
A and B
A= $itemB= builtin::reftype($item)
ABresult
0X0
100
111
173 4 my $file = $item->{mp3Link};
174 4 ▶ return sprintf "%s/%s", DAILYURL, $file if defined $file and length $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file and length $file 4 0 4
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
004
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
175 }
176  
177 1 warn "\nNo MP3 content in response from the Rosary API ... try the following links:\n";
178 1 warn <<'EOF_LINKS';
179  
180 https://www.discerninghearts.com/catholic-podcasts/holy-rosary/
181  
182 + https://www.discerninghearts.com/Devotionals/Rosary-Joyful-Mysteries.mp3
183  
184 + https://www.discerninghearts.com/Devotionals/Rosary-Luminous-Mysteries.mp3
185  
186 + https://www.discerninghearts.com/Devotionals/Rosary-Sorrowful-Mysteries.mp3
187  
188 + https://www.discerninghearts.com/Devotionals/Rosary-Glorious-Mysteries.mp3
189  
190 ... please try back again later
191 EOF_LINKS
192 1 die "Rosary API returned no MP3 link\n";
193 }
194  
195 9 ▶sub day {
Subroutine day: called CC=7 SCAR=23.0
Pod documented
196 9 my ($self, $when) = @_;
197 9 my @days = qw/sunday monday tuesday wednesday thursday friday saturday/;
198 9 ▶ return "" if not defined $when or not grep { m/^\Q$when\E$/i } @days;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $when and grep {/^\Q$when\E$/i;} @days 1 8 9
Condition66%2/3
defined $when and grep {/^\Q$when\E$/i;} @days
!ll&&!rl&&r
018
MC/DC50%1/2
defined $when and grep {/^\Q$when\E$/i;} @days
defined $when grep {/^\Q$when\E$/i;} @days
Truth table66%2/3
A and B
A= defined $whenB= grep {/^\Q$when\E$/i;} @days
ABresult
0X0
100
111
199  
200 8 my $resp = $self->_make_call($when);
201 4 ▶ my $type = reftype($resp) || q{};
Condition50%1/2
builtin::reftype($resp) || ''
l!l
40
MC/DC0%0/1
builtin::reftype($resp) || ''
builtin::reftype($resp)
Truth table50%1/2
A
A= builtin::reftype($resp)
Aresult
11
00
202 4 ▶ return $resp->[0] if $type eq q{ARRAY} and @$resp;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" and @$resp 4 0 4
Condition33%1/3
$type eq "ARRAY" and @$resp
!ll&&!rl&&r
004
MC/DC0%0/2
$type eq "ARRAY" and @$resp
$type eq "ARRAY" @$resp
Truth table33%1/3
A and B
A= $type eq "ARRAY"B= @$resp
ABresult
0X0
100
111
203 0 ▶ return $resp if $type eq q{HASH};
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 0 0 0
204 0 return "";
205 }
206  
207 # Get detailed recitations
208 2 ▶sub details {
Subroutine details: called CC=3 SCAR=11.0
Pod documented
209 2 my ($self, $when) = @_;
210 2 my @mysteries = qw/joyful glorious sorrowful luminous/;
211 2 ▶ return "" if not defined $when or not grep { m/^\Q$when\E$/i } @mysteries;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $when and grep {/^\Q$when\E$/i;} @mysteries 1 1 2
Condition66%2/3
defined $when and grep {/^\Q$when\E$/i;} @mysteries
!ll&&!rl&&r
011
MC/DC50%1/2
defined $when and grep {/^\Q$when\E$/i;} @mysteries
defined $when grep {/^\Q$when\E$/i;} @mysteries
Truth table66%2/3
A and B
A= defined $whenB= grep {/^\Q$when\E$/i;} @mysteries
ABresult
0X0
100
111
212 1 return $self->_make_call($when);
213 }
214  
215 3 1;
216  
217 __END__
« blib/lib/Webservice/Rosary/Stream.pm ↑ Summary blib/lib/Webservice/Rosary/Tradition.pm »