Skip to content

Commit

Permalink
Updated support for Mojolicious 7 and modernize helper calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Akron committed Jul 26, 2016
1 parent a256833 commit 17f50b6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 45 deletions.
11 changes: 11 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
0.16 2016-07-26
- Updated to Mojolicious 7.
- Added pubsub->publish helper.
- Added pubsub->subscribe helper.
- Added pubsub->unsubscribe helper.
- Added pubsub->discover helper.
- Deprecated pubsub_publish in favor of pubsub->publish.
- Deprecated pubsub_subscribe in favor of pubsub->subscribe.
- Deprecated pubsub_unsubscribe in favor of pubsub->unsubscribe.
- Deprecated pubsub_discover in favor of pubsub->discover.

0.15 2016-02-29
- Updated to Mojolicious 6.48.

Expand Down
26 changes: 13 additions & 13 deletions Readme.pod
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ Mojolicious::Plugin::PubSubHubbub - Publish and Subscribe with PubSubHubbub

# In Controllers:
# Publish feeds
$c->pubsub_publish(
$c->pubsub->publish(
'https://sojolicio.us/blog.atom',
'https://sojolicio.us/activity.atom'
);

# Subscribe to a feed
$c->pubsub_subscribe(
$c->pubsub->subscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us'
);

# Discover a resource
my ($topic, $hub) = $c->pubsub_discover('http://sojolicio.us/');
my ($topic, $hub) = $c->pubsub->discover('http://sojolicio.us/');
if ($topic && $hub) {
$c->pubsub_subscribe( topic => $topic, hub => $hub );
$c->pubsub->subscribe( topic => $topic, hub => $hub );
};

# Unsubscribe from a feed
$c->pubsub_unsubscribe(
$c->pubsub->unsubscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us'
);
Expand Down Expand Up @@ -144,19 +144,19 @@ called C<pubsub-callback>.

=head1 HELPERS

=head2 pubsub_discover
=head2 pubsub->discover

# In Controllers
my ($topic, $hub) = $c->pubsub_discover('http://sojolicio.us/');
my ($topic, $hub) = $c->pubsub->discover('http://sojolicio.us/');

Discover a topic feed and a hub based on a URI.
The discovery heuristics may change without notification.


=head2 pubsub_publish
=head2 pubsub->publish

# In Controllers
my $success = $c->pubsub_publish(
my $success = $c->pubsub->publish(
'my_feed', # named route
'/feed.atom', # relative paths
'https://sojolicio.us/feed.atom' # absolute URIs
Expand All @@ -167,10 +167,10 @@ Supports endpoints, named routes, relative paths and absolute URIs.
Returns a true value on success.


=head2 pubsub_subscribe
=head2 pubsub->subscribe

# In Controllers
if ($c->pubsub_subscribe(
if ($c->pubsub->subscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us',
lease_seconds => 123456
Expand All @@ -195,10 +195,10 @@ if an error occured. If called in an array context, the
hub's response message body is returned additionally.


=head2 pubsub_unsubscribe
=head2 pubsub->unsubscribe

# In Controllers
if ($c->pubsub_unsubscribe(
if ($c->pubsub->unsubscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us'
)) {
Expand Down
12 changes: 6 additions & 6 deletions examples/pubsubapp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# For debugging, please set mode and logging level below
#
# -------------------------------------
# Copyright (C) 2011-2013, Nils Diewald
# Copyright (C) 2011-2016, Nils Diewald
# http://nils-diewald.de/
# -------------------------------------
#
Expand Down Expand Up @@ -283,7 +283,7 @@ sub _strip_html {
s!^(?:<br\s*/>)+!!;

};
return b($string)->squish;
return b($string);
};


Expand Down Expand Up @@ -672,7 +672,7 @@ get '/topic/:id/unsubscribe' => [id => qr/\d+/] => sub {
delete $feed->{mode};

# Unsubscription successful
if ($c->pubsub_unsubscribe( %$feed )) {
if ($c->pubsub->unsubscribe( %$feed )) {
$msg = 'You unsubscribed from ' . $feed->{topic};
}

Expand Down Expand Up @@ -732,7 +732,7 @@ any '/topic/subscribe' => sub {
$new_param{secret} = $secret if $secret;

# Subscribe to new feed
if ($c->pubsub_subscribe( %new_param )) {
if ($c->pubsub->subscribe( %new_param )) {
$c->flash(message => 'You subscribed to ' . $topic);
}

Expand All @@ -757,7 +757,7 @@ get '/discover' => sub {
return $c->render_not_found unless $uri;

# Discover uri
my ($topic, $hub) = $c->pubsub_discover($uri);
my ($topic, $hub) = $c->pubsub->discover($uri);

# Set information
$c->flash(hub => $hub) if $hub;
Expand Down Expand Up @@ -933,7 +933,7 @@ CONTENT

# Notify hub for new content
my $msg = 'You posted a new entry';
if ($c->pubsub_publish('atom')) {
if ($c->pubsub->publish('atom')) {
$msg .= ' and notified '. $c->endpoint('pubsub-hub') . ' about ' . $topic;
};

Expand Down
50 changes: 35 additions & 15 deletions lib/Mojolicious/Plugin/PubSubHubbub.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use Mojo::Base 'Mojolicious::Plugin';
use Mojo::UserAgent;
use Mojo::DOM;
use Mojo::ByteStream 'b';
use Mojo::Util qw/secure_compare hmac_sha1_sum/;
use Mojo::Util qw/secure_compare hmac_sha1_sum deprecated/;

our $VERSION = '0.15';
our $VERSION = '0.16';

# Todo:
# - Make everything async (top priority)
# - Maybe allow something like ->feed_to_json (look at superfeedr)
# - Test ->discover

# Default lease seconds before automatic subscription refreshing
has 'lease_seconds' => ( 9 * 24 * 60 * 60 );
Expand Down Expand Up @@ -101,20 +102,39 @@ sub register {
# Add 'publish' helper
$mojo->helper(
pubsub_publish => sub {
deprecated 'pubsub_publish is deprecated in favor of pubsub->publish';
$plugin->publish( @_ );
});

$mojo->helper(
'pubsub.publish' => sub {
$plugin->publish( @_ );
});

# Add 'subscribe' and 'unsubscribe' helper
foreach my $action (qw(subscribe unsubscribe)) {
$mojo->helper(
"pubsub_${action}" => sub {
deprecated "pubsub_${action} is deprecated in favor of pubsub->${action}";
$plugin->_change_subscription(shift, mode => $action, @_);
});

$mojo->helper(
"pubsub.${action}" => sub {
$plugin->_change_subscription(shift, mode => $action, @_);
});
};

# Add 'discovery' helper
$mojo->helper(
pubsub_discover => sub {
deprecated 'pubsub_discover is deprecated in favor of pubsub->discover';
$plugin->discover( @_ )
}
);

$mojo->helper(
'pubsub.discover' => sub {
$plugin->discover( @_ )
}
);
Expand Down Expand Up @@ -944,25 +964,25 @@ Mojolicious::Plugin::PubSubHubbub - Publish and Subscribe with PubSubHubbub
# In Controllers:
# Publish feeds
$c->pubsub_publish(
$c->pubsub->publish(
'https://sojolicio.us/blog.atom',
'https://sojolicio.us/activity.atom'
);
# Subscribe to a feed
$c->pubsub_subscribe(
$c->pubsub->subscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us'
);
# Discover a resource
my ($topic, $hub) = $c->pubsub_discover('http://sojolicio.us/');
my ($topic, $hub) = $c->pubsub->discover('http://sojolicio.us/');
if ($topic && $hub) {
$c->pubsub_subscribe( topic => $topic, hub => $hub );
$c->pubsub->subscribe( topic => $topic, hub => $hub );
};
# Unsubscribe from a feed
$c->pubsub_unsubscribe(
$c->pubsub->unsubscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us'
);
Expand Down Expand Up @@ -1061,19 +1081,19 @@ called C<pubsub-callback>.
=head1 HELPERS
=head2 pubsub_discover
=head2 pubsub->discover
# In Controllers
my ($topic, $hub) = $c->pubsub_discover('http://sojolicio.us/');
my ($topic, $hub) = $c->pubsub->discover('http://sojolicio.us/');
Discover a topic feed and a hub based on a URI.
The discovery heuristics may change without notification.
=head2 pubsub_publish
=head2 pubsub->publish
# In Controllers
my $success = $c->pubsub_publish(
my $success = $c->pubsub->publish(
'my_feed', # named route
'/feed.atom', # relative paths
'https://sojolicio.us/feed.atom' # absolute URIs
Expand All @@ -1084,10 +1104,10 @@ Supports endpoints, named routes, relative paths and absolute URIs.
Returns a true value on success.
=head2 pubsub_subscribe
=head2 pubsub->subscribe
# In Controllers
if ($c->pubsub_subscribe(
if ($c->pubsub->subscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us',
lease_seconds => 123456
Expand All @@ -1112,10 +1132,10 @@ if an error occured. If called in an array context, the
hub's response message body is returned additionally.
=head2 pubsub_unsubscribe
=head2 pubsub->unsubscribe
# In Controllers
if ($c->pubsub_unsubscribe(
if ($c->pubsub->unsubscribe(
topic => 'https://sojolicio.us/feed.atom',
hub => 'https://hub.sojolicio.us'
)) {
Expand Down
22 changes: 11 additions & 11 deletions t/PubSubHubbub.t
Original file line number Diff line number Diff line change
Expand Up @@ -438,41 +438,41 @@ $app->hook(
};
});

ok(!$app->pubsub_subscribe, 'Subscription empty');
ok(!$app->pubsub_subscribe(topic => 'http://sojolicio.us/'),
ok(!$app->pubsub->subscribe, 'Subscription empty');
ok(!$app->pubsub->subscribe(topic => 'http://sojolicio.us/'),
'Subscription invalid');
ok(!$app->pubsub_subscribe(hub => '/hub'),
ok(!$app->pubsub->subscribe(hub => '/hub'),
'Subscription invalid');

ok(!$app->pubsub_subscribe(
ok(!$app->pubsub->subscribe(
topic => 'sojolicio.us',
hub => '/hub'),
'Subscription invalid');

ok($app->pubsub_subscribe(
ok($app->pubsub->subscribe(
topic => 'http://sojolicio.us/blog.xml',
hub => '/hub'
), 'Subscribe');

$request_count++;

ok(
!$app->pubsub_subscribe(
!$app->pubsub->subscribe(
topic => 'http://sojolicio.us/blog/unknown.xml',
hub => '/hub'
), 'Subscribe');

$request_count++;

ok($app->pubsub_unsubscribe(
ok($app->pubsub->unsubscribe(
topic => 'http://sojolicio.us/blog.xml',
hub => '/hub'
), 'Unsubscribe');

$request_count++;

ok(
!$app->pubsub_unsubscribe(
!$app->pubsub->unsubscribe(
topic => 'http://sojolicio.us/blog/unknown.xml',
hub => '/hub'
), 'Unsubscribe');
Expand Down Expand Up @@ -544,17 +544,17 @@ $t->post_ok('/push' => form => { 'hub.mode' => 'unsubscribe',
$request_count = 5;

# Publish
ok(!$app->pubsub_publish, 'Publication empty');
ok(!$app->pubsub->publish, 'Publication empty');

ok($app->pubsub_publish('http://sojolicio.us/blog.xml'),
ok($app->pubsub->publish('http://sojolicio.us/blog.xml'),
'Publication set');

$request_count = 6;

$app->routes->route('/blog.xml')->name('blog_route');
$app->routes->route('/comments.xml')->endpoint('comment_route');

ok($app->pubsub_publish('blog_route','comment_route'),
ok($app->pubsub->publish('blog_route','comment_route'),
'Publication set');


Expand Down

0 comments on commit 17f50b6

Please sign in to comment.