| 1 | =encoding iso-8859-1 | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | =cut | ||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | package Acme::MetaSyntactic::RemoteList; | ||||||||||||||||||||||||||||||||||||||
| 6 | 45 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 45 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 45 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 45 | our $VERSION = '1.003'; | |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | # method that extracts the items from the remote content and returns them | ||||||||||||||||||||||||||||||||||||||
| 13 | sub extract { | ||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | my $class = ref $_[0] || $_[0]; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
extract: called CC=3 SCAR=11.0
Pod
documented
Condition66%2/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||
| 15 | 45 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | 3 | my $func = ${"$class\::Remote"}{extract}; | |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | # provide a very basic default | ||||||||||||||||||||||||||||||||||||||
| 19 | my $meth = ref $func eq 'CODE' | ||||||||||||||||||||||||||||||||||||||
| 20 | 10 | ▶ | ? sub { my %seen; return grep { !$seen{$_}++ } $func->( $_[1], $_[2] ); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | : sub { return $_[1] }; # very basic default | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | # put the method in the subclass symbol table (at runtime) | ||||||||||||||||||||||||||||||||||||||
| 24 | 3 | *{"$class\::extract"} = $meth; | |||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | # now run the function^Wmethod | ||||||||||||||||||||||||||||||||||||||
| 27 | 3 | goto &$meth; | |||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | # methods related to the source URL | ||||||||||||||||||||||||||||||||||||||
| 31 | sub source { | ||||||||||||||||||||||||||||||||||||||
| 32 | 19 | ▶ | my $class = ref $_[0] || $_[0]; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
source: called CC=2 SCAR=7.0
Pod
documented
Condition66%2/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||
| 33 | 45 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | 19 | return ${"$class\::Remote"}{source}; | |||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | sub sources { | ||||||||||||||||||||||||||||||||||||||
| 39 | 6 | ▶ | my $class = ref $_[0] || $_[0]; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sources: called CC=7 SCAR=25.2
Pod
documented
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||
| 40 | 45 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 6 | my $src = ${"$class\::Remote"}{source}; | |||||||||||||||||||||||||||||||||||||
| 43 | 6 | ▶ | if ( ref $src eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 44 | 3 | return @$src; | |||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||
| 46 | elsif ( ref $src eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||
| 47 | return grep $_, | ||||||||||||||||||||||||||||||||||||||
| 48 | defined $_[1] && $_[1] ne ':all' | ||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ? ref $_[1] ? @$src{ @{ $_[1] } } | |||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | : $src->{ $_[1] } | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $_[1] and $_[1] ne ":all"
MC/DC0%0/2
defined $_[1] and $_[1] ne ":all"
defined $_[1]
$_[1] ne ":all"
Truth table0%0/3
A and B
A= defined $_[1]B= $_[1] ne ":all"
| |||||||||||||||||||||||||||||||||||||||
| 51 | : values %$src; | ||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||
| 53 | 3 | return $src; | |||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | 15 | ▶ | sub has_remotelist { return defined $_[0]->source(); } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
has_remotelist: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | # main method: return the list from the remote source | ||||||||||||||||||||||||||||||||||||||
| 59 | sub remote_list { | ||||||||||||||||||||||||||||||||||||||
| 60 | 7 | ▶ | my $class = ref $_[0] || $_[0]; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
remote_list: called CC=12 SCAR=26.8
Pod
documented
Condition66%2/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | return unless $class->has_remotelist(); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | # check that we can access the network | ||||||||||||||||||||||||||||||||||||||
| 64 | 6 | eval { | |||||||||||||||||||||||||||||||||||||
| 65 | 6 | require LWP::UserAgent; | |||||||||||||||||||||||||||||||||||||
| 66 | 6 | ▶ | die "version 5.802 required ($LWP::VERSION installed)\n" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 67 | if $LWP::VERSION < 5.802; | ||||||||||||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 70 | 0 | carp "LWP::UserAgent not available: $@"; | |||||||||||||||||||||||||||||||||||||
| 71 | 0 | return; | |||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | # figure out the default category (for an instance) | ||||||||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | my $category = ref $_[0] ? $_[1] || $_[0]->{category} : $_[1]; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[1] or $_[0]{"category"}
MC/DC0%0/2
$_[1] or $_[0]{"category"}
$_[1]
$_[0]{"category"}
Truth table33%1/3
A or B
A= $_[1]B= $_[0]{"category"}
| |||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | # fetch the content | ||||||||||||||||||||||||||||||||||||||
| 78 | 6 | my @items; | |||||||||||||||||||||||||||||||||||||
| 79 | 6 | my @srcs = $class->sources($category); | |||||||||||||||||||||||||||||||||||||
| 80 | 6 | my $ua = LWP::UserAgent->new( env_proxy => 1 ); | |||||||||||||||||||||||||||||||||||||
| 81 | 6 | foreach my $src (@srcs) { | |||||||||||||||||||||||||||||||||||||
| 82 | 8 | ▶ | my $request = HTTP::Request->new( | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 83 | ref $src | ||||||||||||||||||||||||||||||||||||||
| 84 | ? ( POST => $src->[0], | ||||||||||||||||||||||||||||||||||||||
| 85 | [ content_type => 'application/x-www-form-urlencoded' ], | ||||||||||||||||||||||||||||||||||||||
| 86 | $src->[1] | ||||||||||||||||||||||||||||||||||||||
| 87 | ) | ||||||||||||||||||||||||||||||||||||||
| 88 | : ( GET => $src ) | ||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | 8 | my $res = $ua->request( $request ); | |||||||||||||||||||||||||||||||||||||
| 92 | 8 | ▶ | if ( ! $res->is_success() ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 93 | 2 | carp "Failed to get content at $src (" . $res->status_line(); | |||||||||||||||||||||||||||||||||||||
| 94 | 2 | return; | |||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | # extract, cleanup and return the data | ||||||||||||||||||||||||||||||||||||||
| 98 | # if decoding the content fails, we just deal with the raw content | ||||||||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | push @items => | ||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$res->decoded_content || $res->content
$category || ()
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content
$res->content
MC/DC0%0/2
$category || ()
$category
()
Truth table33%1/3
A || B
A= $res->decoded_contentB= $res->content
Truth table33%1/3
A || B
A= $categoryB= ()
| |||||||||||||||||||||||||||||||||||||||
| 100 | $class->extract( $res->decoded_content() || $res->content(), | ||||||||||||||||||||||||||||||||||||||
| 101 | $category || () ); | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | # return unique items | ||||||||||||||||||||||||||||||||||||||
| 106 | 4 | my %seen; | |||||||||||||||||||||||||||||||||||||
| 107 | 4 | return grep { !$seen{$_}++ } @items; | |||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | # | ||||||||||||||||||||||||||||||||||||||
| 111 | # transformation subroutines | ||||||||||||||||||||||||||||||||||||||
| 112 | # | ||||||||||||||||||||||||||||||||||||||
| 113 | sub tr_nonword { | ||||||||||||||||||||||||||||||||||||||
| 114 | 227 | ▶ | my $str = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
tr_nonword: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 115 | 227 | $str =~ tr/a-zA-Z0-9_/_/c; | |||||||||||||||||||||||||||||||||||||
| 116 | 227 | $str; | |||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | sub tr_accent { | ||||||||||||||||||||||||||||||||||||||
| 120 | 227 | ▶ | my $str = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
tr_accent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 121 | 227 | $str =~ tr{ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ} | |||||||||||||||||||||||||||||||||||||
| 122 | {AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy}; | ||||||||||||||||||||||||||||||||||||||
| 123 | 227 | return $str; | |||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | 45 | my %utf2asc = ( | |||||||||||||||||||||||||||||||||||||
| 127 | "æ" => 'ae', | ||||||||||||||||||||||||||||||||||||||
| 128 | "Æ" => 'AE', | ||||||||||||||||||||||||||||||||||||||
| 129 | "\xc5\xa0" => 'S', | ||||||||||||||||||||||||||||||||||||||
| 130 | "\x{0160}" => 'S', | ||||||||||||||||||||||||||||||||||||||
| 131 | # for pokemons | ||||||||||||||||||||||||||||||||||||||
| 132 | "\x{0101}" => 'a', | ||||||||||||||||||||||||||||||||||||||
| 133 | "\x{012b}" => 'i', | ||||||||||||||||||||||||||||||||||||||
| 134 | "\x{014d}" => 'o', | ||||||||||||||||||||||||||||||||||||||
| 135 | "\x{016b}" => 'u', | ||||||||||||||||||||||||||||||||||||||
| 136 | "\xe2\x99\x80" => 'female', | ||||||||||||||||||||||||||||||||||||||
| 137 | "\xe2\x99\x82" => 'male', | ||||||||||||||||||||||||||||||||||||||
| 138 | "\x{2640}" => 'female', | ||||||||||||||||||||||||||||||||||||||
| 139 | "\x{2642}" => 'male', | ||||||||||||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||||||||||||
| 141 | 45 | my $utf_re = qr/(@{[join( '|', sort keys %utf2asc )]})/; | |||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | sub tr_utf8_basic { | ||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my $str = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
tr_utf8_basic: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 145 | 0 | $str =~ s/$utf_re/$utf2asc{$1}/go; | |||||||||||||||||||||||||||||||||||||
| 146 | 0 | return $str; | |||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||
| 149 | 45 | 1; | |||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||||||||||||