Mojito/Model/MetaCPAN.pm

filter: Statementstmt 17.5% 31 / 177 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 36.3% 8 / 22 Podpod 75.0% 6 / 8 totaltotal 14.3% 45 / 313 CC 45 SCAR 40.4
CC
45
Cov
12%
get_synopsis_from_metacpan
132.5
get_description_from_metacpan
90.0
trim_lines
90.0
CRAP 57.1
↑ Summary Mojito/Template.pm »
1 5 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Mojito::Model::MetaCPAN;
4 5 $Mojito::Model::MetaCPAN::VERSION = '0.25';
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MetaCPAN::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Text::MultiMarkdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 Name
13  
14 Mojito::Model::MetaCPAN - Tap into metacpan.org
15  
16 =cut
17  
18 has http_client => (
19 is => 'ro',
20 lazy => 1,
21 0 ▶ default => sub { HTTP::Tiny->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 5 );
23  
24 has metacpan => (
25 is => 'ro',
26 lazy => 1,
27 0 ▶ default => sub { MetaCPAN::API->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 5 );
29  
30 has cache => (
31 is => 'rw',
32 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 CHI->new(
34 driver => 'Memory',
35 global => 1,
36 # driver => 'File',
37 root_dir => '/tmp/mojito/cache',
38 );
39 },
40 5 );
41  
42 has not_found_string => (
43 is => 'rw',
44 lazy => 1,
45 0 ▶ 'default' => sub { 'NOT FOUND' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 5 );
47  
48 has markdown => (
49 is => 'ro',
50 lazy => 1,
51 0 ▶ 'default' => sub { return Text::MultiMarkdown->new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 5 );
53  
54 =head1 Methods
55  
56 =head2 get_synopsis_from_metacpan
57  
58 Args: (Str: main module path, ModuleName: main module)
59 Returns: (Array: ($pod_url_used, @synopsis_lines)
60  
61 =cut
62  
63 0 ▶sub get_synopsis_from_metacpan {
Subroutine get_synopsis_from_metacpan: not called CC=12 SCAR=48.9
Pod documented
64 0 my ($self, $main_module, $pod_url) = @_;
65  
66 0 my $pod_url_used = 'release';
67 # Use markdown format (easy to parse out SYNOPSIS)
68 # my $format = '?content-type=text/x-markdown';
69 0 my $format = '?content-type=text/plain';
70 0 my $secondary_pod_url = "https://fastapi.metacpan.org/pod/${main_module}${format}";
71 0 ▶ if (not $pod_url) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pod_url) { } 0 0 0
72 0 $pod_url = $secondary_pod_url;
73 0 $pod_url_used = 'main_module';
74 }
75 else {
76 0 $pod_url .= $format;
77 }
78 0 my $response = $self->http_client->get($pod_url);
79 0 ▶ if (not $response->{success} && ($pod_url_used = 'release')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"} and $pod_url_used = "release") 0 0 0
Condition0%0/2
$response->{"success"} and $pod_url_used = "release"
!ll
00
MC/DC0%0/1
$response->{"success"} and $pod_url_used = "release"
$response->{"success"}
Truth table0%0/2
A
A= $response->{"success"}
Aresult
00
11
80 0 warn "Could not find POD at $pod_url. Trying: $secondary_pod_url..";
81 0 $response = $self->http_client->get($secondary_pod_url);
82 0 ▶ if (not $response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
83 0 warn "Could not find POD at $secondary_pod_url";
84 0 return $self->not_found_string;
85 }
86 }
87 0 my $content = $response->{content}; # if length $response->{content};
88 0 my @synopsis_lines = ();
89 0 my $seen_synopsis = my $seen_synopsis_end = 0;
90 0 my @content_lines = split '\n', $content;
91  
92 0 foreach (@content_lines) {
93 # Are we starting the section after the Synopsis?
94 # if ($seen_synopsis && m/^#\s/) {
95 0 ▶ if ($seen_synopsis && m/^\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_synopsis and /^\S/) 0 0 0
Condition0%0/3
$seen_synopsis and /^\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$seen_synopsis and /^\S/
$seen_synopsis /^\S/
Truth table0%0/3
A and B
A= $seen_synopsisB= /^\S/
ABresult
0X0
100
111
96 0 $seen_synopsis_end = 1;
97 }
98 # if (m/^#\s+SYNOPSIS/i) {
99 0 ▶ if (m/^SYNOPSIS/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^SYNOPSIS/i) 0 0 0
100 0 $seen_synopsis = 1;
101 }
102 0 ▶ if ($seen_synopsis && not $seen_synopsis_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_synopsis and not $seen_synopsis_end) 0 0 0
Condition0%0/3
$seen_synopsis and not $seen_synopsis_end
!ll&&!rl&&r
000
MC/DC0%0/2
$seen_synopsis and not $seen_synopsis_end
$seen_synopsis not $seen_synopsis_end
Truth table0%0/3
A and not(B)
A= $seen_synopsisB= not $seen_synopsis_end
ABresult
0X0
100
111
103 5 ▶ use Encode qw/ decode_utf8 encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 0 push @synopsis_lines, decode_utf8($_);
105 # push @synopsis_lines, $_;
106 }
107 }
108 0 ▶ return wantarray ? @synopsis_lines : join "\n", @synopsis_lines;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
109 }
110  
111 0 ▶sub get_description_from_metacpan {
Subroutine get_description_from_metacpan: not called CC=9 SCAR=45.0
Pod undocumented
112 0 my ($self, $Module) = @_;
113
114 0 my $pod_url =
115 "http://api.metacpan.org/pod/${Module}?content-type=text/x-markdown";
116 0 my $response = $self->http_client->get($pod_url);
117 0 ▶ if (not $response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
118 #warn "Failed to get URL: $pod_url";
119 0 return;
120 }
121 0 my $content = $response->{content}; # if length $response->{content};
122 0 my @description_lines = ();
123 0 my $seen_description = my $seen_description_end = 0;
124 0 my @content_lines = split '\n', $content;
125  
126 0 foreach (@content_lines) {
127
128 # Are we starting the section after the Synopsis?
129 0 ▶ if ($seen_description && m/^#\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_description and /^#\s/) 0 0 0
Condition0%0/3
$seen_description and /^#\s/
!ll&&!rl&&r
000
MC/DC0%0/2
$seen_description and /^#\s/
$seen_description /^#\s/
Truth table0%0/3
A and B
A= $seen_descriptionB= /^#\s/
ABresult
0X0
100
111
130 0 $seen_description_end = 1;
131 }
132 0 ▶ if (m/^#\s+DESCRIPTION/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^#\s+DESCRIPTION/i) 0 0 0
133 0 $seen_description = 1;
134 }
135 0 ▶ if ($seen_description && not $seen_description_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_description and not $seen_description_end) 0 0 0
Condition0%0/3
$seen_description and not $seen_description_end
!ll&&!rl&&r
000
MC/DC0%0/2
$seen_description and not $seen_description_end
$seen_description not $seen_description_end
Truth table0%0/3
A and not(B)
A= $seen_descriptionB= not $seen_description_end
ABresult
0X0
100
111
136 0 push @description_lines, $_;
137 }
138 }
139 0 ▶ return wantarray ? @description_lines : join "\n", @description_lines;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
140 }
141
142 =head2 get_synopsis_formatted
143  
144 signature: (a Perl Module name, an element of qw/presentation/)
145 example: my $synop = $self->get_synopsis_formatted('Moose', 'presentation');
146
147 =cut
148  
149 0 ▶sub get_synopsis_formatted {
Subroutine get_synopsis_formatted: not called CC=4 SCAR=30.0
Pod documented
150 0 my ($self, $release, $format) = @_;
151 0 ▶ $format ||= 'presentation';
Condition0%0/2
$format ||= "presentation"
l!l
00
MC/DC0%0/1
$format ||= "presentation"
$format
Truth table0%0/2
A
A= $format
Aresult
11
00
152
153 0 my $main_module_pod_url = my $main_module = $release->{distribution};
154 0 $main_module =~ s|-|::|g;
155 0 $main_module_pod_url =~ s|-|/|g;
156 0 $main_module_pod_url = "http://api.metacpan.org/pod/$release->{author}/$release->{name}/lib/${main_module_pod_url}.pm";
157 # Just have the presentation format for starters.
158 my $dispatch_table = {
159 0 ▶ presentation => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
160 0 my @synopsis_lines = $self->get_synopsis_from_metacpan($main_module, $main_module_pod_url);
161 0 @synopsis_lines = $self->trim_lines(@synopsis_lines);
162 0 ▶ if (not scalar @synopsis_lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @synopsis_lines) 0 0 0
163 0 return $self->not_found_string;
164 }
165 0 my $abstract = '';
166 0 ▶ $abstract = "<section class='module_abstract'>$release->{abstract}</section>\n" if $release->{abstract};
Branch: 1 branch
BranchTrueFalseTotal
if $release->{"abstract"} 0 0 0
167 # Comment out lines that don't start with a comment
168 # and are not indented (i.e. not code)
169 # because we'd like the Synopsis to be runnable (in theory)
170 0 my ($whitespace) = $synopsis_lines[0] =~ m/^(\s*)/;
171 0 @synopsis_lines = map { my $line = $_; $line =~ s/^(\w)/&#35; $1/; $line; } @synopsis_lines;
172  
173 # Trim off leading whitespace (usually 2 or 4)
174 0 @synopsis_lines = map { my $line = $_; $line =~ s/^$whitespace//; $line; } @synopsis_lines;
175 0 my $synopsis = join "\n", @synopsis_lines;
176  
177 # pre wrapper for syntax highlight
178 0 $synopsis = "${abstract}<pre class='sh_perl'>\n" . $synopsis . "</pre>\n";
179 0 $synopsis = "<h2 class='Module'><a href='https://metacpan.org/release/$release->{author}/$release->{name}'>$release->{distribution}</a></h2>" . $synopsis;
180
181 0 return $synopsis;
182 }
183 0 };
184  
185 0 my $cache_key = "$release->{name}:SYNOPSIS:${format}";
186 0 my $synopsis = $self->cache->get($cache_key);
187 0 ▶ if (not $synopsis) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($synopsis) 0 0 0
188 0 ▶ warn "GET $main_module SYNOPSIS from CPAN" if $ENV{MOJITO_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJITO_DEBUG"} 0 0 0
189 0 $synopsis = $dispatch_table->{$format}->($main_module_pod_url);
190 0 $self->cache->set($cache_key, $synopsis, '3 days');
191 }
192 0 return $synopsis;
193 }
194  
195 =head2 trim_lines
196  
197 Remove first line
198 Remove leading and trailing blank lines
199  
200 =cut
201  
202 0 ▶sub trim_lines {
Subroutine trim_lines: not called CC=9 SCAR=45.0
Pod documented
203 0 my ($self, @lines) = @_;
204  
205 0 ▶ return if not scalar @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @lines 0 0 0
206  
207 # Get rid of first line and any blank line directly after
208 # We'll rewrite the first line and are making the results more
209 # compact by removing the blank lines.
210 0 shift @lines;
211 0 ▶ return if not scalar @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @lines 0 0 0
212 0 ▶ while ($lines[0] && $lines[0] =~ m/^\s*?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] and $lines[0] =~ /^\s*?$/) 0 0 0
Condition0%0/3
$lines[0] and $lines[0] =~ /^\s*?$/
!ll&&!rl&&r
000
MC/DC0%0/2
$lines[0] and $lines[0] =~ /^\s*?$/
$lines[0] $lines[0] =~ /^\s*?$/
Truth table0%0/3
A and B
A= $lines[0]B= $lines[0] =~ /^\s*?$/
ABresult
0X0
100
111
213 0 shift @lines;
214 }
215 0 ▶ return if not scalar @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @lines 0 0 0
216  
217 # Do same for tail
218 0 ▶ while ($lines[-1] && $lines[-1] =~ m/^\s*?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[-1] and $lines[-1] =~ /^\s*?$/) 0 0 0
Condition0%0/3
$lines[-1] and $lines[-1] =~ /^\s*?$/
!ll&&!rl&&r
000
MC/DC0%0/2
$lines[-1] and $lines[-1] =~ /^\s*?$/
$lines[-1] $lines[-1] =~ /^\s*?$/
Truth table0%0/3
A and B
A= $lines[-1]B= $lines[-1] =~ /^\s*?$/
ABresult
0X0
100
111
219 0 pop @lines;
220 }
221 0 ▶ return if not scalar @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @lines 0 0 0
222 0 return @lines;
223 }
224 =head2 get_recent_releases_from_metacpan
225  
226 Get an ArrayRef[HashRef] of the most recent CPAN releases
227  
228 =cut
229  
230 0 ▶sub get_recent_releases_from_metacpan {
Subroutine get_recent_releases_from_metacpan: not called CC=2 SCAR=17.9
Pod undocumented
231 0 my ($self, $how_many) = @_;
232 0 ▶ $how_many ||= 10;
Condition0%0/2
$how_many ||= 10
l!l
00
MC/DC0%0/1
$how_many ||= 10
$how_many
Truth table0%0/2
A
A= $how_many
Aresult
11
00
233  
234 0 my @fields = qw/author name distribution version maturity status abstract download_url/;
235 0 my $fields_string = join ',', @fields;
236 0 my $result = $self->metacpan->release(
237 search => {
238 sort => "date:desc",
239 fields => $fields_string,
240 size => $how_many,
241 },
242 );
243  
244 0 return [ map { $_->{fields} } @{ $result->{hits}->{hits} } ];
245 }
246  
247 =head2 recent_synopses_shortcut
248  
249 Create the Mojito shortcut that gets the synopses of the most
250 recently released CPAN distributions. Looks like:
251
252 {{synopsis Module1}}
253 {{synopsis Module2}}
254 ...
255 {{synopsis Modulen}}
256
257 =cut
258  
259 0 ▶sub recent_synopses_shortcut {
Subroutine recent_synopses_shortcut: not called CC=4 SCAR=30.0
Pod documented
260 0 my ($self, $how_many) = @_;
261 0 ▶ $how_many ||= 10;
Condition0%0/2
$how_many ||= 10
l!l
00
MC/DC0%0/1
$how_many ||= 10
$how_many
Truth table0%0/2
A
A= $how_many
Aresult
11
00
262  
263 0 my $cache_key = "CPAN_RECENT_SYNOPSES:${how_many}";
264 0 my $synopses = $self->cache->get($cache_key);
265 0 ▶ if (not $synopses) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($synopses) 0 0 0
266 0 ▶ warn "GET Recent Release from CPAN" if $ENV{MOJITO_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJITO_DEBUG"} 0 0 0
267 0 my @releases = $self->get_recent_releases_from_metacpan($how_many);
268 0 my @recent_synopses = map { "{{cpan.synopsis $_}}" }
269 map {
270 0 my $dist = $_->{distribution};
271 0 $dist =~ s/\-/::/g;
272 0 $dist;
273 } @releases;
274 0 $synopses = join "\n", @recent_synopses;
275 0 $self->cache->set($cache_key, $synopses, '1 minute');
276 }
277 0 return $synopses;
278 }
279  
280 =head2 get_recent_releases
281  
282 Get the most recent releases (as module names)
283
284 =cut
285  
286 0 ▶sub get_recent_releases {
Subroutine get_recent_releases: not called CC=4 SCAR=30.0
Pod documented
287 0 my ($self, $how_many) = @_;
288 0 ▶ $how_many ||= 10;
Condition0%0/2
$how_many ||= 10
l!l
00
MC/DC0%0/1
$how_many ||= 10
$how_many
Truth table0%0/2
A
A= $how_many
Aresult
11
00
289  
290 0 my $cache_key = "CPAN_RECENT_RELEASES:${how_many}";
291 0 my $releases = $self->cache->get($cache_key);
292 0 ▶ if (not $releases) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($releases) 0 0 0
293 0 ▶ warn "GET Recent Releases from CPAN\n" if $ENV{MOJITO_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJITO_DEBUG"} 0 0 0
294 0 $releases = $self->get_recent_releases_from_metacpan($how_many);
295 0 $self->cache->set($cache_key, $releases, '1 minute');
296 }
297 0 return $releases;
298 }
299  
300 =head2 get_recent_synopses
301  
302 Get the most recent synopses from CPAN
303
304 =cut
305  
306 0 ▶sub get_recent_synopses {
Subroutine get_recent_synopses: not called CC=6 SCAR=37.4
Pod documented
307 0 my ($self, $how_many) = @_;
308 0 ▶ $how_many ||= 10;
Condition0%0/2
$how_many ||= 10
l!l
00
MC/DC0%0/1
$how_many ||= 10
$how_many
Truth table0%0/2
A
A= $how_many
Aresult
11
00
309 0 my $metacpan_web_host = 'https://metacpan.org';
310  
311 0 my $html;
312 0 my $releases = $self->get_recent_releases($how_many);
313 # Avoid duplicates
314 0 my %have_seen = ();
315 0 foreach my $release (@{$releases}) {
316 0 my $main_module = $release->{distribution};
317 0 $main_module =~ s/\-/::/g;
318 0 ▶ next if $have_seen{$main_module};
Branch: 1 branch
BranchTrueFalseTotal
if $have_seen{$main_module} 0 0 0
319 0 my $synopsis = $self->get_synopsis_formatted($release, 'presentation');
320 0 my $not_found_message = '';
321 0 ▶ if ($synopsis eq $self->not_found_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($synopsis eq $self->not_found_string) { } 0 0 0
322 0 $not_found_message = 'Synopsis not found for ';
323 0 ▶ if ($release->{maturity} eq 'released') {
Branch: 1 branch
BranchTrueFalseTotal
if ($release->{'maturity'} eq 'released') { } 0 0 0
324 0 $html .= "<section class='released'>$not_found_message
325 <a href='${metacpan_web_host}/release/$release->{author}/$release->{name}'>$release->{name}</a>
326 </section>\n";
327 }
328 else {
329 0 $html .= "<section class='developer'>$not_found_message
330 <a href='${metacpan_web_host}/release/$release->{author}/$release->{name}'>$release->{name}</a>
331 <span style='font-size: 88%;'> (dev)</span>
332 </section>\n";
333 }
334 }
335 else {
336 0 $html .= $synopsis;
337 }
338 0 $have_seen{$main_module}++;
339 }
340 0 return $html;
341 }
342  
343 5 1
↑ Summary Mojito/Template.pm »