| blib/lib/WWW/Shorten/SnipURL.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 21 | 43 | 48.8 |
| branch | 1 | 16 | 6.2 |
| condition | n/a | ||
| subroutine | 8 | 11 | 72.7 |
| pod | 2 | 2 | 100.0 |
| total | 32 | 72 | 44.4 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package WWW::Shorten::SnipURL; | ||||||
| 2 | |||||||
| 3 | 3 | 3 | 79006 | use 5.006; | |||
| 3 | 8 | ||||||
| 4 | 3 | 3 | 22 | use strict; | |||
| 3 | 3 | ||||||
| 3 | 50 | ||||||
| 5 | 3 | 3 | 9 | use warnings; | |||
| 3 | 3 | ||||||
| 3 | 62 | ||||||
| 6 | |||||||
| 7 | 3 | 3 | 21 | use Carp (); | |||
| 3 | 4 | ||||||
| 3 | 39 | ||||||
| 8 | 3 | 3 | 1282 | use HTTP::Request::Common 'POST'; | |||
| 3 | 40670 | ||||||
| 3 | 196 | ||||||
| 9 | 3 | 3 | 929 | use Try::Tiny qw(try catch); | |||
| 3 | 2971 | ||||||
| 3 | 153 | ||||||
| 10 | |||||||
| 11 | 3 | 3 | 11 | use base qw( WWW::Shorten::generic Exporter ); | |||
| 3 | 5 | ||||||
| 3 | 1289 | ||||||
| 12 | our @EXPORT = qw(makeashorterlink makealongerlink); | ||||||
| 13 | our $VERSION = '2.023'; | ||||||
| 14 | $VERSION = eval $VERSION; | ||||||
| 15 | |||||||
| 16 | sub makeashorterlink { | ||||||
| 17 | 2 | 50 | 2 | 1 | 881 | my $url = shift or Carp::croak('No URL passed to makeashorterlink'); | |
| 18 | 0 | my $ua = __PACKAGE__->ua(); | |||||
| 19 | 0 | my $snipurl = 'http://wwww.snipurl.com/site/index'; | |||||
| 20 | 0 | my $req = POST $snipurl, [url => $url,]; | |||||
| 21 | my $resp | ||||||
| 22 | 0 | 0 | = try { return $ua->request($req); } catch { warn $_; return undef }; | ||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 23 | 0 | 0 | return unless $resp; | ||||
| 24 | 0 | 0 | return unless $resp->is_success; | ||||
| 25 | |||||||
| 26 | 0 | 0 | if ($resp->content | ||||
| 27 | =~ m| | ||||||
| 28 | ) | ||||||
| 29 | { | ||||||
| 30 | 0 | return $1; | |||||
| 31 | } | ||||||
| 32 | 0 | return; | |||||
| 33 | } | ||||||
| 34 | |||||||
| 35 | sub makealongerlink { | ||||||
| 36 | 0 | 0 | 0 | 1 | my $code = shift | ||
| 37 | or Carp::croak('No SnipURL key / URL passed to makealongerlink'); | ||||||
| 38 | 0 | my $ua = __PACKAGE__->ua(); | |||||
| 39 | 0 | 0 | $code = "http://snipurl.com/$code" unless ($code =~ m|^http://|i); | ||||
| 40 | |||||||
| 41 | 0 | 0 | my $resp = try { return $ua->get($code); } catch { warn $_; return undef }; | ||||
| 0 | |||||||
| 0 | |||||||
| 0 | |||||||
| 42 | 0 | 0 | return unless $resp; | ||||
| 43 | 0 | 0 | return unless $resp->is_redirect; | ||||
| 44 | 0 | return $resp->header('Location'); | |||||
| 45 | } | ||||||
| 46 | |||||||
| 47 | 1; | ||||||
| 48 | |||||||
| 49 | =head1 NAME | ||||||
| 50 | |||||||
| 51 | WWW::Shorten::SnipURL - Perl interface to L |
||||||
| 52 | |||||||
| 53 | =head1 SYNOPSIS | ||||||
| 54 | |||||||
| 55 | use strict; | ||||||
| 56 | use warnings; | ||||||
| 57 | |||||||
| 58 | use WWW::Shorten 'SnipURL'; # recommended | ||||||
| 59 | # use WWW::Shorten::SnipURL; # also available | ||||||
| 60 | |||||||
| 61 | my $long_url = 'http://www.foo.com/bar/'; | ||||||
| 62 | my $short_url = makeashorterlink($long_url); | ||||||
| 63 | |||||||
| 64 | $long_url = makealongerlink($short_url); | ||||||
| 65 | |||||||
| 66 | =head1 DESCRIPTION | ||||||
| 67 | |||||||
| 68 | B |
||||||
| 69 | resulting HTML. | ||||||
| 70 | |||||||
| 71 | * Also, their service has been up and down quite a bit lately. We have disabled | ||||||
| 72 | live tests due to this. | ||||||
| 73 | |||||||
| 74 | * You have been warned. We suggest using another L |
||||||
| 75 | |||||||
| 76 | A Perl interface to the web service L |
||||||
| 77 | database of long URLs, each of which has a unique identifier or | ||||||
| 78 | nickname. For more features, please visit L |
||||||
| 79 | |||||||
| 80 | =head1 Functions | ||||||
| 81 | |||||||
| 82 | L |
||||||
| 83 | |||||||
| 84 | =head2 makeashorterlink | ||||||
| 85 | |||||||
| 86 | my $short = makeashorterlink('http://www.example.com/'); | ||||||
| 87 | |||||||
| 88 | The function C |
||||||
| 89 | your long URL and will return the shorter version. | ||||||
| 90 | |||||||
| 91 | =head2 makealongerlink | ||||||
| 92 | |||||||
| 93 | my $long = makealongerlink('ablkjadf2314sfd'); | ||||||
| 94 | my $long = makealongerlink('http://snipurl.com/ablkjadf2314sfd'); | ||||||
| 95 | |||||||
| 96 | The function C |
||||||
| 97 | will accept as an argument either the full URL or just the identifier. | ||||||
| 98 | |||||||
| 99 | If anything goes wrong, then either function will return C |
||||||
| 100 | |||||||
| 101 | =head1 AUTHOR | ||||||
| 102 | |||||||
| 103 | Shashank Tripathi |
||||||
| 104 | |||||||
| 105 | =head1 CONTRIBUTORS | ||||||
| 106 | |||||||
| 107 | =over | ||||||
| 108 | |||||||
| 109 | =item * | ||||||
| 110 | |||||||
| 111 | Chase Whitener C |
||||||
| 112 | |||||||
| 113 | =item * | ||||||
| 114 | |||||||
| 115 | Dave Cross C |
||||||
| 116 | |||||||
| 117 | =back | ||||||
| 118 | |||||||
| 119 | =head1 COPYRIGHT AND LICENSE | ||||||
| 120 | |||||||
| 121 | See the main L |
||||||
| 122 | |||||||
| 123 | =head1 SEE ALSO | ||||||
| 124 | |||||||
| 125 | L |
||||||
| 126 | |||||||
| 127 | =cut |