Pocket.pm

filter: Statementstmt 37.7% 23 / 61 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 26.6% 4 / 15 Podpod 100% 5 / 5 totaltotal 37.6% 32 / 85 CC 3 SCAR 10.0
CC
3
Cov
35%
start_authentication
6.0
_request
6.0
__ANON__
2.0
CRAP 2.7
« Pocket/Script.pm ↑ Summary
1 package WWW::Pocket;
2 2 our $AUTHORITY = 'cpan:DOY';
3 2 $WWW::Pocket::VERSION = '0.01';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: Wrapper for the Pocket v3 API
6  
7 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11  
12 2 has consumer_key => (
13 is => 'ro',
14 isa => 'Str',
15 required => 1,
16 );
17  
18  
19 has access_token => (
20 is => 'ro',
21 isa => 'Str',
22 lazy => 1,
23 0 ▶ default => sub { die "You must authenticate first." },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 2 predicate => 'has_access_token',
25 writer => '_set_access_token',
26 );
27  
28  
29 2 has username => (
30 is => 'ro',
31 isa => 'Str',
32 predicate => 'has_username',
33 writer => '_set_username',
34 );
35  
36  
37 2 has base_uri => (
38 is => 'ro',
39 isa => 'Str',
40 default => 'https://getpocket.com/',
41 );
42  
43  
44 has api_base_uri => (
45 is => 'ro',
46 isa => 'Str',
47 lazy => 1,
48 default => sub {
49 0 ▶ my $base = $_[0]->base_uri;
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 $base =~ s{/$}{};
51 0 return "$base/v3/"
52 },
53 2 );
54  
55  
56 has auth_base_uri => (
57 is => 'ro',
58 isa => 'Str',
59 lazy => 1,
60 default => sub {
61 0 ▶ my $base = $_[0]->base_uri;
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 $base =~ s{/$}{};
63 0 return "$base/auth/"
64 },
65 2 );
66  
67  
68 has ua => (
69 is => 'ro',
70 isa => 'HTTP::Tiny',
71 lazy => 1,
72 0 ▶ default => sub { HTTP::Tiny->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 2 );
74  
75  
76 sub start_authentication {
77 0 ▶ my $self = shift;
Subroutine start_authentication: not called CC=2 SCAR=17.9
Pod documented
78 0 my ($redirect_uri) = @_;
79  
80 0 ▶ return if $self->has_access_token;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_access_token 0 0 0
81  
82 0 my $response = $self->_request(
83 $self->api_base_uri . 'oauth/request',
84 {
85 consumer_key => $self->consumer_key,
86 redirect_uri => $redirect_uri,
87 },
88 );
89 return (
90 $self->auth_base_uri . "authorize?request_token=$response->{code}&redirect_uri=$redirect_uri",
91 $response->{code},
92 0 );
93 }
94  
95  
96 sub finish_authentication {
97 0 ▶ my $self = shift;
Subroutine finish_authentication: not called CC=1 SCAR=6.9
Pod documented
98 0 my ($code) = @_;
99  
100 0 my $response = $self->_request(
101 $self->api_base_uri . 'oauth/authorize',
102 {
103 consumer_key => $self->consumer_key,
104 code => $code,
105 },
106 );
107  
108 0 $self->_set_access_token($response->{access_token});
109 0 $self->_set_username($response->{username});
110  
111 0 return;
112 }
113  
114  
115 sub add {
116 0 ▶ my $self = shift;
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
117 0 my (%params) = @_;
118 0 return $self->_endpoint_request('add', \%params);
119 }
120  
121  
122 sub modify {
123 0 ▶ my $self = shift;
Subroutine modify: not called CC=1 SCAR=6.9
Pod documented
124 0 my (%params) = @_;
125 0 return $self->_endpoint_request('send', \%params);
126 }
127  
128  
129 sub retrieve {
130 0 ▶ my $self = shift;
Subroutine retrieve: not called CC=1 SCAR=6.9
Pod documented
131 0 my (%params) = @_;
132 0 return $self->_endpoint_request('get', \%params);
133 }
134  
135 sub _endpoint_request {
136 0 ▶ my $self = shift;
Subroutine _endpoint_request: not called CC=1 SCAR=6.9
137 0 my ($endpoint, $params) = @_;
138 0 $params->{consumer_key} = $self->consumer_key;
139 0 $params->{access_token} = $self->access_token;
140 0 return $self->_request($self->api_base_uri . $endpoint, $params);
141 }
142  
143 sub _request {
144 0 ▶ my $self = shift;
Subroutine _request: not called CC=2 SCAR=17.9
145 0 my ($uri, $params) = @_;
146  
147 0 my $response = $self->ua->post(
148 $uri,
149 {
150 content => encode_json($params),
151 headers => {
152 'Content-Type' => 'application/json; charset=UTF-8',
153 'X-Accept' => 'application/json',
154 },
155 },
156 );
157 die "Request for $uri failed ($response->{status}): $response->{reason}"
158 0 ▶ unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
159  
160 0 return decode_json($response->{content});
161 }
162  
163 2 __PACKAGE__->meta->make_immutable;
164 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166  
167 2 1;
168  
169 __END__
170  
171 =pod
172  
173 =encoding UTF-8
174  
175 =head1 NAME
176  
177 WWW::Pocket - Wrapper for the Pocket v3 API
178  
179 =head1 VERSION
180  
181 version 0.01
182  
183 =head1 SYNOPSIS
184  
185 use WWW::Pocket;
186  
187 my $pocket = WWW::Pocket->new(
188 # get a consumer key at https://getpocket.com/developer/
189 consumer_key => '...',
190 );
191  
192 my ($url, $code) = $pocket->start_authentication('https://example.com/');
193 # visit $url, log in
194 $pocket->finish_authentication($code);
195  
196 say for map { $_->{resolved_url} } values %{ $pocket->retrieve->{list} };
197  
198 =head1 DESCRIPTION
199  
200 This module wraps the L<Pocket|https://getpocket.com/> v3 API. To use, you
201 must first authenticate via OAuth by providing a valid consumer key, and then
202 visiting the OAuth endpoint (handled by the C<start_authentication> and
203 C<finish_authentication> methods). Once logged in, you can interact with the
204 API via the C<add>, C<modify>, and C<retrieve> methods, which correspond to
205 the endpoints documented at L<https://getpocket.com/developer/docs/overview>.
206  
207 This module also ships with a command line scripts called C<pocket>, for
208 interacting with the API from the command line. See the documentation for that
209 script for more details.
210  
211 =head1 ATTRIBUTES
212  
213 =head2 consumer_key
214  
215 The consumer key for your application. You can generate a consumer key at
216 L<https://getpocket.com/developer/apps/>. Required.
217  
218 =head2 access_token
219  
220 The token returned when you successfully authenticate. This can be used to
221 reauthenticate with the server without having to log in every time. It is set
222 automatically by C<finish_authentication>, but can also be provided directly
223 to avoid having to reauthenticate. It is required to be set before any API
224 methods are called.
225  
226 =head2 username
227  
228 The username that you have authenticated as. It is set automatically by
229 C<finish_authentication>, and can also be provided directly. It is
230 informational only.
231  
232 =head2 base_uri
233  
234 The base URI for the Pocket service. Defaults to C<https://getpocket.com/>.
235  
236 =head2 api_base_uri
237  
238 The base URI for the API endpoints. Defaults to appending C</v3/> to the
239 C<base_uri>.
240  
241 =head2 auth_base_uri
242  
243 The base URI for the authentication endpoints. Defaults to appending C</auth/>
244 to the C<base_uri>.
245  
246 =head2 ua
247  
248 The L<HTTP::Tiny> instance used to access the api.
249  
250 =head1 METHODS
251  
252 =head2 start_authentication($redirect_uri)
253  
254 Call this method to begin the authentication process. You must provide the
255 C<$redirect_uri>, which is where the user will be redirected to after
256 authenticating. This method returns a URL and an authentication code. The user
257 must visit the URL to log into Pocket and approve the application, and then
258 you should call C<finish_authentication> with the authentication code after
259 that is done.
260  
261 =head2 finish_authentication($code)
262  
263 Finishes the authentication process. Call this method with the code returned
264 by C<start_authentication> after the user has visited the URL which was also
265 returned by C<start_authentication>. Once this method returns, the
266 C<access_key> and C<username> attributes will be set, and other API methods
267 can be successfully called.
268  
269 =head2 add(%params)
270  
271 Wraps the L<add|https://getpocket.com/developer/docs/v3/add> endpoint.
272 C<%params> can include any parameters documented in the API documentation.
273  
274 =head2 modify
275  
276 Wraps the L<modify|https://getpocket.com/developer/docs/v3/modify> endpoint.
277 C<%params> can include any parameters documented in the API documentation.
278  
279 =head2 retrieve
280  
281 Wraps the L<retrieve|https://getpocket.com/developer/docs/v3/retrieve>
282 endpoint. C<%params> can include any parameters documented in the API
283 documentation.
284  
285 =head1 BUGS
286  
287 No known bugs.
288  
289 Please report any bugs to GitHub Issues at
290 L<https://github.com/doy/www-pocket/issues>.
291  
292 =head1 SEE ALSO
293  
294 L<Webservice::Pocket> for the v2 API
295  
296 =head1 SUPPORT
297  
298 You can find this documentation for this module with the perldoc command.
299  
300 perldoc WWW::Pocket
301  
302 You can also look for information at:
303  
304 =over 4
305  
306 =item * MetaCPAN
307  
308 L<https://metacpan.org/release/WWW-Pocket>
309  
310 =item * Github
311  
312 L<https://github.com/doy/www-pocket>
313  
314 =item * RT: CPAN's request tracker
315  
316 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Pocket>
317  
318 =item * CPAN Ratings
319  
320 L<http://cpanratings.perl.org/d/WWW-Pocket>
321  
322 =back
323  
324 =head1 AUTHOR
325  
326 Jesse Luehrs <doy@tozt.net>
327  
328 =head1 COPYRIGHT AND LICENSE
329  
330 This software is Copyright (c) 2015 by Jesse Luehrs.
331  
332 This is free software, licensed under:
333  
334 The MIT (X11) License
335  
336 =cut
« Pocket/Script.pm ↑ Summary