Catalyst-Plugin-Shorten

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/Shorten.pm  view on Meta::CPAN

	}
	Catalyst::Exception->throw(sprintf 'Unable to find short in params for: %s', $short)
			unless ( $args{allow_missing} );
	undef;
}

sub shorten_redirect {
	my ($c, %args) = @_;
	my $id = inverse($args{$short});
	my $shorten = $c->shorten_get_data($id);
	if (exists $args{cb}) {
		$shorten = $args{cb}->($c, $shorten);
	}
	return $c->res->redirect($shorten->{$ukey}) if $shorten;
	Catalyst::Exception->throw(sprintf 'Unable to find uri to redirect for: %s -> %s', $args{$short}, $id);
}

sub shorten_bijection_set {
	my ($c, @set) = @_;
	bijection_set(@set);
}

sub shorten_offset_set {
	my ($c, @set) = @_;
	offset_set(@set);
}

1;

__END__

=head1 NAME

Catalyst::Plugin::Shorten - The great ancient URI shortner!

=for html
<a href="https://travis-ci.org/ThisUsedToBeAnEmail/Catalyst-Plugin-Shorten"><img src="https://travis-ci.org/ThisUsedToBeAnEmail/Catalyst-Plugin-Shorten.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/r/ThisUsedToBeAnEmail/Catalyst-Plugin-Shorten?branch=master"><img src="https://coveralls.io/repos/ThisUsedToBeAnEmail/Catalyst-Plugin-Shorten/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://metacpan.org/pod/Catalyst-Plugin-Shorten"><img src="https://badge.fury.io/pl/Catalyst-Plugin-Shorten.svg" alt="CPAN version"></a>

=head1 VERSION

Version 0.07

=cut

=head1 SYNOPSIS

	use Catalyst qw/
		Shorten
		Shorten::Store::Dummy
	/;

	sub auto :Path :Args(0) {
		my ($self, $c) = @_;
		$c->shorten_extract; # checks whether the shorten param exists if it does merges the stored params into the request
	}

	........

	sub endpoint :Chained('base') :PathPart('ending') :Args('0') {
		my ($self, $c) = @_;

		my $str = $c->shorten(); # returns bijection references to an ID in the store.
		my $url = $c->shorten(as_uri => 1); # return a url to the current endpoint replacing all params with localhost:300/ending?s=GH
	}

	-------

	use Catalyst qw/
		Shorten
		Shorten::Store::Dummy
	/;

	__PACKAGE__->config(
		......
		'Plugin::Shorten' => {
			set => [qw/c b a ..../],
			map => {
				params => 'data',
				uri => 'url',
				s => 'g'
			}
		}
	);

	package TestApp::Controller::Shorten;

	use Moose;
	use namespace::autoclean;

	BEGIN {
		extends 'Catalyst::Controller';
	}

	sub g :Chained('/') :PathPart('g') :Args('1') {
		my ($self, $c, $cap) = @_;
		$c->shorten_redirect(g => $cap);
	}

	__PACKAGE__->meta->make_immutable;

	1;

=head1 SUBROUTINES/METHODS

=head2 shorten (as_uri => 1|0, uri => URI, store => {} )

Take the current request uri and store, returns an Bijective string.

=cut

=head2 shorten_delete (s => '')

Delete from storage.

=cut

=head2 shorten_extract (params => { s => ...}, cb => sub)

Check for the param (default is 's'), if defined attempt to inverse and then right merge with the current requests params.

This always returns true and you can later access the merged params using -

	$c->req->params;



( run in 0.810 second using v1.01-cache-2.11-cpan-d8267643d1d )