File Coverage

blib/lib/WWW/Crawl4AI/Strategy/Callback.pm
Criterion Covered Total %
statement 17 17 100.0
branch 5 6 83.3
condition 2 6 33.3
subroutine 6 6 100.0
pod 4 4 100.0
total 34 39 87.1


line stmt bran cond sub pod time code
1             package WWW::Crawl4AI::Strategy::Callback;
2             # ABSTRACT: last-resort Crawl4AI strategy delegating to a user coderef
3 2     2   10 use Moo;
  2         3  
  2         9  
4             with 'WWW::Crawl4AI::Strategy';
5 2     2   654 use WWW::Crawl4AI::Markdown qw( resolve_markdown_chain );
  2         4  
  2         419  
6              
7             our $VERSION = '0.001';
8              
9              
10 13     13 1 49 sub name { 'external_callback' }
11              
12              
13 2     2 1 37 sub cost_class { 'paid' }
14              
15              
16             sub applicable {
17 6     6 1 10 my ( $self, $crawler ) = @_;
18 6 100       34 return $crawler->callback ? 1 : 0;
19             }
20              
21              
22             # Does not go through Crawl4AI at all — hand the URL to the user's coderef and
23             # normalize whatever page-shaped hashref it returns. We accept markdown either
24             # as a plain string or as Crawl4AI's structured object, so Detect and Result
25             # see the same shape they get from every (Crawl4AI-backed) strategy.
26             sub crawl {
27 2     2 1 5 my ( $self, $crawler, $url, %opts ) = @_;
28 2         9 my $page = $crawler->callback->( $url, %opts );
29 2 50       13 return undef unless ref $page eq 'HASH';
30 2 100       11 $page->{markdown} = resolve_markdown_chain( $page->{markdown} ) if ref $page->{markdown} eq 'HASH';
31 2   33     9 $page->{url} //= $url;
32 2   33     9 $page->{final_url} //= $page->{url};
33 2         4 return $page;
34             }
35              
36              
37             1;
38              
39             __END__