blib/lib/WWW/Lipsum.pm

filter: Statementstmt 86.1% 62 / 72 Branchbran 50.0% 13 / 26 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 92.3% 12 / 13 Podpod 100% 1 / 1 totaltotal 74.1% 89 / 120 CC 16 SCAR 21.7
CC
16
Cov
74%
_prep_args
20.3
generate
8.6
__ANON__
2.0
CRAP 8.8
↑ Summary
1 package WWW::Lipsum;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '1.001015'; # VERSION
7  
8 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use overload q|""| => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.1
13 6 my $self = shift;
14 6 my $text = $self->generate;
15 6 ▶ return $text || '[Error: ' . $self->error . ']';
Condition50%1/2
$text || '[Error: ' . $self->error . ']'
l!l
60
MC/DC0%0/1
$text || '[Error: ' . $self->error . ']'
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
16 3 };
17  
18 3 has what => ( is => 'rw', default => 'paras' );
19 3 has start => ( is => 'rw', default => 1 );
20 3 has amount => ( is => 'rw', default => 5 );
21 3 has html => ( is => 'rw', default => 0 );
22 3 has lipsum => ( is => 'rw', build_args => undef );
23  
24 3 has error => ( is => 'rw', build_arg => undef );
25 2 ▶has _ua => ( is => 'ro', build_arg => undef, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 return LWP::UserAgent->new( timeout => 30,
27 agent => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) '
28 . 'Gecko/20100101 Firefox/26.0'
29 );
30 3 });
31  
32 7 ▶sub generate {
Subroutine generate: called CC=8 SCAR=21.5
Pod documented
33 7 my $self = shift;
34 7 $self->lipsum(undef);
35  
36 7 $self->_prep_args( @_ );
37  
38 # sometimes we fail to get decent Lipsum content,
39 # so let's retry once in that case and bail out if we still fail
40 7 my $did_already_retry = 0;
41 7 my ( $res, $dom );
42 GET_LIPSUM: {
43 7 ▶ $res = $self->_ua->post( 'https://lipsum.com/feed/html', {
Branch: 1 branch
BranchTrueFalseTotal
$self->start ? : 2 5 7
44 amount => $self->amount,
45 what => $self->what,
46 start => $self->start ? 1 : 0,
47 generate => 'Generate Lorem Ipsum',
48 });
49  
50 7 ▶ return $self->_set_error( 'Network error: ' . $res->status_line )
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 7 7
51 unless $res->is_success;
52  
53 7 $dom = eval {
54 7 Mojo::DOM->new( $res->decoded_content )
55 ->find('#lipsum')
56 ->first
57 ->children;
58 };
59 7 ▶ @$ and return $self->_set_error("Parsing error: $@");
Branch: 1 branch
BranchTrueFalseTotal
@$ and return $self->_set_error("Parsing error: $@") 0 7 7
60 7 ▶ unless ( $dom ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dom) 0 7 7
61 0 ▶ redo GET_LIPSUM unless $did_already_retry;
Branch: 1 branch
BranchTrueFalseTotal
unless $did_already_retry 0 0 0
62 0 $did_already_retry = 1;
63  
64 0 return $self->_set_error(
65 'Unknown error. HTML we got from Lipsum is: '
66 . $res->decoded_content
67 );
68 }
69 }
70  
71 $self->html
72 or return $self->lipsum(
73 10 join "\n\n", map { my $t = $_->all_text; $t =~ s/^\s+|\s+$//g; $t }
74 7 ▶ $dom->grep(sub{ $_->tag eq 'p'})->each
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->html or return $self->lipsum(join("\n\n", map({my $t = $_->all_text; $t =~ s/^\s+|\s+$//g; $t;} $dom->grep(sub { $_->tag eq "p"; } )->each))) 2 5 7
75 );
76  
77 2 my $html = join '', $dom->each;
78  
79 # a little hackery to get rid of lipsum.com's invalid markup
80 2 ▶ $self->what eq 'lists' and $html =~ s{<p>|</p>}{}gi;
Branch: 1 branch
BranchTrueFalseTotal
$self->what eq "lists" and $html =~ s[<p>|</p>][]gi 1 1 2
81 2 $html =~ s/^\s+|\s+$//g;
82  
83 2 return $self->lipsum( $html );
84 }
85  
86 0 ▶sub _set_error {
Subroutine _set_error: not called CC=1 SCAR=6.9
87 0 my ( $self, $error ) = @_;
88 0 $self->error( $error );
89 0 return;
90 }
91  
92 7 ▶sub _prep_args {
Subroutine _prep_args: called CC=8 SCAR=30.1
93 7 my $self = shift;
94 7 my %args = @_;
95  
96 $self->start( $args{start} )
97 7 ▶ if exists $args{start};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"start"} 0 7 7
98  
99 $self->html( $args{html} )
100 7 ▶ if exists $args{html};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"html"} 0 7 7
101  
102 7 ▶ if ( defined $args{what} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"what"}) 0 7 7
103 croak q{Argument 'what' must be one of 'paras', 'words', 'bytes',}
104 . q{ or 'lists'}
105 0 ▶ unless $args{what} =~ /\A(paras|words|bytes|lists)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"what"} =~ /\A(paras|words|bytes|lists)\z/ 0 0 0
106  
107 0 $self->what( $args{what} );
108 }
109  
110 7 ▶ if ( defined $args{amount} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"amount"}) 0 7 7
111 croak q{Argument 'amount' must contain a positive integer}
112 0 ▶ unless $args{amount} and $args{amount} =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"amount"} and $args{"amount"} =~ /\A\d+\z/ 0 0 0
Condition0%0/3
$args{"amount"} and $args{"amount"} =~ /\A\d+\z/
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"amount"} and $args{"amount"} =~ /\A\d+\z/
$args{"amount"} $args{"amount"} =~ /\A\d+\z/
Truth table0%0/3
A and B
A= $args{"amount"}B= $args{"amount"} =~ /\A\d+\z/
ABresult
0X0
100
111
113  
114 0 $self->amount( $args{amount} );
115 }
116  
117 7 return;
118 }
119  
120 3 q|
121 0 bottles of beer on the wall, 0 bottles of beer! You take one down,
122 and pass it around, 4294967295 bottles of beer on the wall!
123 |;
124  
125 __END__
↑ Summary