auto/share/dist/Date-Holidays-GB/generate_date_holidays_gb.pl untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 127 CC 3 SCAR 10.6
CC
3
Cov
0%
parse_existing
6.0
holiday_data
6.0
file
2.0
CRAP 2.9
1 #!/usr/bin/env perl
2  
3 # script to update Date::Holidays::GB with the latest bank holiday dates from
4 # https://www.gov.uk/bank-holidays
5  
6 use strict;
7 use warnings;
8  
9 use Cwd qw( realpath );
10 use DateTime;
11 use File::Spec::Functions qw( catfile splitpath updir );
12 use JSON qw(decode_json);
13 use List::MoreUtils qw( uniq );
14 use LWP::Simple qw( get );
15 use Time::Local ();
16  
17 my $URL = 'https://www.gov.uk/bank-holidays.json';
18  
19 my %CODE = (
20 'england-and-wales' => 'EAW',
21 'scotland' => 'SCT',
22 'northern-ireland' => 'NIR',
23 );
24  
25 write_file( get_dates( download_json() ) );
26  
27 exit;
28  
29 sub file {
30 return catfile( ( splitpath( realpath __FILE__ ) )[ 0, 1 ],
31 updir, qw(lib Date Holidays GB.pm) );
32 }
33  
34 sub download_json {
35  
36 my $contents = get $URL or die "Can't download $URL";
37  
38 return decode_json($contents);
39 }
40  
41 sub get_dates {
42 my $data = shift;
43  
44 my %holiday;
45  
46 foreach my $region ( keys %{$data} ) {
47  
48 foreach my $event ( @{ $data->{$region}->{events} } ) {
49  
50 my ($year) = split /-/, $event->{date};
51  
52 $holiday{$year}->{ $event->{date} }->{ $CODE{$region} } = $event->{title};
53 }
54 }
55  
56 return \%holiday;
57 }
58  
59 sub read_file {
60 my ($file) = @_;
61  
62 open my $READ, '<:encoding(utf-8)', $file
63 or die "Unable to open $file for reading: $!";
64  
65 my $contents = do { local $/; <$READ> };
66  
67 close $READ;
68  
69 my ( $pm, $data ) = split /__DATA__/, $contents;
70  
71 return ( $pm, $data );
72 }
73  
74 sub write_file {
75 my ($holiday_data) = @_;
76  
77 my $file = file();
78  
79 my ( $pm, $data ) = read_file($file);
80  
81 open my $WRITE, '>:encoding(utf-8)', $file
82 or die "Unable to open $file for writing: $!";
83  
84 my $now = DateTime->now->ymd;
85  
86 $pm =~ s/sub date_generated \{[^}]+\}/sub date_generated { '$now' }/;
87  
88 print $WRITE $pm;
89 print $WRITE "__DATA__\n";
90 print $WRITE holiday_data( parse_existing($data), $holiday_data );
91  
92 close $WRITE;
93  
94 return 1;
95 }
96  
97 sub parse_existing {
98 my ($data) = @_;
99  
100 my %parsed;
101 my @lines = split /\n/, $data;
102 foreach my $line (@lines) {
103 next unless $line && $line =~ /\w/;
104 my ( $date, $code, $name ) = split /\t/, $line;
105  
106 my ($year) = split /-/, $date;
107  
108 $parsed{$year}->{$date}->{$code} = $name;
109 }
110  
111 return \%parsed;
112 }
113  
114 sub holiday_data {
115 my ( $existing, $new ) = @_;
116  
117 my @years = uniq keys %{$existing}, keys %{$new};
118  
119 my $data;
120 foreach my $year (sort @years) {
121  
122 # include old data, if removed from current download
123 my $source = $new->{$year} ? $new->{$year} : $existing->{$year};
124  
125 foreach my $date ( sort keys %{$source} ) {
126 foreach my $code ( sort keys %{ $source->{$date} } ) {
127 $data
128 .= sprintf( "%s\t%s\t%s\n", $date, $code, $source->{$date}->{$code} );
129 }
130 }
131 }
132  
133 return $data;
134 }
135  
136 1;
137