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 26 84.6
pod 1 2 50.0
total 152 167 91.0


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 = '1018';
5 11     12   88 use strict;
  11         28  
  11         501  
6 11     11   112 use warnings;
  11         29  
  11         439  
7 11     11   75 use Carp qw( croak );
  11         26  
  11         620  
8 11     11   78 use Package::Stash;
  11         27  
  11         19011  
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 164 my ( $class, $target ) = @_;
78              
79 45 50       210 return if exists $applied{$target};
80 45         132 $applied{$target} = undef;
81              
82 45         583 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         194 my $url_regex = url_match_regex();
95              
96 45         467 my $stash = Package::Stash->new($target);
97              
98              
99             $stash->add_symbol('&category', sub {
100 11 100   11   1733 croak "Only one category allowed."
101             unless scalar @_ == 1;
102 7         21 my $value = shift;
103             croak $value." is not a valid category (Supported: ".join(',',@supported_categories).")"
104 7 50       24 unless grep { $_ eq $value } @supported_categories;
  182         333  
105 7         22 $category = $value;
106            
107 45         675 });
108              
109              
110             $stash->add_symbol('&topics', sub {
111 7     7   44 while (@_) {
112 14         30 my $value = shift;
113             croak $value." is not a valid topic (Supported: ".join(',',@supported_topics).")"
114 14 50       32 unless grep { $_ eq $value } @supported_topics;
  280         474  
115 14         53 push @topics, $value;
116             }
117 45         516 });
118              
119              
120             $stash->add_symbol('&attribution', sub {
121 9     9   4421 while (@_) {
122 13         39 my $type = shift;
123 13         27 my $value = shift;
124             croak $type." is not a valid attribution type (Supported: ".join(',',keys %supported_types).")"
125 13 50       55 unless grep { $_ eq $type } keys %supported_types;
  78         168  
126 13         65 push @attributions, [ $type, $value ];
127             }
128 45         616 });
129              
130              
131             $stash->add_symbol('&name', sub {
132 7 50   7   48 croak 'Only one name allowed.'
133             unless scalar @_ == 1;
134 7         18 my $value = shift;
135 7         19 $name = $value;
136 45         430 });
137              
138              
139             $stash->add_symbol('&source', sub {
140 7 50   7   46 croak 'Only one source allowed.'
141             unless scalar @_ == 1;
142 7         121 my $value = shift;
143 7         26 $source = $value;
144 45         430 });
145              
146              
147             $stash->add_symbol('&description', sub {
148 7 50   7   46 croak 'Only one description allowed.'
149             unless scalar @_ == 1;
150 7         22 my $value = shift;
151 7         18 $description = $value;
152 45         408 });
153              
154              
155             $stash->add_symbol('&primary_example_queries', sub {
156 7     7   40 while(@_){
        5      
        0      
        0      
157 14         26 my $query = shift;
158 14         40 push @primary_example_queries, $query;
159             }
160 45         419 });
161              
162              
163             $stash->add_symbol('&secondary_example_queries', sub {
164 7     7   41 while(@_){
165 14         28 my $query = shift;
166 14         42 push @secondary_example_queries, $query;
167             }
168 45         440 });
169              
170              
171             $stash->add_symbol('&icon_url', sub {
172 11     11   1105 my $value = shift;
173 11 100 66     695 croak $value." is not a valid URL."
174             unless ($value =~ m/$url_regex/g or $value =~ /^\/(i\/)?(.+)\.(ico|png|jpe?g)$/ig);
175 7         33 $icon_url = $value;
176 45         443 });
177              
178              
179             $stash->add_symbol('&code_url', sub {
180 7     7   34 my $value = shift;
181 7 50       165 croak $value." is not a valid URL."
182             unless $value =~ m/$url_regex/g;
183 7         31 $code_url = $value;
184 45         465 });
185              
186              
187             $stash->add_symbol('&status', sub {
188 7     7   31 my $value = shift;
189 7 50       60 croak $value." is not a valid status."
190             unless $value =~ m/^(enabled|disabled)$/ig;
191 7         26 $status = $value;
192 45         380 });
193              
194              
195              
196             $stash->add_symbol('&get_category', sub {
197 4     4   20 return $category;
198 45         385 });
199              
200              
201             $stash->add_symbol('&get_topics', sub {
202 4     4   20 return \@topics;
203 45         395 });
204              
205              
206             $stash->add_symbol('&get_meta_information', sub {
207 4     4   11 my %meta_information;
        0      
        0      
208            
209 4         13 $meta_information{name} = $name;
210 4         12 $meta_information{primary_example_queries} = \@primary_example_queries;
211 4         12 $meta_information{secondary_example_queries} = \@secondary_example_queries;
212 4         11 $meta_information{icon_url} = $icon_url;
213 4         9 $meta_information{description} = $description;
214 4         12 $meta_information{source} = $source;
215 4         10 $meta_information{code_url} = $code_url;
216 4         13 $meta_information{status} = $status;
217              
218 4         51 return \%meta_information;
219 45         479 });
220              
221              
222             $stash->add_symbol('&get_attributions', sub {
223 5     8   9329 my @attribution_links;
224 5         18 for (@attributions) {
225 8         16 my $type = shift @{$_};
  8         26  
226 8         21 my $value = shift @{$_};
  8         20  
227 8 100       45 my ( $a, $b ) = ref $value eq 'ARRAY' ? ( $value->[0], $value->[1] ) : ( $value, $value );
228 8         17 my ( $link, $val ) = @{$supported_types{$type}};
  8         34  
229 8         45 $link =~ s/\Q{{a}}/$a/;
230 8         27 $link =~ s/\Q{{b}}/$b/;
231 8         21 $val =~ s/\Q{{a}}/$a/;
232 8         25 $val =~ s/\Q{{b}}/$b/;
233 8         37 push @attribution_links, $link, $val;
234             }
235 5         37 return \@attribution_links;
236 45         505 });
237              
238             #
239             # apply role
240             #
241              
242 45         270 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     46 0 298 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__