File Coverage

blib/lib/Badge/Depot/Plugin/Cpantesters.pm
Criterion Covered Total %
statement 35 43 81.4
branch 1 8 12.5
condition 0 3 0.0
subroutine 12 13 92.3
pod 0 1 0.0
total 48 68 70.5


line stmt bran cond sub pod time code
1 1     1   13793 use 5.10.0;
  1         3  
2 1     1   3 use strict;
  1         2  
  1         15  
3 1     1   3 use warnings;
  1         1  
  1         133  
4              
5             package Badge::Depot::Plugin::Cpantesters;
6              
7             # ABSTRACT: CPAN testers plugin for Badge::Depot
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.0101';
10              
11 1     1   453 use Moose;
  1         304846  
  1         5  
12 1     1   4710 use namespace::autoclean;
  1         2  
  1         7  
13 1     1   757 use MooseX::AttributeShortcuts;
  1         246365  
  1         4  
14 1     1   26049 use Types::Standard qw/Str HashRef/;
  1         44149  
  1         8  
15 1     1   997 use Types::URI qw/Uri/;
  1         86858  
  1         9  
16 1     1   627 use JSON::MaybeXS 'decode_json';
  1         2622  
  1         46  
17 1     1   11 use Path::Tiny;
  1         1  
  1         331  
18             with 'Badge::Depot';
19              
20             has dist => (
21             is => 'ro',
22             isa => Str,
23             lazy => 1,
24             default => sub {
25             my $self = shift;
26             if($self->get_meta('dist')) {
27             return $self->_meta->{'dist'};
28             }
29             },
30             );
31             has version => (
32             is => 'ro',
33             isa => Str,
34             lazy => 1,
35             default => sub {
36             my $self = shift;
37             if($self->get_meta('version')) {
38             return $self->_meta->{'version'};
39             }
40             },
41             );
42             has base_url => (
43             is => 'ro',
44             isa => Uri,
45             coerce => 1,
46             lazy => 1,
47             default => 'https://badgedepot.code301.com',
48             );
49             has custom_image_url => (
50             is => 'ro',
51             isa => Uri,
52             coerce => 1,
53             lazy => 1,
54             builder => 1,
55             );
56              
57             sub _build_custom_image_url {
58 1     1   1 my $self = shift;
59 1         30 return sprintf '%s/badge/cpantesters/%s/%s', $self->base_url, $self->dist, $self->version;
60             }
61             has _meta => (
62             is => 'ro',
63             isa => HashRef,
64             traits => ['Hash'],
65             lazy => 1,
66             predicate => 'has_meta',
67             builder => '_build_meta',
68             handles => {
69             get_meta => 'get',
70             },
71             );
72              
73             sub _build_meta {
74 0     0   0 my $self = shift;
75              
76 0 0       0 if($self->has_zilla) {
77             return {
78 0         0 dist => $self->zilla->name,
79             version => $self->zilla->version,
80             };
81             }
82              
83 0 0       0 return {} if !path('META.json')->exists;
84              
85 0         0 my $json = path('META.json')->slurp_utf8;
86 0         0 my $data = decode_json($json);
87              
88 0 0 0     0 return {} if !exists $data->{'name'} || !exists $data->{'version'};
89              
90             return {
91             dist => $data->{'name'},
92 0         0 version => $data->{'version'},
93             };
94             }
95              
96             sub BUILD {
97 1     1 0 2 my $self = shift;
98 1 50       35 $self->link_url(sprintf 'http://matrix.cpantesters.org/?dist=%s %s', $self->dist, $self->version eq 'latest' ? '' : $self->version);
99 1         2154 $self->image_url($self->custom_image_url);
100 1         30 $self->image_alt('CPAN Testers result');
101             }
102              
103             __PACKAGE__->meta->make_immutable;
104              
105             1;
106              
107             __END__
108              
109             =pod
110              
111             =encoding UTF-8
112              
113             =head1 NAME
114              
115             Badge::Depot::Plugin::Cpantesters - CPAN testers plugin for Badge::Depot
116              
117              
118              
119             =begin html
120              
121             <p>
122             <img src="https://img.shields.io/badge/perl-5.10+-blue.svg" alt="Requires Perl 5.10+" />
123             <a href="https://travis-ci.org/Csson/p5-Badge-Depot-Plugin-CpanTesters"><img src="https://api.travis-ci.org/Csson/p5-Badge-Depot-Plugin-CpanTesters.svg?branch=master" alt="Travis status" /></a>
124             <a href="http://cpants.cpanauthors.org/dist/Badge-Depot-Plugin-Cpantesters-0.0101"><img src="https://badgedepot.code301.com/badge/kwalitee/Badge-Depot-Plugin-Cpantesters/0.0101" alt="Distribution kwalitee" /></a>
125             <a href="http://matrix.cpantesters.org/?dist=Badge-Depot-Plugin-Cpantesters%200.0101"><img src="https://badgedepot.code301.com/badge/cpantesters/Badge-Depot-Plugin-Cpantesters/0.0101" alt="CPAN Testers result" /></a>
126             <img src="https://img.shields.io/badge/coverage-70.6%-red.svg" alt="coverage 70.6%" />
127             </p>
128              
129             =end html
130              
131             =head1 VERSION
132              
133             Version 0.0101, released 2016-02-22.
134              
135             =head1 SYNOPSIS
136              
137             If used standalone:
138              
139             use Badge::Depot::Plugin::Cpantesters;
140              
141             my $badge = Badge::Depot::Plugin::Cpantesters->new(dist => 'The-Dist', version => '0.1002');
142              
143             print $badge->to_html;
144             # prints:
145             <a href="http://matrix.cpantesters.org/?dist=The-Dist%200.1002">
146             <img src="https://badgedepot.code301.com/badge/cpantesters/The-Dist/0.1002" alt="CPAN Testers result" />
147             </a>
148              
149             If used with L<Pod::Weaver::Section::Badges>, in weaver.ini:
150              
151             [Badges]
152             ; other settings
153             badge = cpantesters
154              
155             =head1 DESCRIPTION
156              
157             Creates a L<CpanTesters|http://cpantesters.org> badge for a distribution.
158              
159             This class consumes the L<Badge::Depot> role.
160              
161             =head1 ATTRIBUTES
162              
163             This badge tries to use distribution meta data to set the attributes. If that is available no attributes need to be set manually. The following checks are made:
164              
165             =over 4
166              
167             =item 1
168              
169             If the badge is used via L<Pod::Weaver::Section::Badges> during a L<Dist::Zilla> build, then C<version> and C<dist> are set to the values in the Dist::Zilla object.
170              
171             =item 2
172              
173             If there is a C<META.json> in the distribution root then that is used to set C<version> and C<dist>.
174              
175             =back
176              
177             If neither of those are true then C<dist> and C<version> should passed to the constructor.
178              
179             =over 4
180              
181              
182              
183             =back
184              
185             =head2 dist
186              
187             Distribution name. With dashes, not colons.
188              
189             =head2 version
190              
191             Distribution version.
192              
193             =head2 base_url
194              
195             Default: C<https://badgedepot.code301.com>
196              
197             Set this if you wish to use another instance of L<Badge::Depot::App>.
198              
199             =head1 SEE ALSO
200              
201             =over 4
202              
203             =item *
204              
205             L<Badge::Depot>
206              
207             =item *
208              
209             L<Task::Badge::Depot>
210              
211             =back
212              
213             =head1 SOURCE
214              
215             L<https://github.com/Csson/p5-Badge-Depot-Plugin-CpanTesters>
216              
217             =head1 HOMEPAGE
218              
219             L<https://metacpan.org/release/Badge-Depot-Plugin-Cpantesters>
220              
221             =head1 AUTHOR
222              
223             Erik Carlsson <info@code301.com>
224              
225             =head1 COPYRIGHT AND LICENSE
226              
227             This software is copyright (c) 2016 by Erik Carlsson.
228              
229             This is free software; you can redistribute it and/or modify it under
230             the same terms as the Perl 5 programming language system itself.
231              
232             =cut