| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Lipsum; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 265607 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 64 |  | 
| 4 | 2 |  |  | 2 |  | 8 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 79 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '1.001010'; # VERSION | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 8 | use Carp qw/croak/; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 98 |  | 
| 9 | 2 |  |  | 2 |  | 617 | use LWP::UserAgent; | 
|  | 2 |  |  |  |  | 34572 |  | 
|  | 2 |  |  |  |  | 54 |  | 
| 10 | 2 |  |  | 2 |  | 492 | use Mojo::DOM; | 
|  | 2 |  |  |  |  | 59997 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 11 | 2 |  |  | 2 |  | 526 | use Moo; | 
|  | 2 |  |  |  |  | 10550 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 12 |  |  |  |  |  |  | use overload q|""| => sub { | 
| 13 | 6 |  |  | 6 |  | 3628 | my $self = shift; | 
| 14 | 6 |  |  |  |  | 21 | my $text = $self->generate; | 
| 15 | 6 |  | 33 |  |  | 2263 | return $text || '[Error: ' . $self->error . ']'; | 
| 16 | 2 |  |  | 2 |  | 1403 | }; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 18 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | has what   => ( is => 'rw', default => 'paras'  ); | 
| 19 |  |  |  |  |  |  | has start  => ( is => 'rw', default => 1        ); | 
| 20 |  |  |  |  |  |  | has amount => ( is => 'rw', default => 5        ); | 
| 21 |  |  |  |  |  |  | has html   => ( is => 'rw', default => 0        ); | 
| 22 |  |  |  |  |  |  | has lipsum => ( is => 'rw', build_args => undef ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | has error => ( is => 'rw', build_arg => undef ); | 
| 25 |  |  |  |  |  |  | has _ua    => ( is => 'ro', build_arg => undef, default => sub { | 
| 26 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | }); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub generate { | 
| 33 | 7 |  |  | 7 | 1 | 1540 | my $self = shift; | 
| 34 | 7 |  |  |  |  | 20 | $self->lipsum(undef); | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 7 |  |  |  |  | 27 | $self->_prep_args( @_ ); | 
| 37 | 7 | 100 |  |  |  | 103 | my $res = $self->_ua->post( 'http://lipsum.com/feed/html', { | 
| 38 |  |  |  |  |  |  | amount => $self->amount, | 
| 39 |  |  |  |  |  |  | what   => $self->what, | 
| 40 |  |  |  |  |  |  | start  => $self->start ? 1 : 0, | 
| 41 |  |  |  |  |  |  | generate => 'Generate Lorem Ipsum', | 
| 42 |  |  |  |  |  |  | }); | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 7 | 50 |  |  |  | 2203380 | return $self->_set_error( 'Network error: ' . $res->status_line ) | 
| 45 |  |  |  |  |  |  | unless $res->is_success; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 7 |  |  |  |  | 119 | my $dom = eval { | 
| 48 | 7 |  |  |  |  | 53 | Mojo::DOM->new( $res->decoded_content ) | 
| 49 |  |  |  |  |  |  | ->find('#lipsum') | 
| 50 |  |  |  |  |  |  | ->first | 
| 51 |  |  |  |  |  |  | ->children; | 
| 52 |  |  |  |  |  |  | }; | 
| 53 | 7 | 50 |  |  |  | 211999 | @$ and return $self->_set_error("Parsing error: $@"); | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | $self->html | 
| 56 |  |  |  |  |  |  | or return $self->lipsum( | 
| 57 |  |  |  |  |  |  | join "\n\n", map $_->all_text, | 
| 58 | 7 | 100 |  | 10 |  | 122 | $dom->grep(sub{ $_->tag eq 'p'})->each | 
|  | 10 |  |  |  |  | 327 |  | 
| 59 |  |  |  |  |  |  | ); | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 2 |  |  |  |  | 14 | my $html = join '', $dom->each; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # a little hackery to get rid of lipsum.com's invalid markup | 
| 64 | 2 | 100 |  |  |  | 37611 | $self->what eq 'lists' and $html =~ s{ |}{}gi; | 
| 65 | 2 |  |  |  |  | 12986 | $html =~ s/^\s+|\s+$//g; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 2 |  |  |  |  | 1505 | return $self->lipsum( $html ); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub _set_error { | 
| 71 | 0 |  |  | 0 |  | 0 | my ( $self, $error ) = @_; | 
| 72 | 0 |  |  |  |  | 0 | $self->error( $error ); | 
| 73 | 0 |  |  |  |  | 0 | return; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub _prep_args { | 
| 77 | 7 |  |  | 7 |  | 10 | my $self = shift; | 
| 78 | 7 |  |  |  |  | 14 | my %args = @_; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 7 | 50 |  |  |  | 26 | $self->start( $args{start} ) | 
| 81 |  |  |  |  |  |  | if exists $args{start}; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 7 | 50 |  |  |  | 19 | $self->html( $args{html} ) | 
| 84 |  |  |  |  |  |  | if exists $args{html}; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 7 | 50 |  |  |  | 24 | if ( defined $args{what} ) { | 
| 87 | 0 | 0 |  |  |  | 0 | croak q{Argument 'what' must be one of 'paras', 'words', 'bytes',} | 
| 88 |  |  |  |  |  |  | . q{ or 'lists'} | 
| 89 |  |  |  |  |  |  | unless $args{what} =~ /\A(paras|words|bytes|lists)\z/; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  | 0 | $self->what( $args{what} ); | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 7 | 50 |  |  |  | 17 | if ( defined $args{amount} ) { | 
| 95 | 0 | 0 | 0 |  |  | 0 | croak q{Argument 'amount' must contain a positive integer} | 
| 96 |  |  |  |  |  |  | unless $args{amount} and $args{amount} =~ /\A\d+\z/; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 |  |  |  |  | 0 | $self->amount( $args{amount} ); | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 7 |  |  |  |  | 12 | return; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | q| | 
| 105 |  |  |  |  |  |  | 0 bottles of beer on the wall, 0 bottles of beer! You take one down, | 
| 106 |  |  |  |  |  |  | and pass it around, 4294967295 bottles of beer on the wall! | 
| 107 |  |  |  |  |  |  | |; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | __END__ |