File Coverage

blib/lib/XML/Grammar/Fortune/Synd/App.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package XML::Grammar::Fortune::Synd::App;
2              
3 1     1   1071 use strict;
  1         2  
  1         33  
4 1     1   6 use warnings;
  1         2  
  1         34  
5              
6 1     1   5 use base 'Exporter';
  1         2  
  1         78  
7              
8 1     1   5 use vars qw(@EXPORT);
  1         9  
  1         68  
9              
10             our $VERSION = '0.0211';
11              
12             @EXPORT=('run');
13              
14 1     1   1341 use Getopt::Long;
  1         12485  
  1         6  
15 1     1   177 use File::Spec;
  1         2  
  1         20  
16              
17 1     1   62 use XML::Grammar::Fortune::Synd;
  0            
  0            
18              
19             =head1 NAME
20              
21             XML::Grammar::Fortune::Synd::App - module implementing a command line
22             application to syndicate FortuneXML as Atom/RSS.
23              
24             =head1 SYNOPSIS
25              
26             perl -MXML::Grammar::Fortune::Synd::App -e 'run()' [ARGS] \
27              
28             =head1 FUNCTIONS
29              
30             =head2 run()
31              
32             Call with no arguments to run the application from the commandline.
33              
34             =cut
35              
36              
37             sub run
38             {
39             my $dir;
40             my $yaml_data_file;
41             my @xml_files = ();
42             my $atom_output_fn;
43             my $rss_output_fn;
44             my $master_url;
45             my $feed_title;
46             my $feed_tagline;
47             my $feed_author;
48              
49             GetOptions(
50             'dir=s' => \$dir,
51             'xml-file=s' => \@xml_files,
52             'yaml-data=s' => \$yaml_data_file,
53             'atom-output=s' => \$atom_output_fn,
54             'rss-output=s' => \$rss_output_fn,
55             'master-url=s' => \$master_url,
56             'title=s' => \$feed_title,
57             'tagline=s' => \$feed_tagline,
58             'author=s' => \$feed_author,
59             );
60              
61              
62             my $url_callback = sub {
63             my ($self, $args) = @_;
64              
65             my $id_obj = $args->{id_obj};
66              
67             my $base_fn = $id_obj->file();
68              
69             $base_fn =~ s{\.[^\.]*\z}{}ms;
70              
71             return $master_url . $base_fn . ".html" . "#" . $id_obj->id();
72             };
73              
74             my $syndicator = XML::Grammar::Fortune::Synd->new(
75             {
76             xml_files => \@xml_files,
77             url_callback => $url_callback,
78             }
79             );
80              
81             my @more_params;
82              
83             if ($atom_output_fn)
84             {
85             my (undef, undef, $atom_base) = File::Spec->splitpath($atom_output_fn);
86             push @more_params, (atom_self_link => "$master_url$atom_base");
87             }
88              
89             if ($rss_output_fn)
90             {
91             my (undef, undef, $rss_base) = File::Spec->splitpath($rss_output_fn);
92             push @more_params, (rss_self_link => "$master_url$rss_base");
93             }
94              
95             my $recent_ids_struct = $syndicator->calc_feeds(
96             {
97             yaml_persistence_file => $yaml_data_file,
98             yaml_persistence_file_out => $yaml_data_file,
99             xmls_dir => $dir,
100             feed_params =>
101             {
102             title => $feed_title,
103             'link' => $master_url,
104             tagline => $feed_tagline,
105             author => $feed_author,
106             @more_params,
107             },
108             }
109             );
110              
111             if (defined($atom_output_fn))
112             {
113             open my $atom_out, ">", $atom_output_fn;
114             print {$atom_out} $recent_ids_struct->{'feeds'}->{'Atom'}->as_xml();
115             close($atom_out);
116             }
117              
118             if (defined($rss_output_fn))
119             {
120             open my $rss20_out, ">", $rss_output_fn;
121             print {$rss20_out} $recent_ids_struct->{'feeds'}->{'rss20'}->as_xml();
122             close($rss20_out);
123             }
124             }
125             1;
126