File Coverage

blib/lib/App/PM/Website/Command.pm
Criterion Covered Total %
statement 30 79 37.9
branch 0 22 0.0
condition 0 18 0.0
subroutine 10 19 52.6
pod 2 9 22.2
total 42 147 28.5


line stmt bran cond sub pod time code
1             package App::PM::Website::Command;
2             {
3             $App::PM::Website::Command::VERSION = '0.131611';
4             }
5             #use App::Cmd::Setup -command;
6 2     2   13 use strict;
  2         3  
  2         63  
7 2     2   12 use warnings;
  2         4  
  2         51  
8 2     2   9 use base 'App::Cmd::Command';
  2         3  
  2         1853  
9 2     2   3577 use Config::YAML;
  2         19257  
  2         69  
10 2     2   1753 use POSIX qw(strftime);
  2         14390  
  2         14  
11 2     2   6515 use Data::Dumper;
  2         17022  
  2         177  
12 2     2   2191 use Date::Parse qw(str2time);
  2         121347  
  2         161  
13 2     2   2293 use DateTime::Format::Strptime;
  2         928241  
  2         132  
14 2     2   22 use DateTime;
  2         3  
  2         38  
15 2     2   1979 use Lingua::EN::Numbers::Ordinate qw(ordinate);
  2         922  
  2         1711  
16              
17             #ABSTRACT: Parent class for App::PM::Website commands
18              
19             sub opt_spec
20             {
21 0     0 1   my ( $class, $app ) = @_;
22             return (
23 0           $class->options($app),
24             [ 'config-file=s' => "path to configuration file",
25             { required => 1, default => "config/pm-website.yaml"}],
26             [],
27             [ 'help|h!' => "show this help" ],
28             [ 'dry-run|n!' => "take no action" ],
29             [ 'verbose|v+' => "increase verbosity" ],
30             );
31             }
32              
33             sub validate_args
34             {
35 0     0 1   my ( $self, $opt, $args ) = @_;
36 0 0         die $self->_usage_text if $opt->{help};
37 0           $self->validate_config( $opt, $args );
38 0           $self->validate( $opt, $args );
39             }
40              
41             sub validate_required_dir
42             {
43 0     0 0   my ($self, $opt, $dir) = @_;
44 0           my $c = $self->{config}{config}{website};
45 0   0       $opt->{$dir} ||= $c->{$dir};
46 0 0         die $self->usage_error("$dir is required")
47             if !$opt->{$dir};
48              
49 0 0         die $self->usage_error(
50             "$dir does not exist: $opt->{$dir}")
51             if !-d $opt->{$dir};
52              
53 0           return 1
54             }
55             sub validate_or_create_dir
56             {
57 0     0 0   my ($self, $opt, $dir) = @_;
58 0           my $c = $self->{config}{config}{website};
59 0   0       $opt->{$dir} ||= $c->{$dir};
60              
61 0 0         die $self->usage_error("$dir is required")
62             if !$opt->{$dir};
63              
64 0 0         if ( ! -d $opt->{$dir} )
65             {
66 0 0         print "creating build dir: $opt->{$dir}\n"
67             if $opt->{verbose};
68 0 0         if ( !$opt->{dry_run} )
69             {
70 0 0         mkdir( $opt->{$dir} )
71             or die $self->usage_error(
72             "failed to make output directory $opt->{$dir} : $!");
73             }
74             }
75              
76 0           return 1
77             }
78              
79              
80             sub validate_config
81             {
82 0     0 0   my ( $self, $opt, $args ) = @_;
83 0 0         $self->{config} = Config::YAML->new( config => $opt->{config_file} )
84             or die $self->usage_error("failed to open configuration file: $!");
85             }
86              
87             sub meetings
88             {
89 0     0 0   my $self = shift;
90 0           my $meetings = $self->{config}->get_meetings;
91 0           my $strp = new DateTime::Format::Strptime(
92             pattern => '%Y-%b-%d',
93             locale => 'en',
94             );
95 0           my $strp_std = new DateTime::Format::Strptime(
96             pattern => '%A %B %e, %Y',
97             locale => 'en',
98             );
99 0           my $strp_pretty = new DateTime::Format::Strptime(
100             pattern => '%A the %e',
101             locale => 'en',
102             );
103              
104 0           for my $meeting (@$meetings)
105             {
106 0   0       $meeting->{epoch} ||= str2time( $meeting->{event_date}, 'PST' );
107 0           my $dt = DateTime->from_epoch( epoch => $meeting->{epoch} );
108 0           $meeting->{dt} = $dt;
109 0   0       $meeting->{ds1} ||= $strp->format_datetime($dt);
110 0   0       $meeting->{ds_std} ||= $strp_std->format_datetime($dt);
111 0           my $pretty = $strp_pretty->format_datetime($dt);
112 0           $pretty =~ s/(\d+) \s* $/ordinate($1)/ex;
  0            
113 0           $meeting->{event_date_pretty} = $pretty;
114             }
115 0           sort { $b->{epoch} <=> $a->{epoch} } @$meetings;
  0            
116             }
117              
118             sub today
119             {
120 0     0 0   my ($class) = @_;
121 0 0         return unless $class;
122 0           $class->date_as_ymd();
123             }
124              
125             sub yesterday
126             {
127 0     0 0   my ($class) = @_;
128 0 0         return unless $class;
129 0           $class->date_as_ymd( time - 24 * 60 * 60 );
130             }
131              
132             sub date_as_ymd
133             {
134 0     0 0   my ( $class, $time ) = @_;
135 0   0       $time ||= time();
136 0           return strftime( "%Y-%m-%d", localtime($time) );
137             }
138              
139             1;
140              
141             __END__