File Coverage

blib/lib/WebService/Bitly.pm
Criterion Covered Total %
statement 143 145 98.6
branch 36 60 60.0
condition 50 64 78.1
subroutine 25 25 100.0
pod 13 13 100.0
total 267 307 86.9


line stmt bran cond sub pod time code
1             package WebService::Bitly;
2              
3 2     2   210099 use warnings;
  2         6  
  2         65  
4 2     2   9 use strict;
  2         4  
  2         57  
5 2     2   10 use Carp;
  2         7  
  2         129  
6 2     2   887 use UNIVERSAL::require;
  2         1661  
  2         17  
7              
8             our $VERSION = '0.06';
9              
10 2     2   999 use URI;
  2         8066  
  2         20  
11 2     2   1685 use URI::QueryParam;
  2         1346  
  2         20  
12 2     2   1114 use LWP::UserAgent;
  2         39228  
  2         19  
13 2     2   1171 use JSON;
  2         11946  
  2         11  
14              
15 2     2   1316 use WebService::Bitly::Result::HTTPError;
  2         5  
  2         17  
16              
17 2     2   66 use base qw(Class::Accessor::Fast);
  2         4  
  2         2796  
18              
19             __PACKAGE__->mk_accessors(qw(
20             user_name
21             user_api_key
22             end_user_name
23             end_user_api_key
24             domain
25             version
26              
27             base_url
28             ua
29             ));
30              
31             sub new {
32 14     14 1 28817 my ($class, %args) = @_;
33 14 50 33     108 if (!defined $args{user_name} || !defined $args{user_api_key}) {
34 0         0 croak("user_name and user_api_key are both required parameters.\n");
35             }
36              
37 14   100     85 $args{version} ||= 'v3';
38 14   66     69 $args{ua} ||= LWP::UserAgent->new(
39             env_proxy => 1,
40             timeout => 30,
41             );
42 14   50     29639 $args{base_url} ||= 'http://api.bit.ly/';
43              
44 14         120 return $class->SUPER::new(\%args);
45             }
46              
47             sub shorten {
48 3     3 1 1779 my ($self, $url) = @_;
49 3 100       9 if (!defined $url) {
50 1         21 croak("url is required parameter.\n");
51             }
52              
53 2         8 my $api_url = $self->_api_url("shorten");
54 2 50       8257 $api_url->query_param(x_login => $self->end_user_name) if $self->end_user_name;
55 2 50       475 $api_url->query_param(x_apiKey => $self->end_user_api_key) if $self->end_user_api_key;
56 2 50       242 $api_url->query_param(domain => $self->domain) if $self->domain;
57 2         15 $api_url->query_param(longUrl => $url);
58              
59 2         308 $self->_do_request($api_url, 'Shorten');
60             }
61              
62             sub expand {
63 2     2 1 1431 my ($self, %args) = @_;
64 2   100     11 my $short_urls = $args{short_urls} || undef;
65 2   100     9 my $hashes = $args{hashes} || undef;
66 2 50 66     7 if (!$short_urls && !$hashes) {
67 1         18 croak("either short_urls or hashes is required parameter.\n");
68             }
69              
70 1         5 my $api_url = $self->_api_url("expand");
71 1 50       122 $api_url->query_param(shortUrl => reverse(@$short_urls)) if $short_urls;
72 1 50       154 $api_url->query_param(hash => reverse(@$hashes)) if $hashes;
73              
74 1         176 $self->_do_request($api_url, 'Expand');
75             }
76              
77             sub validate {
78 1     1 1 741 my ($self) = @_;
79              
80 1         8 my $api_url = $self->_api_url("validate");
81 1         130 $api_url->query_param(x_login => $self->end_user_name);
82 1         125 $api_url->query_param(x_apiKey => $self->end_user_api_key);
83              
84 1         143 $self->_do_request($api_url, 'Validate');
85             }
86              
87             sub set_end_user_info {
88 1     1 1 3249 my ($self, $end_user_name, $end_user_api_key) = @_;
89              
90 1 50 33     15 if (!defined $end_user_name || !defined $end_user_api_key) {
91 0         0 croak("end_user_name and end_user_api_key are both required parameters.\n");
92             }
93              
94 1         9 $self->end_user_name($end_user_name);
95 1         17 $self->end_user_api_key($end_user_api_key);
96              
97 1         20 return $self;
98             }
99              
100             sub clicks {
101 2     2 1 1270 my ($self, %args) = @_;
102 2   100     9 my $short_urls = $args{short_urls} || undef;
103 2   100     10 my $hashes = $args{hashes} || undef;
104 2 50 66     7 if (!$short_urls && !$hashes) {
105 1         22 croak("either short_urls or hashes is required parameter.\n");
106             }
107              
108 1         5 my $api_url = $self->_api_url("clicks");
109 1 50       115 $api_url->query_param(shortUrl => reverse(@$short_urls)) if $short_urls;
110 1 50       163 $api_url->query_param(hash => reverse(@$hashes)) if $hashes;
111              
112 1         172 $self->_do_request($api_url, 'Clicks');
113             }
114              
115             sub referrers {
116 3     3 1 2143 my ($self, %args) = @_;
117 3   100     12 my $short_url = $args{short_url} || '';
118 3   100     12 my $hash = $args{hash} || '';
119              
120 3 100 75     17 unless ($short_url xor $hash) {
121 2         27 croak("please input either short_url or hash, not both.");
122             }
123              
124 1         5 my $api_url = $self->_api_url("referrers");
125 1 50       87 $api_url->query_param(shortUrl => $short_url) if $short_url;
126 1 50       126 $api_url->query_param(hash => $hash) if $hash;
127              
128 1         3 $self->_do_request($api_url, 'Referrers');
129             }
130              
131             sub countries {
132 3     3 1 2612 my ($self, %args) = @_;
133 3   100     15 my $short_url = $args{short_url} || '';
134 3   100     14 my $hash = $args{hash} || '';
135              
136 3 100 75     20 unless ($short_url xor $hash) {
137 2         31 croak("please input either short_url or hash, not both.");
138             }
139              
140 1         6 my $api_url = $self->_api_url("countries");
141 1 50       159 $api_url->query_param(shortUrl => $short_url) if $short_url;
142 1 50       13 $api_url->query_param(hash => $hash) if $hash;
143              
144 1         137 $self->_do_request($api_url, 'Countries');
145             }
146              
147             sub clicks_by_minute {
148 2     2 1 1651 my ($self, %args) = @_;
149 2   100     12 my $short_urls = $args{short_urls} || undef;
150 2   50     12 my $hashes = $args{hashes} || undef;
151 2 50 66     13 if (!$short_urls && !$hashes) {
152 1         25 croak("either short_urls or hashes is required parameter.\n");
153             }
154              
155 1         5 my $api_url = $self->_api_url("clicks_by_minute");
156 1 50       156 $api_url->query_param(shortUrl => reverse(@$short_urls)) if $short_urls;
157 1 50       187 $api_url->query_param(hash => reverse(@$hashes)) if $hashes;
158              
159 1         5 $self->_do_request($api_url, 'ClicksByMinute');
160             }
161              
162             sub clicks_by_day {
163 2     2 1 1499 my ($self, %args) = @_;
164 2   100     11 my $short_urls = $args{short_urls} || undef;
165 2   100     10 my $hashes = $args{hashes} || undef;
166 2 50 66     11 if (!$short_urls && !$hashes) {
167 1         19 croak("either short_urls or hashes is required parameter.\n");
168             }
169              
170 1         5 my $api_url = $self->_api_url("clicks_by_day");
171 1 50       135 $api_url->query_param(shortUrl => reverse(@$short_urls)) if $short_urls;
172 1 50       183 $api_url->query_param(hash => reverse(@$hashes)) if $hashes;
173              
174 1         181 $self->_do_request($api_url, 'ClicksByDay');
175             }
176              
177             sub bitly_pro_domain {
178 2     2 1 1452 my ($self, $domain) = @_;
179 2 100       8 if (!$domain) {
180 1         24 croak("domain is required parameter.\n");
181             }
182              
183 1         5 my $api_url = $self->_api_url("bitly_pro_domain");
184 1         132 $api_url->query_param(domain => $domain);
185              
186 1         135 $self->_do_request($api_url, 'BitlyProDomain');
187             }
188              
189             sub lookup {
190 2     2 1 1219 my ($self, $urls) = @_;
191 2 100       8 if (!$urls) {
192 1         16 croak("urls is required parameter.\n");
193             }
194              
195 1         4 my $api_url = $self->_api_url("lookup");
196 1         120 $api_url->query_param(url => reverse(@$urls));
197              
198 1         231 $self->_do_request($api_url, 'Lookup');
199             }
200              
201             sub info {
202 2     2 1 1300 my ($self, %args) = @_;
203 2   100     8 my $short_urls = $args{short_urls} || undef;
204 2   100     8 my $hashes = $args{hashes} || undef;
205 2 50 66     11 if (!$short_urls && !$hashes) {
206 1         19 croak("either short_urls or hashes is required parameter.\n");
207             }
208              
209 1         4 my $api_url = $self->_api_url("info");
210 1 50       107 $api_url->query_param(shortUrl => reverse(@$short_urls)) if $short_urls;
211 1 50       164 $api_url->query_param(hash => reverse(@$hashes)) if $hashes;
212              
213 1         157 $self->_do_request($api_url, 'Info');
214             }
215              
216             sub _do_request {
217 12     12   31 my ($self, $url, $result_class) = @_;
218              
219 12         58 $url->query_param(login => $self->user_name);
220 12         2333 $url->query_param(apiKey => $self->user_api_key);
221 12         2525 $url->query_param(format => 'json');
222              
223 12         2599 my $response = $self->ua->get($url);
224              
225 12 100       34101 if (!$response->is_success) {
226 1         15 return WebService::Bitly::Result::HTTPError->new({
227             status_code => $response->code,
228             status_txt => $response->message,
229             });
230             }
231              
232 11         145 $result_class = 'WebService::Bitly::Result::' . $result_class;
233 11         112 $result_class->require;
234              
235 11         137 my $bitly_response = from_json($response->content);
236 11         675 return $result_class->new($bitly_response);
237             }
238              
239             sub _api_url {
240 12     12   28 my ($self, $api_name) = @_;
241 12         52 return URI->new($self->base_url . $self->version . "/". $api_name);
242             }
243              
244             1;
245              
246             __END__;