File Coverage

blib/lib/Webservice/Rosary/API.pm
Criterion Covered Total %
statement 18 44 40.9
branch 0 10 0.0
condition 0 3 0.0
subroutine 6 10 60.0
pod 3 4 75.0
total 27 71 38.0


line stmt bran cond sub pod time code
1             package Webservice::Rosary::API;
2              
3 1     1   350656 use v5.10;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         37  
5              
6 1     1   549 use Util::H2O::More qw/baptise ddd HTTPTiny2h2o h2o o2d/;
  1         9276  
  1         79  
7 1     1   1909 use HTTP::Tiny qw//;
  1         48039  
  1         87  
8              
9             our $VERSION = "0.1.5";
10              
11             use constant {
12 1         757 BASEURL => "https://the-rosary-api.vercel.app/v1",
13             DAILYURL => "https://dailyrosary.cf",
14 1     1   9 };
  1         1  
15              
16             sub new {
17 1     1 1 738 my $pkg = shift;
18 1         8 my $params = { @_, ua => HTTP::Tiny->new };
19 1         144 my $self = baptise $params, $pkg, qw//;
20 1         218 return $self;
21             }
22              
23             sub _make_call {
24 0     0     my ($self, $when) = @_;
25 0           my $URL = sprintf "%s/%s", BASEURL, $when;
26 0           my $resp = HTTPTiny2h2o $self->ua->get($URL);
27 0           return $resp->content;
28             }
29              
30             sub mp3Link {
31 0     0 1   my ($self, $when) = @_;
32 0           my @mp3 = qw/today yesterday tomorrow random/;
33             # invalid
34 0 0         return "" if (not grep { m/^$when$/i } @mp3);
  0            
35             # valid
36 0           my $resp = $self->_make_call($when);
37 0           my $ref = $resp->shift;
38             # the problem here is that this API call in the past has returned
39             # a JSON hash rather than an array in some cases; in other cases
40             # it has returned an empty JSON array
41 0 0 0       if (my $ref = $resp->shift) {
    0          
42 0           return sprintf "%s/%s", DAILYURL, $ref->mp3Link;
43             }
44             elsif (ref o2d $resp ne "ARRAY" and my $file = $resp->mp3Link) {
45 0           return sprintf "%s/%s", DAILYURL, $file;
46             }
47             else {
48 0           warn "\nNo content in response from https://therosaryapi.cf ... try the following links:\n";
49 0           warn <
50              
51             https://www.discerninghearts.com/catholic-podcasts/holy-rosary/ -
52              
53             + https://www.discerninghearts.com/Devotionals/Rosary-Joyful-Mysteries.mp3
54              
55             + https://www.discerninghearts.com/Devotionals/Rosary-Luminous-Mysteries.mp3
56              
57             + https://www.discerninghearts.com/Devotionals/Rosary-Sorrowful-Mysteries.mp3
58              
59             + https://www.discerninghearts.com/Devotionals/Rosary-Glorious-Mysteries.mp3
60              
61             ... please try back again later
62             EOF
63 0           die "\n";
64             }
65             }
66              
67             sub day {
68 0     0 1   my ($self, $when) = @_;
69 0           my @days = qw/sunday monday tuesday wednesday thursday friday/;
70 0 0         return "" if (not grep { m/^$when$/i } @days);
  0            
71 0           return $self->_make_call($when)->get(0);
72             }
73              
74             # Get detailed recitations
75             sub details {
76 0     0 0   my ($self, $when) = @_;
77 0           my @mysteries = qw/joyful glorious sorrowful luminous/;
78 0 0         return "" if (not grep { m/^$when$/i } @mysteries);
  0            
79 0           return $self->_make_call($when);
80             }
81              
82             123
83              
84             __END__