blib/lib/Bank/Holidays.pm

filter: Statementstmt 80.0% 64 / 80 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 67.6% 92 / 136 SCAR 37.4
CC
24
Cov
68%
reserve_holidays
43.8
is_holiday
8.1
new
3.0
CRAP 42.3
↑ 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 require Exporter;
11  
12 3 our @ISA = qw(Exporter);
13  
14 # Items to export into callers namespace by default. Note: do not export
15 # names by default without a very good reason. Use EXPORT_OK instead.
16 # Do not simply export all your public functions/methods/constants.
17  
18 # This allows declaration use Bank::Holidays ':all';
19 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
20 # will save memory.
21 3 our %EXPORT_TAGS = (
22 'all' => [
23 qw( is_holiday reserve_holidays
24  
25 )
26 ]
27 );
28  
29 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
30  
31 3 our @EXPORT = qw(
32  
33 );
34  
35 3 our $VERSION = '0.81';
36  
37 sub new {
38 8 ▶ my ( $package, %params ) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
39  
40 8 my $param;
41 $param->{dt} =
42 $params{dt}
43 ? $params{dt}
44 : $params{date}
45 ? $params{date}
46 8 ▶ : DateTime->now;
Branch: 2 branches
BranchTrueFalseTotal
$params{'dt'} ? : 3 5 8
$params{'date'} ? : 5 0 5
47 8 $param->{holidays} = reserve_holidays();
48 8 bless $param, $package;
49 }
50  
51 sub reserve_holidays() {
52 8 ▶ my $te = HTML::TableExtract->new();
Subroutine reserve_holidays: called CC=16 SCAR=37.8
Pod undocumented
53  
54 8 my $ua = new LWP::UserAgent;
55  
56 8 $ua->timeout(120);
57  
58 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
59  
60 8 ▶ unless ( -d $home . "/.bankholidays" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $home . '/.bankholidays') 1 7 8
61 1 mkdir( $home . "/.bankholidays" );
62 }
63  
64 8 my $cache = $home . "/.bankholidays/frbholidays.html";
65  
66 # Cache the content from the FRB since holdays are unlikely to
67 # change from day to day (or year to year)
68  
69 8 my $content;
70  
71 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
72 7 ▶ open( my $fh, "<", $cache ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $cache or die $! 0 7 7
73 7 local $/ = undef;
74 7 $content = <$fh>;
75 7 close $fh;
76 }
77 else {
78 1 my $url = 'http://www.federalreserve.gov/aboutthefed/k8.htm';
79  
80 1 my $request = new HTTP::Request( 'GET', $url );
81  
82 1 my $response = $ua->request($request);
83  
84 1 $content = $response->content();
85  
86 1 ▶ open( my $fh, ">", $cache ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cache or die $! 0 1 1
87 1 print $fh $content;
88 1 close $fh;
89 }
90  
91 8 $te->parse($content);
92  
93 8 my $months = {
94 'January' => 1,
95 'February' => 2,
96 'March' => 3,
97 'April' => 4,
98 'May' => 5,
99 'June' => 6,
100 'July' => 7,
101 'August' => 8,
102 'September' => 9,
103 'October' => 10,
104 'November' => 11,
105 'December' => 12
106 };
107  
108 8 my $holidays;
109  
110 8 foreach my $ts ( $te->tables ) {
111 0 ▶ next if ( $ts->coords ) != 2;
Branch: 1 branch
BranchTrueFalseTotal
if $ts->coords != 2 0 0 0
112 0 my @colyears;
113 0 foreach my $row ( $ts->rows ) {
114  
115 0 ▶ next unless @$row;
Branch: 1 branch
BranchTrueFalseTotal
unless @$row 0 0 0
116 0 ▶ map { s/\r|\n//g if $_ } @$row;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
117 0 my $colcount = 0;
118 0 foreach my $col (@$row) {
119 0 ▶ if ($col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col) 0 0 0
120 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
121 0 $colyears[$colcount] = $1;
122 }
123 elsif ( $col =~ /(\w+)\s(\d{1,2})(\*?)/ ) {
124 0 push @{ $holidays->{ $colyears[$colcount] }->{ $months->{$1} } },
125 {
126 day => $2,
127 satflag => $3
128 };
129  
130 }
131 }
132 0 $colcount++;
133 }
134 }
135 }
136 8 return $holidays;
137 }
138  
139 sub is_holiday {
140 12 ▶ my ( $param, %opts ) = @_;
Subroutine is_holiday: called CC=7 SCAR=21.0
Pod undocumented
141  
142 12 ▶ if ( $opts{date} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"date"}) 0 12 12
143 0 $param->{dt} = $opts{date};
144 }
145  
146 12 ▶ if ( $opts{Tomorrow} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'Tomorrow'}) { } 4 8 12
elsif ($opts{'Yesterday'}) { } 4 4 8
147 4 $param->{dt}->add( days => 1 );
148 }
149 elsif ( $opts{Yesterday} ) {
150 4 $param->{dt}->subtract( days => 1 );
151 }
152 12 ▶ return 1 if $param->{dt}->dow == 7;
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"dt"}->dow == 7 0 12 12
153 12 foreach my $holiday ( @{ $param->{holidays}->{ $param->{dt}->year }->{ int( $param->{dt}->month ) } } ) {
154 0 ▶ return 1 if int( $param->{dt}->day ) == $holiday->{day};
Branch: 1 branch
BranchTrueFalseTotal
if int $param->{"dt"}->day == $holiday->{"day"} 0 0 0
155 }
156 12 return undef;
157 }
158  
159 # Preloaded methods go here.
160  
161 3 1;
162 __END__
163 # Below is stub documentation for your module. You'd better edit it!
164  
165 =head1 NAME
166  
167 Bank::Holidays - Perl extension for determining Federal Reserve holidays. 2015 - 2019
168  
169 =head1 VERSION
170  
171 0.81
172  
173 =head1 SYNOPSIS
174  
175 use Bank::Holidays;
176  
177 # Using the date => reference you can specify any date you like.
178 my $bank = Bank::Holidays->new( date => DateTime->now ); # or any datetime object
179  
180 # Check yesterday to see if it was a holiday
181 print "Yesterday ";
182 $bank->is_holiday( Yesterday => 1 ) ? print "is " : print "is not";
183 print " a holiday";
184  
185 # Check to see if today is a holiday;
186 print "Today ";
187 $bank->is_holiday ? print "is" : print "is not";
188 print " a holiday\n";
189  
190 # Check to see if tomorrow is a holiday.
191 print "Tomorrow ";
192 $bank->is_holiday( Tomorrow => 1 ) ? print "is" : print "is not";
193 print " a holiday\n";
194  
195  
196 =head1 DESCRIPTION
197  
198 Bank::Holidays reads a page from the Federal Reserve's website that contains
199 holidays until 2016. However should the FR's site change this code may not work.
200 This code is very useful for determining days that a valid banking transaction
201 can occur, remembering that Sunday is never a banking day.
202  
203 =head2 methods
204  
205 new( [ date => dt->object ] ) Defaults to today if undefines.
206  
207 is_holiday( [ Yesterday|Tomorrow => 1 ] ) To determine what day to check default is current date in date object.
208  
209 =head1 AUTHOR
210  
211 Tyler Hardison, E<lt>thardison@seraph-net.netE<gt>
212  
213 =head1 THANKS TO
214  
215 Alex White E<lt>wu@geekfarm.orgE<gt> - For providing a patch for 2010 changes to the fed's site.
216  
217 Robert Leap E<lt>robertleap@gmail.comE<gt> - For providing a patch for the 2012-2016 holday period.
218  
219 =head1 COPYRIGHT AND LICENSE
220  
221 Copyright (C) 2013 by Tyler Hardison
222  
223 This library is free software; you can redistribute it and/or modify
224 it under the same terms as Perl itself, either Perl version 5.8.3 or,
225 at your option, any later version of Perl 5 you may have available.
226  
227  
228 =cut
↑ Summary