File Coverage

blib/lib/WebService/Flattr.pm
Criterion Covered Total %
statement 30 94 31.9
branch 1 12 8.3
condition n/a
subroutine 9 22 40.9
pod 13 13 100.0
total 53 141 37.5


line stmt bran cond sub pod time code
1             package WebService::Flattr;
2             {
3             $WebService::Flattr::VERSION = '0.55';
4             }
5              
6 1     1   972 use strict;
  1         2  
  1         41  
7 1     1   5 use warnings;
  1         2  
  1         36  
8              
9 1     1   6 use JSON 'decode_json';
  1         2  
  1         7  
10 1     1   1057 use LWP::UserAgent ();
  1         54152  
  1         29  
11 1     1   12 use URI ();
  1         2  
  1         15  
12 1     1   812 use URI::QueryParam ();
  1         1557  
  1         17  
13 1     1   829 use URI::Template ();
  1         31225  
  1         91  
14 1     1   849 use WebService::Flattr::Response ();
  1         4  
  1         1079  
15              
16             =head1 NAME
17              
18             WebService::Flattr - An interface to Flattr's social micro-payment API
19              
20             =head1 VERSION
21              
22             version 0.55
23              
24             =head1 SYNOPSIS
25              
26             use WebService::Flattr();
27              
28             my $flattr = WebService::Flattr->new;
29             my $thing = $flattr->thing_exists("http://www.example.com/")->data;
30              
31              
32             =head1 DESCRIPTION
33              
34             This module provides an interface to the L<< http://flattr.com/ >>
35             social micropayment system.
36              
37             Flattr have documented their interface at L<<
38             http://developers.flattr.net/api/ >>.
39              
40             Currently, this module only implements part of Flattr's interface.
41             Future versions will implement more features.
42              
43             =head1 METHODS
44              
45             =head2 Constructor
46              
47             =head3 new
48              
49             my $flattr = WebService::Flattr->new();
50              
51             This returns a C<< WebService::Flattr >> object to call L on.
52              
53             =cut
54              
55             sub new {
56 1     1 1 419 my $class = shift;
57              
58 1         2 my $name = 'WebService::Flattr';
59 1 50       5 if ($WebService::Flattr::VERSION) {
60 1         3 $name .= '/'. $WebService::Flattr::VERSION;
61             }
62 1         11 my $ua = LWP::UserAgent->new(
63             # the space at the end below makes LWP prepend its name and
64             # version number
65             agent => "${name} ",
66             keep_alive => 4,
67             max_redirect => 0, # Avoid auto-redirect on thing_exists() success
68             protocols_allowed => ['https'],
69             );
70              
71 1         7402 return bless {
72             ua => $ua,
73             }, $class;
74             }
75              
76             sub _req {
77 0     0     my $self = shift;
78 0           my $uri = shift;
79              
80 0           my $resp = $self->{ua}->get($uri, Accept => 'application/json');
81              
82 0 0         if ($resp->is_error) {
83 0           die $resp->status_line;
84             }
85              
86 0           return WebService::Flattr::Response->_new({
87             data => decode_json $resp->content,
88             response => $resp,
89             });
90             }
91              
92             =head2 Request Methods
93              
94             The following request methods perform actions against Flattr's API.
95             Each method returns a L<< WebService::Flattr::Response >> object on
96             success and dies on failure.
97              
98             =head3 user_flattrs
99              
100             Takes a list or hash reference containing the mandatory I<< username >>
101             argument and zero or more optional arguments.
102              
103             L<<
104             http://developers.flattr.net/api/resources/flattrs/#list-a-users-flattrs
105             >>
106              
107             =cut
108              
109             sub user_flattrs {
110 0     0 1   my $self = shift;
111 0 0         my $arg = @_ == 1 ? shift : { @_ };
112              
113 0           my $tmpl = "https://api.flattr.com/rest/v2/users/{username}/flattrs";
114 0           my $uri = URI::Template->new($tmpl)->process(username => $arg->{username});
115 0           foreach (keys %$arg) {
116 0           $uri->query_param($_, $arg->{$_});
117             }
118              
119 0           return $self->_req($uri);
120             }
121              
122             =head3 thing_flattrs
123              
124             Takes a list or hash reference containing the mandatory I<< id >>
125             argument and zero or more optional arguments.
126              
127             L<<
128             http://developers.flattr.net/api/resources/flattrs/#list-a-things-flattrs
129             >>
130              
131             =cut
132              
133             sub thing_flattrs {
134 0     0 1   my $self = shift;
135 0 0         my $arg = @_ == 1 ? shift : { @_ };
136              
137 0           my $tmpl = "https://api.flattr.com/rest/v2/things/{id}/flattrs";
138 0           my $uri = URI::Template->new($tmpl)->process(id => $arg->{id});
139 0           foreach (keys %$arg) {
140 0           $uri->query_param($_, $arg->{$_});
141             }
142              
143 0           return $self->_req($uri);
144             }
145              
146             =head3 things_owned_by
147              
148             Takes a list or hash reference containing the mandatory I<< username >>
149             argument and zero or more optional arguments.
150              
151             L<<
152             http://developers.flattr.net/api/resources/things/#list-a-users-things
153             >>
154              
155             =cut
156              
157             sub things_owned_by {
158 0     0 1   my $self = shift;
159 0 0         my $arg = @_ == 1 ? shift : { @_ };
160              
161 0           my $tmpl = "https://api.flattr.com/rest/v2/users/{username}/things";
162 0           my $uri = URI::Template->new($tmpl)->process(username => $arg->{username});
163 0           foreach (keys %$arg) {
164 0           $uri->query_param($_, $arg->{$_});
165             }
166              
167 0           return $self->_req($uri);
168             }
169              
170             =head3 get_thing
171              
172             Takes one argument, the ID of a thing.
173              
174             L<< http://developers.flattr.net/api/resources/things/#get-a-thing >>
175              
176             =cut
177              
178             sub get_thing {
179 0     0 1   my $self = shift;
180 0           my $id = shift;
181              
182 0           my $tmpl = "https://api.flattr.com/rest/v2/things/{id}";
183 0           my $uri = URI::Template->new($tmpl)->process(id => $id);
184              
185 0           return $self->_req($uri);
186             }
187              
188             =head3 get_things
189              
190             Takes a list of IDs of things to retrieve.
191              
192             L<<
193             http://developers.flattr.net/api/resources/things/#get-multiple-things
194             >>
195              
196             =cut
197              
198             sub get_things {
199 0     0 1   my $self = shift;
200 0           my $ids = join ",", @_;
201              
202 0           my $uri = URI->new("https://api.flattr.com/rest/v2/things/${ids}");
203              
204 0           return $self->_req($uri);
205             }
206              
207             =head3 thing_exists
208              
209             Takes one argument, the URL of a thing.
210              
211             L<<
212             http://developers.flattr.net/api/resources/things/#check-if-a-thing-exists
213             >>.
214              
215             =cut
216              
217             sub thing_exists {
218 0     0 1   my $self = shift;
219 0           my $url = shift;
220              
221 0           my $tmpl = "https://api.flattr.com/rest/v2/things/lookup/?url={url}";
222 0           my $uri = URI::Template->new($tmpl)->process(url => $url);
223              
224 0           return $self->_req($uri);
225             }
226              
227             =head3 search_things
228              
229             Takes optional arguments either as a list or a hash reference.
230              
231             L<< http://developers.flattr.net/api/resources/things/#search-things >>
232              
233             =cut
234              
235             sub search_things {
236 0     0 1   my $self = shift;
237 0 0         my $arg = @_ == 1 ? shift : { @_ };
238              
239 0           my $tmpl = "https://api.flattr.com/rest/v2/things/search";
240 0           my $uri = URI::Template->new($tmpl)->process;
241 0           foreach (keys %$arg) {
242 0           $uri->query_param($_, $arg->{$_});
243             }
244              
245 0           return $self->_req($uri);
246             }
247              
248             =head3 user
249              
250             Takes one argument, a string containing a username.
251              
252             L<< http://developers.flattr.net/api/resources/users/#get-a-user >>
253              
254             =cut
255              
256             sub user {
257 0     0 1   my $self = shift;
258 0           my $username = shift;
259              
260 0           my $tmpl = "https://api.flattr.com/rest/v2/users/{username}";
261 0           my $uri = URI::Template->new($tmpl)->process(username => $username);
262              
263 0           return $self->_req($uri);
264             }
265              
266             =head3 user_activities
267              
268             Takes one argument, a string containing a username.
269              
270             L<<
271             http://developers.flattr.net/api/resources/activities/#list-an-users-activities
272             >>
273              
274             =cut
275              
276             sub user_activities {
277 0     0 1   my $self = shift;
278 0           my $username = shift;
279              
280 0           my $tmpl = "https://api.flattr.com/rest/v2/users/{username}/activities";
281 0           my $uri = URI::Template->new($tmpl)->process(username => $username);
282              
283 0           return $self->_req($uri);
284             }
285              
286             =head3 categories
287              
288             Takes no arguments.
289              
290             L<<
291             http://developers.flattr.net/api/resources/categories/#list-categories
292             >>
293              
294             =cut
295              
296             sub categories {
297 0     0 1   my $self = shift;
298              
299 0           return $self->_req("https://api.flattr.com/rest/v2/categories");
300             }
301              
302             =head3 languages
303              
304             Takes no arguments.
305              
306             L<<
307             http://developers.flattr.net/api/resources/languages/#list-all-available-languages
308             >>
309              
310             =cut
311              
312             sub languages {
313 0     0 1   my $self = shift;
314              
315 0           return $self->_req("https://api.flattr.com/rest/v2/languages");
316             }
317              
318             =head3 rate_limit
319              
320             Takes no arguments.
321              
322             L<< http://developers.flattr.net/api/#rate-limiting >>
323              
324             =cut
325              
326             sub rate_limit {
327 0     0 1   my $self = shift;
328              
329 0           return $self->_req("https://api.flattr.com/rest/v2/rate_limit");
330             }
331              
332             1;
333             __END__