File Coverage

blib/lib/DDG/Meta/Information.pm
Criterion Covered Total %
statement 112 112 100.0
branch 15 24 62.5
condition 2 3 66.6
subroutine 22 22 100.0
pod 1 2 50.0
total 152 163 93.2


line stmt bran cond sub pod time code
1             package DDG::Meta::Information;
2             our $AUTHORITY = 'cpan:DDG';
3             # ABSTRACT: DDG plugin meta information storage
4             $DDG::Meta::Information::VERSION = '1017';
5 11     11   67 use strict;
  11         85  
  11         272  
6 11     11   50 use warnings;
  11         21  
  11         253  
7 11     11   46 use Carp qw( croak );
  11         21  
  11         358  
8 11     11   50 use Package::Stash;
  11         30  
  11         12275  
9              
10             require Moo::Role;
11              
12             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             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             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             my %applied;
75              
76             sub apply_keywords {
77 45     45 1 135 my ( $class, $target ) = @_;
78              
79 45 50       163 return if exists $applied{$target};
80 45         98 $applied{$target} = undef;
81              
82 45         494 my @attributions;
83             my @topics;
84 45         0 my @primary_example_queries;
85 45         0 my @secondary_example_queries;
86 45         0 my $description;
87 45         0 my $source;
88 45         0 my $icon;
89 45         0 my $category;
90 45         0 my $name;
91 45         0 my $icon_url;
92 45         0 my $code_url;
93 45         0 my $status;
94 45         137 my $url_regex = url_match_regex();
95              
96 45         354 my $stash = Package::Stash->new($target);
97              
98              
99             $stash->add_symbol('&category', sub {
100 11 100   11   1477 croak "Only one category allowed."
101             unless scalar @_ == 1;
102 7         20 my $value = shift;
103             croak $value." is not a valid category (Supported: ".join(',',@supported_categories).")"
104 7 50       19 unless grep { $_ eq $value } @supported_categories;
  182         297  
105 7         22 $category = $value;
106            
107 45         605 });
108              
109              
110             $stash->add_symbol('&topics', sub {
111 7     7   37 while (@_) {
112 14         31 my $value = shift;
113             croak $value." is not a valid topic (Supported: ".join(',',@supported_topics).")"
114 14 50       26 unless grep { $_ eq $value } @supported_topics;
  280         458  
115 14         44 push @topics, $value;
116             }
117 45         365 });
118              
119              
120             $stash->add_symbol('&attribution', sub {
121 9     9   3179 while (@_) {
122 13         34 my $type = shift;
123 13         29 my $value = shift;
124             croak $type." is not a valid attribution type (Supported: ".join(',',keys %supported_types).")"
125 13 50       52 unless grep { $_ eq $type } keys %supported_types;
  78         177  
126 13         68 push @attributions, [ $type, $value ];
127             }
128 45         333 });
129              
130              
131             $stash->add_symbol('&name', sub {
132 7 50   7   40 croak 'Only one name allowed.'
        1      
133             unless scalar @_ == 1;
134 7         16 my $value = shift;
135 7         18 $name = $value;
136 45         303 });
137              
138              
139             $stash->add_symbol('&source', sub {
140 7 50   7   43 croak 'Only one source allowed.'
141             unless scalar @_ == 1;
142 7         16 my $value = shift;
143 7         18 $source = $value;
144 45         304 });
145              
146              
147             $stash->add_symbol('&description', sub {
148 7 50   7   38 croak 'Only one description allowed.'
149             unless scalar @_ == 1;
150 7         15 my $value = shift;
151 7         16 $description = $value;
152 45         292 });
153              
154              
155             $stash->add_symbol('&primary_example_queries', sub {
156 7     7   43 while(@_){
157 14         24 my $query = shift;
158 14         38 push @primary_example_queries, $query;
159             }
160 45         331 });
161              
162              
163             $stash->add_symbol('&secondary_example_queries', sub {
164 7     7   39 while(@_){
165 14         25 my $query = shift;
166 14         38 push @secondary_example_queries, $query;
167             }
168 45         335 });
169              
170              
171             $stash->add_symbol('&icon_url', sub {
172 11     11   1094 my $value = shift;
173 11 100 66     651 croak $value." is not a valid URL."
174             unless ($value =~ m/$url_regex/g or $value =~ /^\/(i\/)?(.+)\.(ico|png|jpe?g)$/ig);
175 7         27 $icon_url = $value;
176 45         324 });
177              
178              
179             $stash->add_symbol('&code_url', sub {
180 7     7   28 my $value = shift;
181 7 50       76 croak $value." is not a valid URL."
182             unless $value =~ m/$url_regex/g;
183 7         30 $code_url = $value;
184 45         352 });
185              
186              
187             $stash->add_symbol('&status', sub {
188 7     7   26 my $value = shift;
189 7 50       45 croak $value." is not a valid status."
190             unless $value =~ m/^(enabled|disabled)$/ig;
191 7         19 $status = $value;
192 45         299 });
193              
194              
195              
196             $stash->add_symbol('&get_category', sub {
197 4     4   21 return $category;
198 45         317 });
199              
200              
201             $stash->add_symbol('&get_topics', sub {
202 4     4   21 return \@topics;
203 45         311 });
204              
205              
206             $stash->add_symbol('&get_meta_information', sub {
207 4     4   13 my %meta_information;
208            
209 4         13 $meta_information{name} = $name;
210 4         13 $meta_information{primary_example_queries} = \@primary_example_queries;
211 4         13 $meta_information{secondary_example_queries} = \@secondary_example_queries;
212 4         14 $meta_information{icon_url} = $icon_url;
213 4         12 $meta_information{description} = $description;
214 4         11 $meta_information{source} = $source;
215 4         12 $meta_information{code_url} = $code_url;
216 4         14 $meta_information{status} = $status;
217              
218 4         42 return \%meta_information;
219 45         367 });
220              
221              
222             $stash->add_symbol('&get_attributions', sub {
223 5     5   9281 my @attribution_links;
224 5         18 for (@attributions) {
225 8         14 my $type = shift @{$_};
  8         23  
226 8         14 my $value = shift @{$_};
  8         20  
227 8 100       38 my ( $a, $b ) = ref $value eq 'ARRAY' ? ( $value->[0], $value->[1] ) : ( $value, $value );
228 8         18 my ( $link, $val ) = @{$supported_types{$type}};
  8         29  
229 8         37 $link =~ s/\Q{{a}}/$a/;
230 8         21 $link =~ s/\Q{{b}}/$b/;
231 8         18 $val =~ s/\Q{{a}}/$a/;
232 8         23 $val =~ s/\Q{{b}}/$b/;
233 8         27 push @attribution_links, $link, $val;
234             }
235 5         36 return \@attribution_links;
236 45         338 });
237              
238             #
239             # apply role
240             #
241              
242 45         212 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     45 0 227 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`!()\[\]{};:'".,<>?«»“”‘’]))};
253             }
254              
255             1;
256              
257             __END__