blib/lib/App/MergeCal.pm

filter: Statementstmt 54.8% 34 / 62 Branchbran 21.4% 3 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 62.5% 10 / 16 Podpod 100% 9 / 9 totaltotal 53.8% 56 / 104 CC 12 SCAR 21.3
CC
12
Cov
47%
gather
20.0
render
20.0
new_from_json_file
12.0
CRAP 8.4
↑ Summary
1 =head1 NAME
2  
3 App::MergeCal
4  
5 =head1 ABSTRACT
6  
7 Command line program that merges iCal files into a single calendar.
8  
9 =head1 SYNOPSIS
10  
11 use App::MergeCal;
12  
13 my $app = App::MergeCal->new;
14 $app->run;
15  
16 # Or, more likely, use the mergecal program
17  
18 =cut
19  
20 4 ▶use 5.24.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Feature::Compat::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 package App::MergeCal; # For PAUSE
24  
25 4 class App::MergeCal {
26  
27 4 our $VERSION = '0.0.5';
28  
29 4 ▶ use Encode 'encode_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶ use Text::vFile::asData;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶ use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 4 field $vfile_parser :param = Text::vFile::asData->new;
36 4 field $title :param;
37 4 field $output :param = '';
38 4 field $calendars :param;
39 4 field $objects;
40  
41 =head1 METHODS
42  
43 =head2 $app->calendars, $app->title, $app->output
44  
45 Accessors for fields.
46  
47 =cut
48  
49 4 ▶ method calendars { return $calendars }
Subroutine calendars: called CC=1 SCAR=0.0
Pod documented
50 1 ▶ method title { return $title }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
51 0 ▶ method output { return $title }
Subroutine output: not called CC=1 SCAR=6.9
Pod documented
52  
53 =head2 $app->run
54  
55 Main driver method.
56  
57 =cut
58  
59 0 ▶ method run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
60 0 $self->gather;
61 0 $self->render;
62 }
63  
64 =head2 $app->gather
65  
66 Access all of the calendars and gather their contents into $objects.
67  
68 =cut
69  
70 0 ▶ method gather {
Subroutine gather: not called CC=4 SCAR=30.0
Pod documented
71 0 $self->clean_calendars;
72 0 for (@$calendars) {
73 0 ▶ my $ics = get($_) or die "Can't get " . $_->as_string . "\n";
Branch: 1 branch
BranchTrueFalseTotal
my $ics = &get($_) or die q[Can't get ] . $_->as_string . "\n" 0 0 0
74 0 $ics = encode_utf8($ics);
75 0 ▶ open my $fh, '<', \$ics or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$ics or die $! 0 0 0
76 0 my $data = $vfile_parser->parse( $fh );
77  
78 0 push @$objects, @{ $data->{objects}[0]{objects} };
79 }
80 }
81  
82 =head2 $app->render
83  
84 Take all of the objects in $objects and write them to an output file.
85  
86 =cut
87  
88 0 ▶ method render {
Subroutine render: not called CC=4 SCAR=30.0
Pod documented
89 0 my $combined = {
90 type => 'VCALENDAR',
91 properties => {
92 'X-WR-CALNAME' => [ { value => $title } ],
93 },
94 objects => $objects,
95 };
96  
97 0 my $out_fh;
98 0 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 0 0 0
99 0 ▶ open $out_fh, '>', $output
Branch: 1 branch
BranchTrueFalseTotal
open $out_fh, ">", $output or die "Cannot open output file [$output]: $!\n" 0 0 0
100 or die "Cannot open output file [$output]: $!\n";
101 0 select $out_fh;
102 }
103  
104 0 say "$_\r" for Text::vFile::asData->generate_lines($combined);
105 }
106  
107 =head2 $app->clean_calendars
108  
109 Ensure that all of the calendars are URIs. If a calendar doesn't have a scheme
110 then it is assumed to be a file URI.
111  
112 =cut
113  
114 1 ▶ method clean_calendars {
Subroutine clean_calendars: called CC=4 SCAR=13.9
Pod documented
115 1 for (@$calendars) {
116 2 ▶ $_ = URI->new($_) unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 2 0 2
117 2 ▶ if (! $_->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_->scheme) 1 1 2
118 1 $_ = URI->new('file://' . $_);
119 }
120 }
121 }
122  
123 =head2 App::MergeCal->new, App::MergeCal->new_from_json, App::MergeCal->new_from_json_file
124  
125 Constructor methods.
126  
127 =over 4
128  
129 =item new
130  
131 Constructs an object from a hash of attribute/value pairs
132  
133 =item new_from_json
134  
135 Constructs an object from a JSON string representing attribute/value pairs.
136  
137 =item new_from_json_file
138  
139 Constructs an object from a file containing a JSON string representing
140 attribute/value pairs.
141  
142 =back
143  
144 =cut
145  
146 0 ▶ sub new_from_json {
Subroutine new_from_json: not called CC=1 SCAR=6.9
Pod documented
147 0 my $class = shift;
148 0 my ($json) = @_;
149  
150 0 my $data = JSON->new->decode($json);
151  
152 0 return $class->new(%$data);
153 }
154  
155 0 ▶ sub new_from_json_file {
Subroutine new_from_json_file: not called CC=3 SCAR=24.8
Pod documented
156 0 my $class = shift;
157 0 ▶ my $conf_file = $_[0] // 'config.json';
Condition0%0/2
$_[0] // "config.json"
l!l
00
MC/DC0%0/1
$_[0] // "config.json"
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
158  
159 0 ▶ open my $conf_fh, '<', $conf_file or die "$conf_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $conf_fh, "<", $conf_file or die "${conf_file}: $!" 0 0 0
160 0 my $json = do { local $/; <$conf_fh> };
161  
162 0 return $class->new_from_json($json);
163 }
164 }
165  
166 =head1 CONFIGURATION
167  
168 The behaviour of the program is controlled by a JSON file. The default name
169 for this file is C<config.json>. The contents of the file will look something
170 like this:
171  
172 {
173 "title":"My Combined Calendar",
174 "output":"my_calendar.ics",
175 "calendars":[
176 "https://example.com/some_calendar.ics",
177 "https://example.com/some_other_calendar.ics",
178 ]
179 }
180  
181 This configuration will read the the calendars from the URLs listed and
182 combine their contents into a file called C<my_calendar.ics> (which you will
183 presumably make available on the internet).
184  
185 The <output> configuration option is optional. If it is omitted, then the
186 output will be written to C<STDOUT>.
187  
188 =head1 AUTHOR
189  
190 Dave Cross <dave@perlhacks.com>
191  
192 =head1 LICENSE
193  
194 Copyright (C) 2024, Magnum Solutions Ltd. All Rights Reserved.
195  
196 This script is free software; you can redistribute it and/or modify it
197 under the same terms as Perl itself.
198  
199 =cut
200  
201 4 1;
↑ Summary