Navigation Menu

Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
doherty committed Jan 18, 2011
1 parent d140b57 commit c68d197
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGES
@@ -1,4 +1,4 @@
Revision history for Perl module WWW-Shorten-IsGd

{{$NEXT}}
*
* First released version
56 changes: 55 additions & 1 deletion MANIFEST.SKIP
@@ -1,4 +1,58 @@
#!include_default

#!start included /home/mike/perl5/perlbrew/perls/perl-5.12.2/lib/5.12.2/ExtUtils/MANIFEST.SKIP
# Avoid version control files.
\bRCS\b
\bCVS\b
\bSCCS\b
,v$
\B\.svn\b
\B\.git\b
\B\.gitignore\b
\b_darcs\b
\B\.cvsignore$

# Avoid VMS specific MakeMaker generated files
\bDescrip.MMS$
\bDESCRIP.MMS$
\bdescrip.mms$

# Avoid Makemaker generated and utility files.
\bMANIFEST\.bak
\bMakefile$
\bblib/
\bMakeMaker-\d
\bpm_to_blib\.ts$
\bpm_to_blib$
\bblibdirs\.ts$ # 6.18 through 6.25 generated this

# Avoid Module::Build generated and utility files.
\bBuild$
\b_build/
\bBuild.bat$
\bBuild.COM$
\bBUILD.COM$
\bbuild.com$

# Avoid temp and backup files.
~$
\.old$
\#$
\b\.#
\.bak$
\.tmp$
\.#
\.rej$

# Avoid OS-specific files/dirs
# Mac OSX metadata
\B\.DS_Store
# Mac OSX SMB mount metadata files
\B\._

# Avoid Devel::Cover files.
\bcover_db\b
#!end included /home/mike/perl5/perlbrew/perls/perl-5.12.2/lib/5.12.2/ExtUtils/MANIFEST.SKIP


# Specific to this project
^dist\.ini$
Expand Down
56 changes: 56 additions & 0 deletions README
@@ -0,0 +1,56 @@
NAME
WWW::Shorten::IsGd - Shorten (or lengthen) URLs with http://is.gd

VERSION
version 0.001

SYNOPSIS
use WWW::Shorten::IsGd;

my $url = q{http://averylong.link/wow?thats=really&really=long};
my $short_url = makeashorterlink($url);
my $long_url = makealongerlink($short_url); # eq $url

DESCRIPTION
A Perl interface to the web site <http://is.gd>. is.gd simply maintains
a database of long URLs, each of which has a unique identifier.

Functions
makeashorterlink
The function "makeashorterlink" will call the is.gd web site passing it
your long URL and will return the shortened link.

makealongerlink
The function "makealongerlink" does the reverse. "makealongerlink" will
accept as an argument either the full TinyURL URL or just the TinyURL
identifier.

If anything goes wrong, then either function will return "undef".

AVAILABILITY
The latest version of this module is available from the Comprehensive
Perl Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a
CPAN site near you, or see
<http://search.cpan.org/dist/WWW-Shorten-IsGd/>.

The development version lives at
<http://github.com/doherty/WWW-Shorten-IsGd> and may be cloned from
<git://github.com/doherty/WWW-Shorten-IsGd.git>. Instead of sending
patches, please fork this project using the standard git and github
infrastructure.

BUGS AND LIMITATIONS
No bugs have been reported.

Please report any bugs or feature requests through the web interface at
<http://github.com/doherty/WWW-Shorten-IsGd/issues>.

AUTHOR
Mike Doherty <doherty@cpan.org>

COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Mike Doherty.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

70 changes: 69 additions & 1 deletion lib/WWW/Shorten/IsGd.pm
@@ -1,8 +1,76 @@
use strict;
use warnings;
use 5.006;

package WWW::Shorten::IsGd;
# ABSTRACT:
# ABSTRACT: Shorten (or lengthen) URLs with http://is.gd

=head1 SYNOPSIS
use WWW::Shorten::IsGd;
my $url = q{http://averylong.link/wow?thats=really&really=long};
my $short_url = makeashorterlink($url);
my $long_url = makealongerlink($short_url); # eq $url
=head1 DESCRIPTION
A Perl interface to the web site L<http://is.gd>. is.gd simply maintains
a database of long URLs, each of which has a unique identifier.
=cut

use base qw( WWW::Shorten::generic Exporter );
our @EXPORT = qw( makeashorterlink makealongerlink );
use Carp;
use URI;

=head1 Functions
=head2 makeashorterlink
The function C<makeashorterlink> will call the is.gd web site passing
it your long URL and will return the shortened link.
=cut

sub makeashorterlink {
my $url = shift or croak 'No URL passed to makeashorterlink';
my $ua = __PACKAGE__->ua();
my $response = $ua->post('http://is.gd/create.php', [
url => $url,
source => 'PerlAPI-' . (defined __PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'),
format => 'simple',
]);
return unless $response->is_success;
my $shorturl = $response->{_content};
return if $shorturl =~ m/Error/;
if ($response->content =~ m{(\Qhttp://is.gd/\E[\w_]+)}) {
return $1;
}
return;
}

=head2 makealongerlink
The function C<makealongerlink> does the reverse. C<makealongerlink>
will accept as an argument either the full TinyURL URL or just the
TinyURL identifier.
If anything goes wrong, then either function will return C<undef>.
=cut

sub makealongerlink {
my $url = shift or croak 'No is.gd key/URL passed to makealongerlink';
my $ua = __PACKAGE__->ua();

$url = "http://is.gd/$url" unless $url =~ m{^https?://}i;
my $response = $ua->get($url);

return unless $response->is_redirect;
return $response->header('Location');
}

1;

Expand Down
25 changes: 25 additions & 0 deletions t/isgd.t
@@ -0,0 +1,25 @@
use strict;
use warnings;
use Test::More tests => 6;

BEGIN {
use_ok 'WWW::Shorten::IsGd';
};

my $longurl = q{http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=louth&sll=53.800651,-4.064941&sspn=33.219383,38.803711&ie=UTF8&hq=&hnear=Louth,+United+Kingdom&ll=53.370272,-0.004034&spn=0.064883,0.075788&z=14};
my $return = makeashorterlink($longurl);
my ($code) = $return =~ /([\w_]+)$/;
my $prefix = 'http://is.gd/';

is ( makeashorterlink($longurl), $prefix.$code, 'make it shorter');
is ( makealongerlink($prefix.$code), $longurl, 'make it longer');
is ( makealongerlink($code), $longurl, 'make it longer by Id',);

{
eval { &makeashorterlink() };
ok($@, 'makeashorterlink fails with no args');
}
{
eval { &makealongerlink() };
ok($@, 'makealongerlink fails with no args');
}

0 comments on commit c68d197

Please sign in to comment.