package WWW::Shorten::SnipURL;

use 5.006;
use strict;
use warnings;

use Carp ();
use HTTP::Request::Common 'POST';
use Try::Tiny qw(try catch);

use base qw( WWW::Shorten::generic Exporter );
our @EXPORT  = qw(makeashorterlink makealongerlink);
our $VERSION = '2.023';

sub makeashorterlink {
    my $url     = shift or Carp::croak('No URL passed to makeashorterlink');
    my $ua      = __PACKAGE__->ua();
    my $snipurl = '';
    my $req     = POST $snipurl, [url => $url,];
    my $resp
        = try { return $ua->request($req); } catch { warn $_; return undef };
    return unless $resp;
    return unless $resp->is_success;

    if ($resp->content
        =~ m|<input name="SNIPPED" class="snipped textsnipped" type="text" value="(\w+)"|
        return $1;

sub makealongerlink {
    my $code = shift
        or Carp::croak('No SnipURL key / URL passed to makealongerlink');
    my $ua = __PACKAGE__->ua();
    $code = "$code" unless ($code =~ m|^http://|i);

    my $resp = try { return $ua->get($code); } catch { warn $_; return undef };
    return unless $resp;
    return unless $resp->is_redirect;
    return $resp->header('Location');


=head1 NAME

WWW::Shorten::SnipURL - Perl interface to L<>


  use strict;
  use warnings;

  use WWW::Shorten 'SnipURL'; # recommended
  # use WWW::Shorten::SnipURL; # also available

  my $long_url = '';
  my $short_url = makeashorterlink($long_url);

  $long_url  = makealongerlink($short_url);


B<WARNING:> L<> does not provide an API.  We must scrape the
resulting HTML.

* Also, their service has been up and down quite a bit lately.  We have disabled
live tests due to this.

* You have been warned.  We suggest using another L<WWW::Shorten> service.

A Perl interface to the web service L<>. The service maintains a
database of long URLs, each of which has a unique identifier or
nickname. For more features, please visit L<>.

=head1 Functions

L<WWW::Shorten::SnipURL> makes the following functions available.

=head2 makeashorterlink

  my $short = makeashorterlink('');

The function C<makeashorterlink> will call use the web service, passing it
your long URL and will return the shorter version.

=head2 makealongerlink

  my $long = makealongerlink('ablkjadf2314sfd');
  my $long = makealongerlink('');

The function C<makealongerlink> does the reverse. C<makealongerlink>
will accept as an argument either the full URL or just the identifier.

If anything goes wrong, then either function will return C<undef>.

=head1 AUTHOR

Shashank Tripathi <>



=item *

Chase Whitener C<>

=item *

Dave Cross C<>



See the main L<WWW::Shorten> docs.

=head1 SEE ALSO

L<WWW::Shorten>, L<>