Acme/MetaSyntactic/RemoteList.pm

filter: Statementstmt 90.9% 70 / 77 Branchbran 63.6% 14 / 22 Conditioncond 41.6% 10 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 93.7% 15 / 16 Podpod 100% 8 / 8 totaltotal 71.7% 117 / 163 CC 21 SCAR 20.8
CC
21
Cov
76%
remote_list
14.6
sources
12.4
extract
3.0
CRAP 8.0
« Acme/MetaSyntactic/Locale.pm ↑ Summary Acme/MetaSyntactic/MultiList.pm »
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]
l!l&&r!l&&!r
120
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
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
BranchTrueFalseTotal
ref $func eq 'CODE' ? : 2 1 3
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]
l!l&&r!l&&!r
6130
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
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]
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (ref $src eq 'ARRAY') { } 3 3 6
elsif (ref $src eq 'HASH') { } 0 3 3
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
BranchTrueFalseTotal
defined $_[1] && $_[1] ne ':all' ? : 0 0 0
ref $_[1] ? : 0 0 0
Condition0%0/3
defined $_[1] and $_[1] ne ":all"
!ll&&!rl&&r
000
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"
ABresult
0X0
100
111
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]
l!l&&r!l&&!r
520
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
61 7 ▶ return unless $class->has_remotelist();
Branch: 1 branch
BranchTrueFalseTotal
unless $class->has_remotelist 1 6 7
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
BranchTrueFalseTotal
if $LWP::VERSION < "5.802" 0 6 6
67 if $LWP::VERSION < 5.802;
68 };
69 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
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
BranchTrueFalseTotal
ref $_[0] ? : 4 2 6
Condition33%1/3
$_[1] or $_[0]{"category"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$_[1] or $_[0]{"category"}
$_[1] $_[0]{"category"}
Truth table33%1/3
A or B
A= $_[1]B= $_[0]{"category"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
ref $src ? : 0 8 8
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
BranchTrueFalseTotal
unless ($res->is_success) 2 6 8
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
l!l&&r!l&&!r
600
$category || ()
l!l&&r!l&&!r
060
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
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $categoryB= ()
ABresult
1X1
011
000
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__
« Acme/MetaSyntactic/Locale.pm ↑ Summary Acme/MetaSyntactic/MultiList.pm »