DDG/Meta/Information.pm

filter: Statementstmt 100% 120 / 120 Branchbran 71.8% 23 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 21 / 21 Podpod 50.0% 1 / 2 totaltotal 93.3% 168 / 180 CC 19 SCAR 7.8
CC
19
Cov
94%
__ANON__
3.0
__ANON__
3.0
__ANON__
3.0
CRAP 2.2
« DDG/Test/Location.pm ↑ Summary DDG/HasShareDir.pm »
1 package DDG::Meta::Information;
2 11 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: DDG plugin meta information storage
4 11 $DDG::Meta::Information::VERSION = '1017';
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 require Moo::Role;
11  
12 11 my %supported_types = (
13 email => [ 'mailto:{{a}}', '{{b}}' ],
14 twitter => [ 'https://twitter.com/{{a}}', '@{{b}}' ],
15 web => [ '{{a}}', '{{b}}' ],
16 github => [ 'https://github.com/{{a}}', '{{b}}' ],
17 facebook => [ 'https://facebook.com/{{a}}', '{{b}}' ],
18 cpan => [ 'https://metacpan.org/author/{{a}}', '{{a}}' ],
19 );
20  
21 11 my @supported_categories = qw(
22 bang
23 calculations
24 cheat_sheets
25 computing_info
26 computing_tools
27 conversions
28 dates
29 entertainment
30 facts
31 finance
32 food
33 formulas
34 forums
35 geography
36 ids
37 language
38 location_aware
39 physical_properties
40 programming
41 q/a
42 random
43 reference
44 special
45 software
46 time_sensitive
47 transformations
48 );
49  
50 11 my @supported_topics = qw(
51 everyday
52 economy_and_finance
53 computing
54 cryptography
55 entertainment
56 food_and_drink
57 gaming
58 geek
59 geography
60 math
61 music
62 programming
63 science
64 social
65 special_interest
66 sysadmin
67 travel
68 trivia
69 web_design
70 words_and_games
71 );
72  
73  
74 11 my %applied;
75  
76 sub apply_keywords {
77 45 ▶ my ( $class, $target ) = @_;
Subroutine apply_keywords: called CC=2 SCAR=6.9
Pod documented
78  
79 45 ▶ return if exists $applied{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $applied{$target} 0 45 45
80 45 $applied{$target} = undef;
81  
82 45 my @attributions;
83 my @topics;
84 45 my @primary_example_queries;
85 45 my @secondary_example_queries;
86 45 my $description;
87 45 my $source;
88 45 my $icon;
89 45 my $category;
90 45 my $name;
91 45 my $icon_url;
92 45 my $code_url;
93 45 my $status;
94 45 my $url_regex = url_match_regex();
95  
96 45 my $stash = Package::Stash->new($target);
97  
98  
99 $stash->add_symbol('&category', sub {
100 11 ▶ croak "Only one category allowed."
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 1 4 7 11
101 unless scalar @_ == 1;
102 7 my $value = shift;
103 croak $value." is not a valid category (Supported: ".join(',',@supported_categories).")"
104 7 ▶ unless grep { $_ eq $value } @supported_categories;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $value;} @supported_categories 0 7 7
105 7 $category = $value;
106
107 45 });
108  
109  
110 $stash->add_symbol('&topics', sub {
111 7 ▶ while (@_) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 7 21
112 14 my $value = shift;
113 croak $value." is not a valid topic (Supported: ".join(',',@supported_topics).")"
114 14 ▶ unless grep { $_ eq $value } @supported_topics;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $value;} @supported_topics 0 14 14
115 14 push @topics, $value;
116 }
117 45 });
118  
119  
120 $stash->add_symbol('&attribution', sub {
121 9 ▶ while (@_) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 9 22
122 13 my $type = shift;
123 13 my $value = shift;
124 croak $type." is not a valid attribution type (Supported: ".join(',',keys %supported_types).")"
125 13 ▶ unless grep { $_ eq $type } keys %supported_types;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $type;} keys %supported_types 0 13 13
126 13 push @attributions, [ $type, $value ];
127 }
128 45 });
129  
130  
131 $stash->add_symbol('&name', sub {
132 7 ▶ croak 'Only one name allowed.'
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 1 0 7 7
133 unless scalar @_ == 1;
134 7 my $value = shift;
135 7 $name = $value;
136 45 });
137  
138  
139 $stash->add_symbol('&source', sub {
140 7 ▶ croak 'Only one source allowed.'
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 1 0 7 7
141 unless scalar @_ == 1;
142 7 my $value = shift;
143 7 $source = $value;
144 45 });
145  
146  
147 $stash->add_symbol('&description', sub {
148 7 ▶ croak 'Only one description allowed.'
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 1 0 7 7
149 unless scalar @_ == 1;
150 7 my $value = shift;
151 7 $description = $value;
152 45 });
153  
154  
155 $stash->add_symbol('&primary_example_queries', sub {
156 7 ▶ while(@_){
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 7 21
157 14 my $query = shift;
158 14 push @primary_example_queries, $query;
159 }
160 45 });
161  
162  
163 $stash->add_symbol('&secondary_example_queries', sub {
164 7 ▶ while(@_){
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 7 21
165 14 my $query = shift;
166 14 push @secondary_example_queries, $query;
167 }
168 45 });
169  
170  
171 $stash->add_symbol('&icon_url', sub {
172 11 ▶ my $value = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
173 11 ▶ croak $value." is not a valid URL."
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$url_regex/g or $value =~ m[^/(i/)?(.+)\.(ico|png|jpe?g)$]gi 4 7 11
Condition66%2/3
$value =~ /$url_regex/g or $value =~ m[^/(i/)?(.+)\.(ico|png|jpe?g)$]gi
l!l&&r!l&&!r
074
MC/DC50%1/2
$value =~ /$url_regex/g or $value =~ m[^/(i/)?(.+)\.(ico|png|jpe?g)$]gi
$value =~ /$url_regex/g $value =~ m[^/(i/)?(.+)\.(ico|png|jpe?g)$]gi
Truth table66%2/3
A or B
A= $value =~ /$url_regex/gB= $value =~ m[^/(i/)?(.+)\.(ico|png|jpe?g)$]gi
ABresult
1X1
011
000
174 unless ($value =~ m/$url_regex/g or $value =~ /^\/(i\/)?(.+)\.(ico|png|jpe?g)$/ig);
175 7 $icon_url = $value;
176 45 });
177  
178  
179 $stash->add_symbol('&code_url', sub {
180 7 ▶ my $value = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
181 7 ▶ croak $value." is not a valid URL."
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$url_regex/g 0 7 7
182 unless $value =~ m/$url_regex/g;
183 7 $code_url = $value;
184 45 });
185  
186  
187 $stash->add_symbol('&status', sub {
188 7 ▶ my $value = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
189 7 ▶ croak $value." is not a valid status."
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^(enabled|disabled)$/gi 0 7 7
190 unless $value =~ m/^(enabled|disabled)$/ig;
191 7 $status = $value;
192 45 });
193  
194  
195  
196 $stash->add_symbol('&get_category', sub {
197 4 ▶ return $category;
Subroutine __ANON__: called CC=1 SCAR=0.0
198 45 });
199  
200  
201 $stash->add_symbol('&get_topics', sub {
202 4 ▶ return \@topics;
Subroutine __ANON__: called CC=1 SCAR=0.0
203 45 });
204  
205  
206 $stash->add_symbol('&get_meta_information', sub {
207 4 ▶ my %meta_information;
Subroutine __ANON__: called CC=1 SCAR=0.0
208
209 4 $meta_information{name} = $name;
210 4 $meta_information{primary_example_queries} = \@primary_example_queries;
211 4 $meta_information{secondary_example_queries} = \@secondary_example_queries;
212 4 $meta_information{icon_url} = $icon_url;
213 4 $meta_information{description} = $description;
214 4 $meta_information{source} = $source;
215 4 $meta_information{code_url} = $code_url;
216 4 $meta_information{status} = $status;
217  
218 4 return \%meta_information;
219 45 });
220  
221  
222 $stash->add_symbol('&get_attributions', sub {
223 5 ▶ my @attribution_links;
Subroutine __ANON__: called CC=3 SCAR=11.0
224 5 for (@attributions) {
225 8 my $type = shift @{$_};
226 8 my $value = shift @{$_};
227 8 ▶ my ( $a, $b ) = ref $value eq 'ARRAY' ? ( $value->[0], $value->[1] ) : ( $value, $value );
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 2 6 8
228 8 my ( $link, $val ) = @{$supported_types{$type}};
229 8 $link =~ s/\Q{{a}}/$a/;
230 8 $link =~ s/\Q{{b}}/$b/;
231 8 $val =~ s/\Q{{a}}/$a/;
232 8 $val =~ s/\Q{{b}}/$b/;
233 8 push @attribution_links, $link, $val;
234 }
235 5 return \@attribution_links;
236 45 });
237  
238 #
239 # apply role
240 #
241  
242 45 Moo::Role->apply_role_to_package($target,'DDG::HasAttribution');
243  
244 }
245  
246 #
247 # Function taken from URL::RegexMatching
248 # - couldn't install due to bad Makefile
249 #
250 sub url_match_regex {
251 return
252 45 ▶ qr{(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))};
Subroutine url_match_regex: called CC=1 SCAR=0.0
Pod undocumented
253 }
254  
255 11 1;
256  
257 __END__
« DDG/Test/Location.pm ↑ Summary DDG/HasShareDir.pm »