Pocket/Script.pm

filter: Statementstmt 19.7% 32 / 162 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 22.8% 8 / 35 Podpod 0.0% 0 / 13 totaltotal 13.8% 40 / 289 CC 38 SCAR 38.2
CC
38
Cov
14%
retrieve_raw
182.0
_method_is_command
42.0
_parse_retrieve_options
42.0
CRAP 45.7
↑ Summary Pocket.pm »
1 package WWW::Pocket::Script;
2 1 our $AUTHORITY = 'cpan:DOY';
3 1 $WWW::Pocket::Script::VERSION = '0.02';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Getopt::Long 'GetOptionsFromArray';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util 'sum';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use WWW::Pocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has consumer_key => (
15 is => 'ro',
16 isa => 'Str',
17 lazy => 1,
18 0 ▶ default => sub { die "consumer_key is required to authenticate" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 1 predicate => '_has_consumer_key',
20 );
21  
22 1 has redirect_uri => (
23 is => 'ro',
24 isa => 'Str',
25 default => 'https://getpocket.com/',
26 );
27  
28 1 has credentials_file => (
29 is => 'ro',
30 isa => 'Str',
31 default => "$ENV{HOME}/.pocket",
32 );
33  
34 has pocket => (
35 is => 'ro',
36 isa => 'WWW::Pocket',
37 lazy => 1,
38 default => sub {
39 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
40  
41 0 my $credentials_file = file($self->credentials_file);
42 0 ▶ if (-e $credentials_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $credentials_file) { } 0 0 0
43 0 return $self->_apply_credentials($credentials_file);
44 }
45 else {
46 0 return $self->_authenticate;
47 }
48 },
49 1 );
50  
51 sub run {
52 0 ▶ my $self = shift;
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
53 0 my @argv = @_;
54  
55 0 my $method = shift @argv;
56 0 ▶ if ($self->_method_is_command($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_method_is_command($method)) { } 0 0 0
57 0 return $self->$method(@argv);
58 }
59 else {
60 0 die "insert usage here";
61 }
62 }
63  
64 sub _method_is_command {
65 0 ▶ my $self = shift;
Subroutine _method_is_command: not called CC=6 SCAR=37.4
66 0 my ($name) = @_;
67  
68 0 ▶ return if $name eq 'run' || $name eq 'meta';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "run" or $name eq "meta" 0 0 0
Condition0%0/3
$name eq "run" or $name eq "meta"
l!l&&r!l&&!r
000
MC/DC0%0/2
$name eq "run" or $name eq "meta"
$name eq "run" $name eq "meta"
Truth table0%0/3
A or B
A= $name eq "run"B= $name eq "meta"
ABresult
1X1
011
000
69 0 ▶ return if $name =~ /^_/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^_/ 0 0 0
70 0 my $method = $self->meta->find_method_by_name($name);
71 0 ▶ return unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 0 0
72 0 ▶ return if $method->isa('Class::MOP::Method::Accessor');
Branch: 1 branch
BranchTrueFalseTotal
if $method->isa("Class::MOP::Method::Accessor") 0 0 0
73  
74 0 return 1;
75 }
76  
77 sub authenticate {
78 0 ▶ my $self = shift;
Subroutine authenticate: not called CC=1 SCAR=6.9
Pod undocumented
79 0 $self->pocket;
80 }
81  
82 sub _apply_credentials {
83 0 ▶ my $self = shift;
Subroutine _apply_credentials: not called CC=1 SCAR=6.9
84 0 my ($file) = @_;
85  
86 0 my ($consumer_key, $access_token, $username) = $file->slurp(chomp => 1);
87 0 return WWW::Pocket->new(
88 consumer_key => $consumer_key,
89 access_token => $access_token,
90 username => $username,
91 );
92 }
93  
94 sub _authenticate {
95 0 ▶ my $self = shift;
Subroutine _authenticate: not called CC=2 SCAR=17.9
96  
97 0 ▶ my $consumer_key = $self->_has_consumer_key
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_consumer_key ? : 0 0 0
98 ? $self->consumer_key
99 : $self->_prompt_for_consumer_key;
100  
101 0 my $pocket = WWW::Pocket->new(consumer_key => $consumer_key);
102  
103 0 my $redirect_uri = $self->redirect_uri;
104 0 my ($url, $code) = $pocket->start_authentication($redirect_uri);
105  
106 0 print "Visit $url and log in. When you're done, press enter to continue.\n";
107 0 <STDIN>;
108  
109 0 $pocket->finish_authentication($code);
110  
111 0 my $fh = file($self->credentials_file)->openw;
112 0 $fh->write($pocket->consumer_key . "\n");
113 0 $fh->write($pocket->access_token . "\n");
114 0 $fh->write($pocket->username . "\n");
115 0 $fh->close;
116  
117 0 return $pocket;
118 }
119  
120 sub _prompt_for_consumer_key {
121 0 ▶ my $self = shift;
Subroutine _prompt_for_consumer_key: not called CC=1 SCAR=6.9
122  
123 0 print "Enter your consumer key: ";
124 0 chomp(my $key = <STDIN>);
125 0 return $key;
126 }
127  
128 sub list {
129 0 ▶ my $self = shift;
Subroutine list: not called CC=2 SCAR=17.9
Pod undocumented
130 0 my @argv = @_;
131  
132 0 my ($params) = $self->_parse_retrieve_options(@argv);
133 0 my %params = (
134 $self->_default_search_params,
135 %$params,
136 );
137  
138 0 print "$_\n" for $self->_retrieve_urls(%params);
139 }
140  
141 sub words {
142 0 ▶ my $self = shift;
Subroutine words: not called CC=2 SCAR=17.9
Pod undocumented
143 0 my @argv = @_;
144  
145 0 my ($params) = $self->_parse_retrieve_options(@argv);
146 0 my %params = (
147 $self->_default_search_params,
148 %$params,
149 );
150  
151 0 ▶ my $word_count = sum($self->_retrieve_field('word_count', %params)) || 0;
Condition0%0/2
&sum($self->_retrieve_field('word_count', %params)) || 0
l!l
00
MC/DC0%0/1
&sum($self->_retrieve_field('word_count', %params)) || 0
&sum($self->_retrieve_field('word_count', %params))
Truth table0%0/2
A
A= &sum($self->_retrieve_field('word_count', %params))
Aresult
11
00
152 0 print "$word_count\n";
153 }
154  
155 sub search {
156 0 ▶ my $self = shift;
Subroutine search: not called CC=2 SCAR=17.9
Pod undocumented
157 0 my @argv = @_;
158  
159 0 my ($params, $extra_argv) = $self->_parse_retrieve_options(@argv);
160 0 my ($search) = @$extra_argv;
161 0 my %params = (
162 $self->_default_search_params,
163 %$params,
164 search => $search,
165 );
166  
167 0 print "$_\n" for $self->_retrieve_urls(%params);
168 }
169  
170 sub favorites {
171 0 ▶ my $self = shift;
Subroutine favorites: not called CC=2 SCAR=17.9
Pod undocumented
172 0 my @argv = @_;
173  
174 0 my ($params) = $self->_parse_retrieve_options(@argv);
175 0 my %params = (
176 $self->_default_search_params,
177 state => 'all',
178 %$params,
179 favorite => 1,
180 );
181  
182 0 print "$_\n" for $self->_retrieve_urls(%params);
183 }
184  
185 sub retrieve_raw {
186 0 ▶ my $self = shift;
Subroutine retrieve_raw: not called CC=13 SCAR=52.0
Pod undocumented
187 0 my @argv = @_;
188  
189 0 my ($state, $favorite, $tag, $contentType, $sort, $detailType);
190 0 my ($search, $domain, $since, $count, $offset);
191 GetOptionsFromArray(
192 \@argv,
193 "state=s" => \$state,
194 0 ▶ "favorite!" => sub { $favorite = $_[1] ? '1' : '0' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
195 0 ▶ "tag=s" => \$tag,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@argv, "state=s", \$state, "favorite!", sub { $favorite = $_[1] ? 1 : 0; } , "tag=s", \$tag, "contentType=s", \$contentType, "sort=s", \$sort, "detailType=s", \$detailType, "search=s", \$search, "domain=s", \$domain, "since=i", \$since, "count=i", \$count, "offset=i", \$offset) or die "???" 0 0 0
196 "contentType=s" => \$contentType,
197 "sort=s" => \$sort,
198 "detailType=s" => \$detailType,
199 "search=s" => \$search,
200 "domain=s" => \$domain,
201 "since=i" => \$since,
202 "count=i" => \$count,
203 "offset=i" => \$offset,
204 ) or die "???";
205  
206 0 ▶ my %params = (
Branch: 11 branches
BranchTrueFalseTotal
defined $state ? : 0 0 0
defined $favorite ? : 0 0 0
defined $tag ? : 0 0 0
defined $contentType ? : 0 0 0
defined $sort ? : 0 0 0
defined $detailType ? : 0 0 0
defined $search ? : 0 0 0
defined $domain ? : 0 0 0
defined $since ? : 0 0 0
defined $count ? : 0 0 0
defined $offset ? : 0 0 0
207 (defined($state) ? (state => $state) : ()),
208 (defined($favorite) ? (favorite => $favorite) : ()),
209 (defined($tag) ? (tag => $tag) : ()),
210 (defined($contentType) ? (contentType => $contentType) : ()),
211 (defined($sort) ? (sort => $sort) : ()),
212 (defined($detailType) ? (detailType => $detailType) : ()),
213 (defined($search) ? (search => $search) : ()),
214 (defined($domain) ? (domain => $domain) : ()),
215 (defined($since) ? (since => $since) : ()),
216 (defined($count) ? (count => $count) : ()),
217 (defined($offset) ? (offset => $offset) : ()),
218 );
219  
220 0 $self->_pretty_print($self->pocket->retrieve(%params));
221 }
222  
223 sub _parse_retrieve_options {
224 0 ▶ my $self = shift;
Subroutine _parse_retrieve_options: not called CC=6 SCAR=37.4
225 0 my @argv = @_;
226  
227 0 my ($unread, $archive, $all, @tags);
228 0 ▶ GetOptionsFromArray(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@argv, "unread", \$unread, "archive", \$archive, "all", \$all, "tag=s", \@tags) or die "???" 0 0 0
229 \@argv,
230 "unread" => \$unread,
231 "archive" => \$archive,
232 "all" => \$all,
233 "tag=s" => \@tags,
234 ) or die "???";
235  
236 return (
237 {
238 0 ▶ ($unread ? (state => 'unread') : ()),
Branch: 4 branches
BranchTrueFalseTotal
$unread ? : 0 0 0
$archive ? : 0 0 0
$all ? : 0 0 0
@tags ? : 0 0 0
239 ($archive ? (state => 'archive') : ()),
240 ($all ? (state => 'all') : ()),
241 (@tags ? (tag => join(',', @tags)) : ()),
242 },
243 [ @argv ],
244 );
245 }
246  
247 sub _default_search_params {
248 0 ▶ my $self = shift;
Subroutine _default_search_params: not called CC=1 SCAR=6.9
249  
250 return (
251 0 sort => 'oldest',
252 detailType => 'simple',
253 );
254 }
255  
256 sub _retrieve_urls {
257 0 ▶ my $self = shift;
Subroutine _retrieve_urls: not called CC=1 SCAR=6.9
258 0 my %params = @_;
259  
260 0 $self->_retrieve_field('resolved_url', %params);
261 }
262  
263 sub _retrieve_field {
264 0 ▶ my $self = shift;
Subroutine _retrieve_field: not called CC=3 SCAR=24.8
265 0 my ($field, %params) = @_;
266  
267 0 my $response = $self->pocket->retrieve(%params);
268 0 my $list = $response->{list};
269 0 ▶ return unless ref($list) && ref($list) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list and ref $list eq "HASH" 0 0 0
Condition0%0/3
ref $list and ref $list eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $list and ref $list eq "HASH"
ref $list ref $list eq "HASH"
Truth table0%0/3
A and B
A= ref $listB= ref $list eq "HASH"
ABresult
0X0
100
111
270  
271 return map {
272 0 $_->{$field}
273 } sort {
274 0 $a->{sort_id} <=> $b->{sort_id}
275 0 } values %$list;
276 }
277  
278 sub _pretty_print {
279 0 ▶ my $self = shift;
Subroutine _pretty_print: not called CC=1 SCAR=6.9
280 0 my ($data) = @_;
281  
282 0 print JSON::PP->new->utf8->pretty->canonical->encode($data), "\n";
283 }
284  
285 sub add {
286 0 ▶ my $self = shift;
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
287 0 my ($url, $title) = @_;
288  
289 0 $self->pocket->add(
290 url => $url,
291 title => $title,
292 );
293 0 print "Page Saved!\n";
294 }
295  
296 sub archive {
297 0 ▶ my $self = shift;
Subroutine archive: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my ($url) = @_;
299  
300 0 $self->_modify('archive', $url);
301 0 print "Page archived!\n";
302 }
303  
304 sub readd {
305 0 ▶ my $self = shift;
Subroutine readd: not called CC=1 SCAR=6.9
Pod undocumented
306 0 my ($url) = @_;
307  
308 0 $self->_modify('readd', $url);
309 0 print "Page added!\n";
310 }
311  
312 sub favorite {
313 0 ▶ my $self = shift;
Subroutine favorite: not called CC=1 SCAR=6.9
Pod undocumented
314 0 my ($url) = @_;
315  
316 0 $self->_modify('favorite', $url);
317 0 print "Page favorited!\n";
318 }
319  
320 sub unfavorite {
321 0 ▶ my $self = shift;
Subroutine unfavorite: not called CC=1 SCAR=6.9
Pod undocumented
322 0 my ($url) = @_;
323  
324 0 $self->_modify('unfavorite', $url);
325 0 print "Page unfavorited!\n";
326 }
327  
328 sub delete {
329 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
330 0 my ($url) = @_;
331  
332 0 $self->_modify('delete', $url);
333 0 print "Page deleted!\n";
334 }
335  
336 sub _modify {
337 0 ▶ my $self = shift;
Subroutine _modify: not called CC=1 SCAR=6.9
338 0 my ($action, $url) = @_;
339  
340 0 $self->pocket->modify(
341 actions => [
342 {
343 action => $action,
344 item_id => $self->_get_id_for_url($url),
345 },
346 ],
347 );
348 }
349  
350 sub _get_id_for_url {
351 0 ▶ my $self = shift;
Subroutine _get_id_for_url: not called CC=6 SCAR=37.4
352 0 my ($url) = @_;
353  
354 0 my $response = $self->pocket->retrieve(
355 domain => URI->new($url)->host,
356 state => 'all',
357 );
358 0 my $list = $response->{list};
359 0 ▶ return unless ref($list) && ref($list) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list and ref $list eq "HASH" 0 0 0
Condition0%0/3
ref $list and ref $list eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $list and ref $list eq "HASH"
ref $list ref $list eq "HASH"
Truth table0%0/3
A and B
A= ref $listB= ref $list eq "HASH"
ABresult
0X0
100
111
360  
361 0 for my $item (values %$list) {
362 return $item->{item_id}
363 if $item->{resolved_url} eq $url
364 0 ▶ || $item->{given_url} eq $url;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"resolved_url"} eq $url or $item->{"given_url"} eq $url 0 0 0
Condition0%0/3
$item->{"resolved_url"} eq $url or $item->{"given_url"} eq $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$item->{"resolved_url"} eq $url or $item->{"given_url"} eq $url
$item->{"resolved_url"} eq $url $item->{"given_url"} eq $url
Truth table0%0/3
A or B
A= $item->{"resolved_url"} eq $urlB= $item->{"given_url"} eq $url
ABresult
1X1
011
000
365 }
366  
367 0 return;
368 }
369  
370 1 __PACKAGE__->meta->make_immutable;
371 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
372  
373 =begin Pod::Coverage
374  
375 add
376 archive
377 authenticate
378 delete
379 favorite
380 favorites
381 list
382 readd
383 retrieve_raw
384 run
385 search
386 unfavorite
387 words
388  
389 =end Pod::Coverage
390  
391 =cut
392  
393 1 1;
↑ Summary Pocket.pm »