blib/lib/WWW/Lipsum.pm

filter: Statementstmt 75.3% 52 / 69 Branchbran 26.9% 7 / 26 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 92.3% 12 / 13 Podpod 100% 1 / 1 totaltotal 62.3% 73 / 117 SCAR 34.8
CC
16
Cov
60%
generate
28.6
_prep_args
20.3
__ANON__
2.0
CRAP 32.4
↑ 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.001012'; # 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 use overload q|""| => sub {
13 6 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
14 6 my $text = $self->generate;
15 6 ▶ return $text || '[Error: ' . $self->error . ']';
Condition50%1/2
$text || '[Error: ' . $self->error . ']'
l!l
06
MC/DC0%0/1
$text || '[Error: ' . $self->error . ']'
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
16 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
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 has _ua => ( is => 'ro', build_arg => undef, default => sub {
26 2 ▶ return LWP::UserAgent->new( timeout => 30,
Subroutine __ANON__: called CC=1 SCAR=0.0
27 agent => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) '
28 . 'Gecko/20100101 Firefox/26.0'
29 );
30 3 });
31  
32 sub generate {
33 7 ▶ my $self = shift;
Subroutine generate: called CC=8 SCAR=33.5
Pod documented
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( 'http://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 7 0 7
51 unless $res->is_success;
52  
53 0 $dom = eval {
54 0 Mojo::DOM->new( $res->decoded_content )
55 ->find('#lipsum')
56 ->first
57 ->children;
58 };
59 0 ▶ @$ and return $self->_set_error("Parsing error: $@");
Branch: 1 branch
BranchTrueFalseTotal
@$ and return $self->_set_error("Parsing error: $@") 0 0 0
60 0 ▶ unless ( $dom ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dom) 0 0 0
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 join "\n\n", map $_->all_text,
74 0 ▶ $dom->grep(sub{ $_->tag eq 'p'})->each
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->html or return $self->lipsum(join("\n\n", map($_->all_text, $dom->grep(sub { $_->tag eq "p"; } )->each))) 0 0 0
75 );
76  
77 0 my $html = join '', $dom->each;
78  
79 # a little hackery to get rid of lipsum.com's invalid markup
80 0 ▶ $self->what eq 'lists' and $html =~ s{<p>|</p>}{}gi;
Branch: 1 branch
BranchTrueFalseTotal
$self->what eq "lists" and $html =~ s[<p>|</p>][]gi 0 0 0
81 0 $html =~ s/^\s+|\s+$//g;
82  
83 0 return $self->lipsum( $html );
84 }
85  
86 sub _set_error {
87 7 ▶ my ( $self, $error ) = @_;
Subroutine _set_error: called CC=1 SCAR=0.0
88 7 $self->error( $error );
89 7 return;
90 }
91  
92 sub _prep_args {
93 7 ▶ my $self = shift;
Subroutine _prep_args: called CC=8 SCAR=30.1
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