1package WWW::LongURL;
2
3use JSON::Any;
4use LWP::UserAgent;
5use URI::Escape;
6use strict;
7use warnings;
8use base qw(Class::Accessor::Fast);
9
10__PACKAGE__->mk_accessors(qw(apibase useragent format error));
11
12our $VERSION = '0.05';
13
14sub new {
15    my $class = shift;
16
17    my $self = {};
18    bless $self, $class;
19    $self->apibase('http://api.longurl.org/v2/');
20    $self->format('json');
21    $self->useragent('WWW-LongURL/0.05');
22    return $self;
23}
24
25sub expand {
26    my ($self, $url) = @_;
27
28    if (! $url) {
29        $self->error("No URL found to expand");
30        return;
31    }
32
33    my $request  = $self->apibase() . 'expand?url=' . uri_escape($url) . '&format=' . $self->format();
34    my $response = $self->_call_api($request);
35    return if (! $response);
36    if ($response->{'long-url'}) {
37        return $response->{'long-url'};
38    } else {
39        $self->error("Unrecognized response from " . $self->apibase());
40        return;
41    }
42}
43
44sub get_services {
45    my $self = shift;
46
47    my $request = $self->apibase() . 'services?&format=' . $self->format();
48    my $response = $self->_call_api($request);
49    return if (! $response);
50
51    # TODO: handle :utf8 by default
52    return keys (%$response);
53}
54
55sub _call_api {
56    my ($self, $what) = @_;
57
58    my $ua = LWP::UserAgent->new();
59    $ua->agent($self->useragent());
60
61    my $response = $ua->get($what);
62    if ($response->is_success()) {
63        return JSON::Any->jsonToObj($response->decoded_content());
64    } else {
65        $self->error($response->status_line());
66        return;
67    }
68}
69
701;
71
72__END__
73
74=head1 NAME
75
76WWW::LongURL - Perl interface to the LongURL API.
77
78=head1 SYNOPSIS
79
80  use WWW::LongURL;
81
82  my $longurl = WWW::LongURL->new();
83
84  my $expanded_url = $longurl->expand('http://bit.ly/cZcYFn');
85  if (! $expanded_url) {
86      die $longurl->error(), "\n";
87  }
88
89  my @shortening_services = $longurl->get_services();
90  if (! @shortening_services) {
91      die $longurl->error(), "\n";
92  }
93  for my $service (@services) {
94      binmode(STDOUT, ':utf8');
95      print $service, "\n";
96  }
97
98=head1 DESCRIPTION
99
100A simple interface for using the LongURL API to expand shortened URLs.
101
102You can expand a bit.ly URL like so:
103
104  my $url = $longurl->expand($some_bitly_url);
105
106You can retrieve a list of shortening services supported by longurl like so:
107
108  my @services = $longurl->get_services();
109
110=head2 METHODS
111
112=over 4
113
114=item C<new>
115
116Constructor
117
118=item C<expand($url)>
119
120On success, will return the expanded URL from LongURL.  On failure, returns undef.
121
122=item C<get_services>
123
124On success, returns an array of shortening services that longurl.org supports.  On failure, returns undef.
125
126=item C<error>
127
128Returns the last error message.
129
130=back
131
132=head1 SUPPORTED SHORTENING SERVICES
133
134LongURL will expand the URL from over 200 supported shortening services.
135
136See L<http://longurl.org/services>
137
138=head1 REPOSITORY
139
140Development is on-going at:
141https://github.com/kevinspencer/WWW-LongURL
142
143=head1 AUTHOR
144
145Kevin Spencer, kspencer@cpan.org
146
147=head1 COPYRIGHT
148
149Copyright (c) 2011.  Kevin Spencer
150
151This program is free software; you can redistribute it and/or modify it
152under the same terms as Perl itself.
153
154See http://www.perl.com/perl/misc/Artistic.html
155
156=head1 SEE ALSO
157
158L<http://longurl.org/>,
159L<http://longurl.org/api/>
160
161=cut
162