blib/lib/App/gcal.pm

filter: Statementstmt 41.3% 31 / 75 Branchbran 9.0% 2 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 72.7% 8 / 11 Podpod n/a totaltotal 34.7% 41 / 118 CC 16 SCAR 31.5
CC
16
Cov
32%
_save_to_gcal
56.0
run
30.0
_process_text
12.0
CRAP 23.2
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package App::gcal;
5 {
6 $App::gcal::VERSION = '1.121460';
7 }
8  
9 2 ▶use Class::ReturnValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::ICal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Command Line Interface interface to Google Calendar.
14  
15 # cache the timezone lookup
16 my $localTZ = DateTime::TimeZone->new( name => 'local' );
17  
18 my $gcal;
19  
20  
21 # entry point
22 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
23 0 my ( $args, $username, $password ) = @_;
24  
25 # loop over args
26 0 for my $arg (@$args) {
27  
28 0 my $cal;
29 0 ▶ if ( ( -e $arg ) && ( -r $arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $arg and -r $arg) { } 0 0 0
Condition0%0/3
-e $arg and -r $arg
!ll&&!rl&&r
000
MC/DC0%0/2
-e $arg and -r $arg
-e $arg -r $arg
Truth table0%0/3
A and B
A= -e $argB= -r $arg
ABresult
0X0
100
111
30  
31 # looks like a file
32 0 $cal = _process_file($arg);
33  
34 }
35 else {
36  
37 # give to ICal::QuickAdd
38 0 $cal = _process_text($arg);
39 }
40  
41 0 ▶ if ($cal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cal) { } 0 0 0
42 0 _save_to_gcal( $cal, $username, $password );
43 }
44 else {
45 0 print STDERR $cal->error_message . "\n";
46 }
47 }
48 }
49  
50 # process an ics file
51 2 ▶sub _process_file {
Subroutine _process_file: called CC=2 SCAR=6.9
52 2 my ($file) = @_;
53  
54 2 my $calendar = Data::ICal->new( filename => $file );
55 2 ▶ unless ($calendar) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calendar) 1 1 2
56 1 return _error("error parsing $file");
57 }
58  
59 1 return $calendar;
60 }
61  
62 # process a text event
63 0 ▶sub _process_text {
Subroutine _process_text: not called CC=3 SCAR=24.8
64 0 my ($text) = @_;
65  
66 0 my $error_msg = 'error parsing text';
67  
68 0 ▶ unless ($text) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text) 0 0 0
69 0 return _error($error_msg);
70 }
71  
72 0 require ICal::Format::Natural;
73 0 my $calendar = ICal::Format::Natural::ical_format_natural($text);
74  
75 0 ▶ unless ( $calendar->isa('Data::ICal') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calendar->isa("Data::ICal")) 0 0 0
76 0 return _error($error_msg);
77 }
78  
79 0 return $calendar;
80 }
81  
82 # save event to Google Calendar
83 0 ▶sub _save_to_gcal {
Subroutine _save_to_gcal: not called CC=7 SCAR=40.3
84 0 my ( $cal, $username, $password ) = @_;
85  
86 0 ▶ unless ($gcal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gcal) 0 0 0
87  
88 0 ▶ unless ( $username && $password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($username and $password) 0 0 0
Condition0%0/3
$username and $password
!ll&&!rl&&r
000
MC/DC0%0/2
$username and $password
$username $password
Truth table0%0/3
A and B
A= $usernameB= $password
ABresult
0X0
100
111
89  
90 # get login and password from .netrc
91 0 require Net::Netrc;
92 0 my $netrc = Net::Netrc->lookup('google.com');
93  
94 0 ▶ unless ($netrc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($netrc) 0 0 0
95 0 die(
96 'Error. Could not find your credentials in your .netrc file'
97 );
98 }
99  
100 0 $username = $netrc->login;
101 0 $password = $netrc->password;
102 }
103  
104 # login
105 0 require Net::Google::Calendar;
106 0 $gcal = Net::Google::Calendar->new;
107 0 $gcal->login( $username, $password );
108 }
109  
110 0 for my $entry ( @{ $cal->entries } ) {
111  
112 # create gcal event
113 0 my $event = _create_new_gcal_event($entry);
114  
115 # save
116 0 my $tmp = $gcal->add_entry($event);
117 0 ▶ die "Couldn't add event: $@\n" unless defined $tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp 0 0 0
118 }
119 }
120  
121 # converts Data::ICal to Net::Google::Calendar::Entry
122 1 ▶sub _create_new_gcal_event {
Subroutine _create_new_gcal_event: called CC=3 SCAR=17.0
123 1 my ($entry) = @_;
124  
125 1 require Net::Google::Calendar::Entry;
126 1 require DateTime::Format::ICal;
127  
128 1 my $event = Net::Google::Calendar::Entry->new();
129  
130 1 $event->title( $entry->property('summary')->[0]->value );
131  
132 # ensure the times are in the local timezone
133 1 my $dtstart = DateTime::Format::ICal->parse_datetime(
134 $entry->property('dtstart')->[0]->value );
135 1 $dtstart->set_time_zone($localTZ);
136 0 my $dtend = DateTime::Format::ICal->parse_datetime(
137 $entry->property('dtend')->[0]->value );
138 0 $dtend->set_time_zone($localTZ);
139 0 $event->when( $dtstart, $dtend );
140  
141 0 $event->status('confirmed');
142  
143 # optional
144 0 ▶ if ( $entry->property('description') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->property("description")) 0 0 0
145 0 $event->content( $entry->property('description')->[0]->value );
146 }
147 0 ▶ if ( $entry->property('location') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->property("location")) 0 0 0
148 0 $event->location( $entry->property('location')->[0]->value );
149 }
150  
151 0 return $event;
152 }
153  
154 # return an error
155 1 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
156 1 my $msg = shift;
157  
158 1 my $ret = Class::ReturnValue->new;
159 1 $ret->as_error( errno => 1, message => $msg );
160 1 return $ret;
161 }
162  
163  
164 1;
165  
166 __END__
↑ Summary