Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to turn D::P::F into Dancer2::Plugin::Feed #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 17 additions & 33 deletions lib/Dancer/Plugin/Feed.pm → lib/Dancer2/Plugin/Feed.pm
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package Dancer::Plugin::Feed;
package Dancer2::Plugin::Feed;

use Dancer ':syntax';
use Dancer::Plugin;
use Dancer::Exception qw(:all);
use Dancer2 ':syntax';
use Dancer2::Plugin;
use XML::Feed;

#ABSTRACT: Easy to generate feed rss or atom for Dancer applications.
#ABSTRACT: Easy to generate feed rss or atom for Dancer2 applications.

my $ct = {
atom => 'application/atom+xml',
rss => 'application/rss+xml',
};

#Register exception
register_exception('FeedInvalidFormat',
message_pattern => "Unknown format use rss or atom: %s"
);
register_exception('FeedNoFormat',
message_pattern => "Format is missing"
);

my @feed_properties =
qw/format title base link tagline description author id language copyright self_link modified/;
Expand All @@ -27,7 +19,7 @@ my @entries_properties =
qw/title base link content summary category tags author id issued modified enclosure/;

register create_feed => sub {
my ($dsl, %params) = plugin_args(@_);
my ($dsl, %params) = @_;

my $format = _validate_format(\%params);

Expand All @@ -36,7 +28,7 @@ register create_feed => sub {
}elsif($format =~/^rss$/i) {
_create_rss_feed(\%params);
}else{
raise FeedInvalidFormat => $format;
die "Unknown format $format, use rss or atom\n";
}
};

Expand All @@ -53,16 +45,17 @@ register create_rss_feed => sub {
};

sub _validate_format {
my $params = shift;
my( $params, $dsl ) = @_;
my $format = delete $params->{format};

if (!$format) {
my $settings = plugin_setting;
$format = $settings->{format} or raise 'FeedNoFormat';
$format = $settings->{format}
or die "Feed format is missing\n";
}

if ($format !~ /^(?:atom|rss)$/i) {
raise FeedInvalidFormat => $format;
die "Unknown format $format, use rss or atom\n";
}

return $format;
Expand Down Expand Up @@ -109,16 +102,16 @@ sub _create_rss_feed {
_create_feed('RSS', $params);
}

register_plugin for_versions => [1, 2];
register_plugin for_versions => [2];

1;

=encoding UTF-8

=head1 SYNOPSIS

use Dancer;
use Dancer::Plugin::Feed;
use Dancer2;
use Dancer2::Plugin::Feed;
use Try::Tiny;

get '/feed/:format' => sub {
Expand Down Expand Up @@ -208,21 +201,11 @@ This method call B<create_feed> by setting the format to Atom.

This method call B<create_feed> by setting the format to RSS.

=head1 Exception

=over

=item FeedNoFormat

=item FeedInvalidFormat

=back

=head1 CONTRIBUTING

This module is developed on Github at:

L<http://github.com/hobbestigrou/Dancer-Plugin-Feed>
L<http://github.com/hobbestigrou/Dancer2-Plugin-Feed>

Feel free to fork the repo and submit pull requests

Expand All @@ -238,10 +221,11 @@ Please report any bugs or feature requests in github.

You can find documentation for this module with the perldoc command.

perldoc Dancer::Plugin::Feed
perldoc Dancer2::Plugin::Feed

=head1 SEE ALSO

L<Dancer>
L<Dancer2>
L<XML::Feed>
L<XML::Feed::Entry>
L<Dancer::Plugin::Feed>
11 changes: 0 additions & 11 deletions t/00-load.t

This file was deleted.

14 changes: 8 additions & 6 deletions t/01-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use warnings;

use Test::More import => ['!pass'];

use Dancer;
use Dancer::Test;

use lib 't/lib';
use XML::Feed;
use TestApp;

use XML::Feed;
use Dancer2;
use Dancer2::Test apps => [ 'TestApp' ];

plan tests => 32;

Expand All @@ -21,7 +20,7 @@ is $res->{status}, 500, "response for GET /feed is 500";
for my $format (qw/atom rss/) {
for my $route ("/feed/$format", "/other/feed/$format") {
ok ($res = dancer_response(GET => $route));
is ($res->{status}, 200);
is $res->status, 200, "$format - $route";
is ($res->header('Content-Type'), "application/$format+xml");
ok ( $feed = XML::Feed->parse( \$res->{content} ) );
is ( $feed->title, 'TestApp with ' . $format );
Expand All @@ -36,6 +35,9 @@ for my $format (qw/atom rss/) {
$res = dancer_response GET => '/feed/foo';
is $res->{status}, 500, "response for GET /feed/foo is 500";

setting plugins => { Feed => { format => 'atom' } };
{
package TestApp;
setting plugins => { Feed => { format => 'atom' } };
}
ok ($res = dancer_response(GET => '/feed'));
is ($res->header('Content-Type'), 'application/atom+xml');
6 changes: 3 additions & 3 deletions t/lib/TestApp.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package t::lib::TestApp;
package TestApp;

use Dancer;
use Dancer::Plugin::Feed;
use Dancer2;
use Dancer2::Plugin::Feed;

get '/feed' => sub {
create_feed(
Expand Down