File Coverage

blib/lib/Software/Catalog/Util.pm
Criterion Covered Total %
statement 14 39 35.9
branch 0 10 0.0
condition 0 3 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 20 59 33.9


line stmt bran cond sub pod time code
1             package Software::Catalog::Util;
2              
3 1     1   261101 use 5.010001;
  1         4  
4 1     1   4 use strict;
  1         1  
  1         22  
5 1     1   10 use warnings;
  1         2  
  1         68  
6 1     1   1594 use Log::ger;
  1         46  
  1         5  
7              
8             our %SPEC;
9              
10 1     1   208 use Exporter qw(import);
  1         2  
  1         432  
11              
12             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13             our $DATE = '2024-07-17'; # DATE
14             our $DIST = 'Software-Catalog'; # DIST
15             our $VERSION = '1.0.8'; # VERSION
16              
17             our @EXPORT_OK = qw(
18             extract_from_url
19             );
20              
21             $SPEC{extract_from_url} = {
22             v => 1.1,
23             args => {
24             url => {
25             schema => 'url*',
26             req => 1,
27             pos => 0,
28             },
29             re => {
30             schema => 're*',
31             },
32             code => {
33             schema => 'code*',
34             },
35             all => {
36             schema => 'bool*',
37             },
38             agent => {
39             schema => 'str*',
40             },
41             },
42             args_rels => {
43             req_one => [qw/re code/],
44             },
45             };
46             sub extract_from_url {
47 0     0 1   state $ua = do {
48 0           require LWP::UserAgent;
49 0           LWP::UserAgent->new;
50             };
51 0           state $orig_agent = $ua->agent;
52 0           my %args = @_;
53              
54 0   0       $ua->agent( $args{agent} || $orig_agent);
55 0           my $lwp_res = $ua->get($args{url});
56 0 0         unless ($lwp_res->is_success) {
57 0 0         return [$lwp_res->code, "Couldn't retrieve URL '$args{url}'" . (
58             $lwp_res->message ? ": " . $lwp_res->message : "")];
59             }
60              
61 0           my $res;
62 0 0         if ($args{re}) {
63 0           log_trace "Finding version from $args{url} using regex $args{re} ...";
64 0 0         if ($args{all}) {
65 0           my $content = $lwp_res->content;
66 0           my %m;
67 0           while ($content =~ /$args{re}/g) {
68 0           $m{$1}++;
69             }
70 0           $res = [200, "OK (all)", [sort keys %m]];
71             } else {
72 0 0         if ($lwp_res->content =~ $args{re}) {
73 0           $res = [200, "OK", $1];
74             } else {
75 0           $res = [543, "Couldn't match pattern $args{re} against ".
76             "content of URL '$args{url}'"];
77             }
78             }
79             } else {
80 0           log_trace "Finding version from $args{url} using code ...";
81 0           $res = $args{code}->(
82             content => $lwp_res->content, _lwp_res => $lwp_res);
83             }
84 0           log_trace "Result: %s", $res;
85 0           $res;
86             }
87              
88             1;
89             # ABSTRACT: Utility routines
90              
91             __END__