| Net-Lujoyglamour documentation | Contained in the Net-Lujoyglamour distribution. |
Net::Lujoyglamour - Create short URLs with luxury and glamour
This document describes Net::Lujoyglamour version 0.0.5.1.9
use Net::Lujoyglamour qw(is_valid);
#Deploy database
my $dsn = 'dbi:SQLite:dbname=:memory:';
my $schema = Net::Lujoyglamour->connect($dsn);
$schema->deploy({ add_drop_tables => 1});
#Most straighforward way to use
if ( is_valid( "shortie" ) {
$schema->create_new_short( "very.long.url/like/this",
"shortie" );
}
$schema->create_new_short( "even.longer.url/this/and/that" );
Model/Control part of a framework intended for creating short
URLs. Inherits from DBIx::Class; adds functionality for
creating a keeping a table of long/short URLs. Funky name comes
from the novel lujoyglamour.net , in Spanish, which I also
wrote (and obtained a literary price), and you can check out athttp://lujoyglamour.es or buy athttp://compra.lujoyglamour.net.
The module has been tested with SQLite and MySQL, and is in fact
running using both. You just need to change the $dsn for using
either. It hasn't been tested using other databases; would be
grateful if anybody tried/patched for these.
Actually, inherited from DBIx::Class, but useful here too
Also inherited, used for creating the database from the schema description in the class
Check for requested short URL validity; only alphanumeric characters are allowed
Generates a random URL with the character limitation set. It checks URLs with increasing size, until it finds one.
Creates and inserts into the database a new short URL, optionally using a requested short URL.
Returns the long URL that corresponds to the short one
Url space exhausted!! No more URLs availables. You probably have a very successful URL shortener site. You already used up all available short addresses. Time to expand your database!
Invalid short URL $want_shortThe requested URL is invalid
You need to have a database system ready and able. If there's none
installed, SQLite will do nicely, but do check reading/writing
permissions over the file. Use the app/lg-deploy script,
modified for your environment, to create the initial environment.
There are some template and CGI examples under the app/ directory of the
distribution. Check them for usage, or use your own. You only need
to keep the filenames of the .html files and copy them in the
same directory as the CGI. A htaccess file is also provided as
an example; you will have to modify it with the actual file names
you will be using.
DBIx::Class, String::Random, Regexp::Common and drivers for database system. If you want to use the provided WebApp, you will need CGI::Application and corresponding drivers.
None reported.
Other than the limited space available, for the time being there are none.
Please report any bugs or feature requests to
bug-net-lujoyglamour@rt.cpan.org, or through the web interface at
http://rt.cpan.org.
The module is used with a SQLite database at the sitehttp://lugl.info/, and with MySQL at http://l-g.me (in Spanish). Any comments and suggestions are welcome. If you decide to run your URL shortener on this, please let me know.
Other URL shortening modules you might want to check out are CGI::Shorten, WWW::Shorten::MakeAShorterLink and WWW::Shorten, which is rather an interface for available URL shortening services.
There is an example app at the aptly named app directory,
retrieve it from your CPAN directory or from the CPAN website.
This version includes the PDF of the novel lujoyglamour.net,
distributed with a CC licence, in Spanish. Look under the t/
directory, and read it using your favorite PDF browser or ebook
reader. If you publish a review, please trackbackhttp://lujoyglamour.es .
CPANTS tester Sebastian Woetzel helped me fix a bug in my tests scripts.
JJ Merelo <jj@merelo.net>
Copyright (c) 2009, 2010, JJ Merelo <jj@merelo.net>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
| Net-Lujoyglamour documentation | Contained in the Net-Lujoyglamour distribution. |
package Net::Lujoyglamour; use warnings; use strict; use Carp; # 1 == l 9==g Double luxury and glamour use version; our $VERSION = qv('0.0.6.1.9'); use base qw/DBIx::Class::Schema Exporter/; use String::Random qw(random_regex); use Regexp::Common; # for %RE our @EXPORT_OK = qw(is_valid); our $valid_short_urls = '\w'; our $equivalent_pattern = '[A-Za-z0-9_]'; our $short_url_size= 16; __PACKAGE__->load_namespaces(); sub create_new_short { my $schema = shift; my $long_url = shift || croak "What? No URL?\n"; if ( $long_url !~ m{^http://} ) { $long_url = "http://$long_url"; } if ( $long_url =~ m{($RE{'URI'}{'HTTP'}#?[/\w]*)} ) { $long_url = $1; } else { croak "Invalid URL $long_url" ; } my $want_short = shift; my $url_rs = $schema->resultset('Url'); if ( $want_short ) { my $is_there_long = $url_rs->single( { shortu => $want_short } ); croak "Short URL '$want_short' already in use" if $is_there_long; } my $short_url = $url_rs->single( { longu => $long_url } ); if ( !$short_url ) { # Doesn't exist, create my $new_pair; if ( $want_short ) { croak "Invalid short URL $want_short" if !is_valid($want_short); $short_url = $want_short; } else { $short_url = $schema->generate_candidate_url; } $new_pair = $url_rs->new( { shortu => $short_url, longu => $long_url } ); $new_pair->insert; } else { $short_url = $short_url->short_url } if ( $short_url ne '' ) { return $short_url; } else { croak "Something along the way went wrong, no short URL obtained for $long_url"; } } sub is_valid { my $string = shift; return $string =~ /^[$valid_short_urls]{1,$short_url_size}$/; } sub generate_candidate_url { my $schema = shift; my $candidate_url; my $url_rs = $schema->resultset('Url'); my $i = 1; do { $candidate_url = random_regex($equivalent_pattern.'{1,'.$i.'}'); } while ( $url_rs->find( { shortu => $candidate_url } ) && ($i++ <= $short_url_size ) ); if ( $i > $short_url_size ) { croak "Url space exhausted!!"; } if ( $candidate_url eq '' ) { croak "Couldn't generate a candidate URL!" } return $candidate_url; } sub get_long_for { my $schema = shift; my $short_url = shift; my $result = $schema->resultset('Url')->single({shortu => $short_url}); if ( defined $result ) { return $result->long_url; } else { return; } } "lujo and glamour all over"; # Magic true value required at end of module __END__