| 1 | package WWW::Crawl4AI::Result; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: normalized result of a WWW::Crawl4AI strategy chain | ||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use Moo; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use URI (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 7 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 0 | ▶ | has ok => ( is => 'ro', default => sub { 0 } ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||
| 13 | 7 | has url => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 7 | has final_url => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | 7 | has status => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 7 | has markdown => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 7 | has html => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 7 | has title => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | 7 | has backend => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||
| 34 | 7 | has cost_class => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | has signals => ( is => 'ro', default => sub { {} } ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | 7 | has why_failed => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | 7 | has error => ( is => 'ro' ); | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | has attempts => ( is => 'ro', default => sub { [] } ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | has links => ( is => 'ro', default => sub { { internal => [], external => [] } } ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | has response_headers => ( is => 'ro', default => sub { {} } ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | has _json => ( | ||||||||||||||||||||||||||||||
| 56 | is => 'lazy', | ||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | default => sub { JSON::MaybeXS->new( utf8 => 0, canonical => 1, convert_blessed => 1 ) }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 58 | 7 | ); | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | 31 | ▶ | sub from_attempt { | ||||||||||||||||||||||||||||
|
Subroutine
from_attempt: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 61 | 31 | my ( $class, $attempt, %extra ) = @_; | |||||||||||||||||||||||||||||
| 62 | 31 | ▶ | my $page = $attempt->page || {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$attempt->page || {}
MC/DC0%0/1
$attempt->page || {}
$attempt->page
Truth table50%1/2
A
A= $attempt->page
| |||||||||||||||||||||||||||||||
| 63 | return $class->new( | ||||||||||||||||||||||||||||||
| 64 | ok => $attempt->ok, | ||||||||||||||||||||||||||||||
| 65 | url => $page->{url}, | ||||||||||||||||||||||||||||||
| 66 | final_url => $page->{final_url} // $page->{url}, | ||||||||||||||||||||||||||||||
| 67 | status => $page->{status_code}, | ||||||||||||||||||||||||||||||
| 68 | markdown => $page->{markdown}, | ||||||||||||||||||||||||||||||
| 69 | html => $page->{html}, | ||||||||||||||||||||||||||||||
| 70 | title => $page->{title}, | ||||||||||||||||||||||||||||||
| 71 | backend => $attempt->backend, | ||||||||||||||||||||||||||||||
| 72 | cost_class => $attempt->cost_class, | ||||||||||||||||||||||||||||||
| 73 | signals => $attempt->signals, | ||||||||||||||||||||||||||||||
| 74 | why_failed => $attempt->why_failed, | ||||||||||||||||||||||||||||||
| 75 | links => $page->{links} // { internal => [], external => [] }, | ||||||||||||||||||||||||||||||
| 76 | 31 | ▶ | response_headers => _lc_headers( $page->{response_headers} ), | ||||||||||||||||||||||||||||
|
Condition80%4/5
$page->{"final_url"} // $page->{"url"}
$page->{"links"} // {"internal", [], "external", []}
MC/DC50%1/2
$page->{"final_url"} // $page->{"url"}
$page->{"final_url"}
$page->{"url"}
MC/DC100%1/1
$page->{"links"} // {"internal", [], "external", []}
$page->{"links"}
Truth table66%2/3
A // B
A= $page->{"final_url"}B= $page->{"url"}
Truth table100%2/2
A
A= $page->{"links"}
| |||||||||||||||||||||||||||||||
| 77 | %extra, | ||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | sub attempt_count { scalar @{ $_[0]->attempts } } | ||||||||||||||||||||||||||||
|
Subroutine
attempt_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 14 | ▶ | sub internal_links { $_[0]->links->{internal} || [] } | ||||||||||||||||||||||||||||
|
Subroutine
internal_links: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | 14 | ▶ | sub external_links { $_[0]->links->{external} || [] } | ||||||||||||||||||||||||||||
|
Subroutine
external_links: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | 13 | ▶ | sub urls { | ||||||||||||||||||||||||||||
|
Subroutine
urls: called CC=11 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 92 | 13 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 93 | 13 | ▶ | my $base = $self->final_url // $self->url; | ||||||||||||||||||||||||||||
|
Condition33%1/3
$self->final_url // $self->url
MC/DC0%0/2
$self->final_url // $self->url
$self->final_url
$self->url
Truth table33%1/3
A // B
A= $self->final_urlB= $self->url
| |||||||||||||||||||||||||||||||
| 94 | 13 | my ( %seen, @urls ); | |||||||||||||||||||||||||||||
| 95 | 13 | for my $link ( @{ $self->internal_links }, @{ $self->external_links } ) { | |||||||||||||||||||||||||||||
| 96 | 22 | ▶ | my $href = ref $link eq 'HASH' ? $link->{href} : $link; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 97 | 22 | ▶ | next unless defined $href && length $href; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $href and length $href
MC/DC0%0/2
defined $href and length $href
defined $href
length $href
Truth table33%1/3
A and B
A= defined $hrefB= length $href
| |||||||||||||||||||||||||||||||
| 98 | 22 | ▶ | next if $href =~ m{\A(?:javascript|mailto|tel|data):}i; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | 20 | ▶ | next if $href =~ /\A#/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 100 | 19 | ▶ | my $abs = $base ? URI->new_abs( $href, $base )->as_string : $href; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 101 | 19 | ▶ | next unless $abs =~ m{\Ahttps?://}i; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 102 | 19 | ▶ | next if $seen{$abs}++; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 103 | 18 | push @urls, $abs; | |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||
| 105 | 13 | return \@urls; | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub to_hash { | ||||||||||||||||||||||||||||
|
Subroutine
to_hash: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 110 | 2 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 111 | return { | ||||||||||||||||||||||||||||||
| 112 | ok => $self->ok ? \1 : \0, | ||||||||||||||||||||||||||||||
| 113 | url => $self->url, | ||||||||||||||||||||||||||||||
| 114 | final_url => $self->final_url, | ||||||||||||||||||||||||||||||
| 115 | status => $self->status, | ||||||||||||||||||||||||||||||
| 116 | backend => $self->backend, | ||||||||||||||||||||||||||||||
| 117 | cost_class => $self->cost_class, | ||||||||||||||||||||||||||||||
| 118 | title => $self->title, | ||||||||||||||||||||||||||||||
| 119 | markdown => $self->markdown, | ||||||||||||||||||||||||||||||
| 120 | signals => $self->signals, | ||||||||||||||||||||||||||||||
| 121 | why_failed => $self->why_failed, | ||||||||||||||||||||||||||||||
| 122 | links => $self->links, | ||||||||||||||||||||||||||||||
| 123 | urls => $self->urls, | ||||||||||||||||||||||||||||||
| 124 | response_headers => $self->response_headers, | ||||||||||||||||||||||||||||||
| 125 | 0 | ( defined $self->error ? ( error => "@{[ $self->error ]}" ) : () ), | |||||||||||||||||||||||||||||
| 126 | 2 | ▶ | attempts => [ map { $_->to_hash } @{ $self->attempts } ], | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | sub TO_JSON { $_[0]->to_hash } | ||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | 1 | ▶ | sub attempts_json { | ||||||||||||||||||||||||||||
|
Subroutine
attempts_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 134 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||||
| 135 | 1 | return $self->_json->encode( [ map { $_->to_hash } @{ $self->attempts } ] ); | |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | # Lowercase all header keys for deterministic, case-insensitive matching by callers. | ||||||||||||||||||||||||||||||
| 140 | 31 | ▶ | sub _lc_headers { | ||||||||||||||||||||||||||||
|
Subroutine
_lc_headers: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 141 | 31 | my ($h) = @_; | |||||||||||||||||||||||||||||
| 142 | 31 | ▶ | return {} unless ref $h eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 143 | 1 | return { map { lc($_) => $h->{$_} } keys %$h }; | |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | 7 | 1; | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||