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 SCAR 38.0
CC
24
Cov
67%
reserve_holidays
42.5
is_holiday
8.1
new
3.0
CRAP 44.8
↑ 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.84';
11  
12 sub new {
13 8 ▶ my ( $package, %params ) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
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 sub reserve_holidays() {
27 8 ▶ my $te = HTML::TableExtract->new();
Subroutine reserve_holidays: called CC=16 SCAR=37.5
Pod undocumented
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 $! 0 7 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 $! 0 1 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 sub is_holiday {
114 12 ▶ my ( $param, %opts ) = @_;
Subroutine is_holiday: called CC=7 SCAR=21.0
Pod undocumented
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__
137 # Below is stub documentation for your module. You'd better edit it!
138  
139 =head1 NAME
140  
141 Bank::Holidays - Perl extension for determining Federal Reserve holidays. 2015 - 2019
142  
143 =head1 VERSION
144  
145 0.84
146  
147 =head1 SYNOPSIS
148  
149 use Bank::Holidays;
150  
151 # Using the date => reference you can specify any date you like.
152 my $bank = Bank::Holidays->new( date => DateTime->now ); # or any datetime object
153  
154 # Check yesterday to see if it was a holiday
155 print "Yesterday ";
156 $bank->is_holiday( Yesterday => 1 ) ? print "is " : print "is not";
157 print " a holiday";
158  
159 # Check to see if today is a holiday;
160 print "Today ";
161 $bank->is_holiday ? print "is" : print "is not";
162 print " a holiday\n";
163  
164 # Check to see if tomorrow is a holiday.
165 print "Tomorrow ";
166 $bank->is_holiday( Tomorrow => 1 ) ? print "is" : print "is not";
167 print " a holiday\n";
168  
169 =head1 EXPORTER
170  
171 As of version 0.82, no functions are exported, and EXPORTER has been removed. It's
172 unclear if exported functions worked, as both (`is_holiday' and `reserve_holidays')
173 required an existing Bank::Holidays object to have been created by the caller.
174  
175 =head1 DESCRIPTION
176  
177 Bank::Holidays reads a page from the Federal Reserve's website that contains
178 holidays until 2019. However should the FR's site change this code may not work.
179 This code is very useful for determining days that a valid banking transaction
180 can occur, remembering that Sunday is never a banking day.
181  
182 =head2 methods
183  
184 new( [ date => dt->object ] ) Defaults to today if undefines.
185  
186 is_holiday( [ Yesterday|Tomorrow => 1 ] ) To determine what day to check default is current date in date object.
187  
188 =head1 AUTHOR
189  
190 Tyler Hardison, E<lt>thardison@seraph-net.netE<gt>
191  
192 =head1 THANKS TO
193  
194 Alex White E<lt>wu@geekfarm.orgE<gt> - For providing a patch for 2010 changes to the fed's site.
195  
196 Robert Leap E<lt>robertleap@gmail.comE<gt> - For providing a patch for the 2012-2016 holday period.
197  
198 =head1 COPYRIGHT AND LICENSE
199  
200 Copyright (C) 2013 by Tyler Hardison
201  
202 This library is free software; you can redistribute it and/or modify
203 it under the same terms as Perl itself, either Perl version 5.8.3 or,
204 at your option, any later version of Perl 5 you may have available.
205  
206  
207 =cut
↑ Summary