Anonymous avatar Anonymous committed 9f0a91a

Added XML::Grammar::Fortune::Synd::App.

Comments (0)

Files changed (3)

XML-Grammar-Fortune-Synd/module/MANIFEST

 Build.PL
 Changes
+lib/XML/Grammar/Fortune/Synd/App.pm
 lib/XML/Grammar/Fortune/Synd/Heap/Elem.pm
 lib/XML/Grammar/Fortune/Synd.pm
+Makefile.PL
 MANIFEST
+META.yml
 README
 t/00-load.t
 t/pod-coverage.t
 t/pod.t
-Makefile.PL
-META.yml

XML-Grammar-Fortune-Synd/module/lib/XML/Grammar/Fortune/Synd/App.pm

+package XML::Grammar::Fortune::Synd::App;
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+XML::Grammar::Fortune::Synd::App - module implementing a command line 
+application to syndicate FortuneXML as Atom/RSS.
+
+=head1 SYNOPSIS
+
+    perl -MXML::Grammar::Fortune::Synd::App -e 'run()' [ARGS] \
+
+=head1 FUNCTIONS
+
+=head2 run()
+
+Call with no arguments to run the application from the commandline.
+
+=cut
+
+use Getopt::Long;
+use File::Spec;
+
+sub run
+{
+    my $dir;
+    my $yaml_data_file;
+    my @xml_files = ();
+    my $atom_output_fn;
+    my $rss_output_fn;
+    my $master_url;
+    my $feed_title;
+    my $feed_tagline;
+    my $feed_author;
+
+    GetOptions(
+        'dir=s' => \$dir,
+        'xml-file=s' => \@xml_files,
+        'yaml-data=s' => \$yaml_data_file,
+        'atom-output=s' => \$atom_output_fn,
+        'rss-output=s' => \$rss_output_fn,
+        'master-url=s' => \$master_url,
+        'title=s' => \$feed_title,
+        'tagline=s' => \$feed_tagline,
+        'author=s' => \$feed_author,
+    );
+
+    
+    my $url_callback = sub {
+        my ($self, $args) = @_;
+
+        my $id_obj = $args->{id_obj};
+
+        my $base_fn = $id_obj->file();
+
+        $base_fn =~ s{\.[^\.]*\z}{}ms;
+
+        return $master_url . $base_fn . ".html" . "#" . $id_obj->id();
+    };
+
+    my $syndicator = XML::Grammar::Fortune::Synd->new(
+        { 
+            xml_files => \@xml_files,
+            url_callback => $url_callback,
+        }
+    );
+
+    my @more_params;
+
+    if ($atom_output_fn)
+    {
+        my (undef, undef, $atom_base) = File::Spec->splitpath($atom_output_fn);
+        push @more_params, (atom_self_link => "$master_url$atom_base");
+    }
+
+    if ($rss_output_fn)
+    {
+        my (undef, undef, $rss_base) = File::Spec->splitpath($rss_output_fn);
+        push @more_params, (rss_self_link => "$master_url$rss_base");
+    }
+
+    my $recent_ids_struct = $syndicator->get_most_recent_ids(
+           {
+                yaml_persistence_file => $yaml_data_file,
+                yaml_persistence_file_out => $yaml_data_file,
+                xmls_dir => $dir,
+                feed_params =>
+                {
+                    title => $feed_title,
+                    'link' => $master_url,
+                    tagline => $feed_tagline,
+                    author => $feed_author,
+                    @more_params,
+                },
+            }
+        );
+
+    if (defined($atom_output_fn))
+    {
+        open my $atom_out, ">", $atom_output_fn;
+        print {$atom_out} $recent_ids_struct->{'feeds'}->{'Atom'}->as_xml();
+        close($atom_out);
+    }
+
+    if (defined($rss_output_fn))
+    {
+        open my $rss20_out, ">", $rss_output_fn;
+        print {$rss20_out} $recent_ids_struct->{'feeds'}->{'rss20'}->as_xml();
+        close($rss20_out);
+    }
+}
+1;
+

XML-Grammar-Fortune-Synd/module/t/00-load.t

 #!perl -T
 
-use Test::More tests => 2;
+use Test::More tests => 3;
 
 BEGIN {
     # TEST
 	use_ok( 'XML::Grammar::Fortune::Synd' );
     # TEST
 	use_ok( 'XML::Grammar::Fortune::Synd::Heap::Elem' );
+    # TEST
+	use_ok( 'XML::Grammar::Fortune::Synd::App' );
 }
 
 diag( "Testing XML::Grammar::Fortune::Synd $XML::Grammar::Fortune::Synd::VERSION, Perl $], $^X" );
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.