File Coverage

blib/lib/Badge/Depot/Plugin/Travis.pm
Criterion Covered Total %
statement 25 33 75.7
branch 0 6 0.0
condition n/a
subroutine 8 9 88.8
pod 0 1 0.0
total 33 49 67.3


line stmt bran cond sub pod time code
1 1     1   15206 use strict;
  1         1  
  1         32  
2 1     1   3 use warnings;
  1         5  
  1         33  
3              
4             package Badge::Depot::Plugin::Travis;
5              
6 1     1   452 use Moose;
  1         311350  
  1         5  
7 1     1   5149 use namespace::autoclean;
  1         956  
  1         4  
8 1     1   480 use Types::Standard qw/Str HashRef/;
  1         42517  
  1         10  
9 1     1   1162 use Path::Tiny;
  1         8423  
  1         54  
10 1     1   326 use JSON::MaybeXS 'decode_json';
  1         3506  
  1         289  
11             with 'Badge::Depot';
12              
13             our $VERSION = '0.0201'; # VERSION
14             # ABSTRACT: Travis plugin for Badge::Depot
15              
16             has user => (
17             is => 'ro',
18             isa => Str,
19             lazy => 1,
20             default => sub {
21             my $self = shift;
22             if($self->has_meta) {
23             return $self->_meta->{'username'} if exists $self->_meta->{'username'};
24             }
25             },
26             );
27             has repo => (
28             is => 'ro',
29             isa => Str,
30             lazy => 1,
31             default => sub {
32             my $self = shift;
33             if($self->has_meta) {
34             return $self->_meta->{'repo'} if exists $self->_meta->{'repo'};
35             }
36             },
37             );
38             has branch => (
39             is => 'ro',
40             isa => Str,
41             default => 'master',
42             );
43             has _meta => (
44             is => 'ro',
45             isa => HashRef,
46             predicate => 'has_meta',
47             builder => '_build_meta',
48             );
49              
50             sub _build_meta {
51 0     0   0 my $self = shift;
52              
53 0 0       0 return if !path('META.json')->exists;
54              
55 0         0 my $json = path('META.json')->slurp_utf8;
56 0         0 my $data = decode_json($json);
57              
58 0 0       0 return if !exists $data->{'resources'}{'repository'}{'web'};
59              
60 0         0 my $repository = $data->{'resources'}{'repository'}{'web'};
61 0 0       0 return if $repository !~ m{^https://(?:www\.)?github\.com/([^/]+)/(.*)(?:\.git)?$};
62              
63             return {
64 0         0 username => $1,
65             repo => $2,
66             };
67             }
68              
69             sub BUILD {
70 1     1 0 1532 my $self = shift;
71 1         78 $self->link_url(sprintf 'https://travis-ci.org/%s/%s', $self->user, $self->repo);
72 1         2838 $self->image_url(sprintf 'https://api.travis-ci.org/%s/%s.svg?branch=%s', $self->user, $self->repo, $self->branch);
73 1         101 $self->image_alt('Travis status');
74             }
75              
76             1;
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             Badge::Depot::Plugin::Travis - Travis plugin for Badge::Depot
87              
88             =head1 VERSION
89              
90             Version 0.0201, released 2015-03-21.
91              
92             =head1 SYNOPSIS
93              
94             use Badge::Depot::Plugin::Travis;
95              
96             my $badge = Badge::Depot::Plugin::Travis->new(user => 'my_name', repo => 'the_repo', branch => 'master');
97              
98             print $badge->to_html;
99             # prints '<a href="https://travis-ci.org/my_name/my_repo"><img src="https://api.travis-ci.org/my_name/my_repo.svg?branch=master" /></a>'
100              
101             =head1 DESCRIPTION
102              
103             Create a L<Travis|https://travis-ci.org> badge for a github repository.
104              
105             This class consumes the L<Badge::Depot> role.
106              
107             =head1 ATTRIBUTES
108              
109             The C<user> and C<repo> attributes are required or optional, depending on your configuration. It looks for the C<resources/repository/web> setting in C<META.json>:
110              
111             =over 4
112              
113             =item *
114              
115             If C<META.json> doesn't exist in the dist root, C<user> and C<repo> are required.
116              
117             =item *
118              
119             If C<resources/repository/web> doesn't exist (or is not a github url), C<user> and C<repo> are required.
120              
121             =back
122              
123             =head2 user
124              
125             Github username.
126              
127             =head2 repo
128              
129             Github repository.
130              
131             =head2 branch
132              
133             Github branch. Optional, C<master> by default.
134              
135             =head1 SEE ALSO
136              
137             =over 4
138              
139             =item *
140              
141             L<Badge::Depot>
142              
143             =back
144              
145             =head1 SOURCE
146              
147             L<https://github.com/Csson/p5-Badge-Depot-Plugin-Travis>
148              
149             =head1 HOMEPAGE
150              
151             L<https://metacpan.org/release/Badge-Depot-Plugin-Travis>
152              
153             =head1 AUTHOR
154              
155             Erik Carlsson <info@code301.com>
156              
157             =head1 COPYRIGHT AND LICENSE
158              
159             This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
160              
161             This is free software; you can redistribute it and/or modify it under
162             the same terms as the Perl 5 programming language system itself.
163              
164             =cut