blib/lib/Bank/Holidays.pm

filter: Statementstmt 78.6% 59 / 75 Branchbran 44.1% 15 / 34 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 66.4% 87 / 131 CC 24 SCAR 31.6
CC
24
Cov
67%
reserve_holidays
42.5
is_holiday
8.1
new
3.0
CRAP 23.5
↑ Summary
1 package Bank::Holidays;
2  
3 3 ▶use 5.006001;
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 3 ▶use HTML::TableExtract;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.86';
11  
12 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
13 8 my ( $package, %params ) = @_;
14  
15 8 my $param;
16 $param->{dt} =
17 $params{dt}
18 ? $params{dt}
19 : $params{date}
20 ? $params{date}
21 8 ▶ : DateTime->now;
Branch: 2 branches
BranchTrueFalseTotal
$params{'dt'} ? : 3 5 8
$params{'date'} ? : 5 0 5
22 8 $param->{holidays} = reserve_holidays();
23 8 bless $param, $package;
24 }
25  
26 8 ▶sub reserve_holidays() {
Subroutine reserve_holidays: called CC=16 SCAR=37.5
Pod undocumented
27 8 my $te = HTML::TableExtract->new();
28  
29 8 my $ua = LWP::UserAgent->new();
30  
31 8 $ua->timeout(120);
32  
33 8 ▶ my $home = $ENV{HOME} || $ENV{LOCALAPPDATA};
Condition33%1/3
$ENV{'HOME'} || $ENV{'LOCALAPPDATA'}
l!l&&r!l&&!r
800
MC/DC0%0/2
$ENV{'HOME'} || $ENV{'LOCALAPPDATA'}
$ENV{'HOME'} $ENV{'LOCALAPPDATA'}
Truth table33%1/3
A || B
A= $ENV{'HOME'}B= $ENV{'LOCALAPPDATA'}
ABresult
1X1
011
000
34  
35 8 ▶ unless ( -d $home . "/.bankholidays" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $home . '/.bankholidays') 1 7 8
36 1 mkdir( $home . "/.bankholidays" );
37 }
38  
39 8 my $cache = $home . "/.bankholidays/frbholidays.html";
40  
41 # Cache the content from the FRB since holdays are unlikely to
42 # change from day to day (or year to year)
43  
44 8 my $content;
45  
46 8 ▶ if ( -f $cache && ( time() - ( stat($cache) )[9] ) < 86400 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cache and time - (stat $cache)[9] < 86400) { } 7 1 8
Condition66%2/3
-f $cache and time - (stat $cache)[9] < 86400
!ll&&!rl&&r
107
MC/DC50%1/2
-f $cache and time - (stat $cache)[9] < 86400
-f $cache time - (stat $cache)[9] < 86400
Truth table66%2/3
A and B
A= -f $cacheB= time - (stat $cache)[9] < 86400
ABresult
0X0
100
111
47 7 ▶ open my $fh, "<", $cache or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $cache or die $! 7 0 7
48 7 $content = do { local $/ = <$fh> };
49 7 close $fh;
50 }
51 else {
52 1 my $url = 'http://www.federalreserve.gov/aboutthefed/k8.htm';
53  
54 1 my $request = HTTP::Request->new( 'GET', $url );
55  
56 1 my $response = $ua->request($request);
57  
58 1 $content = $response->content();
59  
60 1 ▶ open my $fh, ">", $cache or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cache or die $! 1 0 1
61 1 print {$fh} $content;
62 1 close $fh;
63 }
64  
65 8 $te->parse($content);
66  
67 8 my $months = {
68 'January' => 1,
69 'February' => 2,
70 'March' => 3,
71 'April' => 4,
72 'May' => 5,
73 'June' => 6,
74 'July' => 7,
75 'August' => 8,
76 'September' => 9,
77 'October' => 10,
78 'November' => 11,
79 'December' => 12
80 };
81  
82 8 my $holidays;
83  
84 8 foreach my $ts ( $te->tables ) {
85 0 ▶ next if ( $ts->coords ) != 2;
Branch: 1 branch
BranchTrueFalseTotal
if $ts->coords != 2 0 0 0
86 0 my @colyears;
87 0 foreach my $row ( $ts->rows ) {
88  
89 0 ▶ next unless @$row;
Branch: 1 branch
BranchTrueFalseTotal
unless @$row 0 0 0
90 0 ▶ map { s/\r|\n//g if $_ } @$row;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
91 0 my $colcount = 0;
92 0 foreach my $col (@$row) {
93 0 ▶ if ($col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col) 0 0 0
94 0 ▶ if ( $col =~ /(\d{4})/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($col =~ /(\d{4})/) { } 0 0 0
elsif ($col =~ /(\w+)\s(\d{1,2})(\*?)/) { } 0 0 0
95 0 $colyears[$colcount] = $1;
96 }
97 elsif ( $col =~ /(\w+)\s(\d{1,2})(\*?)/ ) {
98 0 push @{ $holidays->{ $colyears[$colcount] }->{ $months->{$1} } },
99 {
100 day => $2,
101 satflag => $3
102 };
103  
104 }
105 }
106 0 $colcount++;
107 }
108 }
109 }
110 8 return $holidays;
111 }
112  
113 12 ▶sub is_holiday {
Subroutine is_holiday: called CC=7 SCAR=21.0
Pod undocumented
114 12 my ( $param, %opts ) = @_;
115  
116 12 ▶ if ( $opts{date} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"date"}) 0 12 12
117 0 $param->{dt} = $opts{date};
118 }
119  
120 12 ▶ if ( $opts{Tomorrow} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'Tomorrow'}) { } 4 8 12
elsif ($opts{'Yesterday'}) { } 4 4 8
121 4 $param->{dt}->add( days => 1 );
122 }
123 elsif ( $opts{Yesterday} ) {
124 4 $param->{dt}->subtract( days => 1 );
125 }
126 12 ▶ return 1 if $param->{dt}->dow == 7;
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"dt"}->dow == 7 0 12 12
127 12 foreach my $holiday ( @{ $param->{holidays}->{ $param->{dt}->year }->{ int( $param->{dt}->month ) } } ) {
128 0 ▶ return 1 if int( $param->{dt}->day ) == $holiday->{day};
Branch: 1 branch
BranchTrueFalseTotal
if int $param->{"dt"}->day == $holiday->{"day"} 0 0 0
129 }
130 12 return undef;
131 }
132  
133 # Preloaded methods go here.
134  
135 3 1;
136 __END__
↑ Summary