| 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
| |||||||||||||||||||||||||||||
| 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'}
MC/DC0%0/2
$ENV{'HOME'} || $ENV{'LOCALAPPDATA'}
$ENV{'HOME'}
$ENV{'LOCALAPPDATA'}
Truth table33%1/3
A || B
A= $ENV{'HOME'}B= $ENV{'LOCALAPPDATA'}
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 8 | ▶ | unless ( -d $home . "/.bankholidays" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
-f $cache and time - (stat $cache)[9] < 86400
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
| |||||||||||||||||||||||||||||
| 47 | 7 | ▶ | open my $fh, "<", $cache or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 86 | 0 | my @colyears; | |||||||||||||||||||||||||||
| 87 | 0 | foreach my $row ( $ts->rows ) { | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | next unless @$row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | map { s/\r|\n//g if $_ } @$row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | my $colcount = 0; | |||||||||||||||||||||||||||
| 92 | 0 | foreach my $col (@$row) { | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($col) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $col =~ /(\d{4})/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | 0 | $param->{dt} = $opts{date}; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 12 | ▶ | if ( $opts{Tomorrow} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 12 | return undef; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Preloaded methods go here. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 3 | 1; | |||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||