File Coverage

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


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 = '1016';
5 11     12   61 use strict;
  11         149  
  11         252  
6 11     11   37 use warnings;
  11         12  
  11         228  
7 11     11   31 use Carp qw( croak );
  11         10  
  11         337  
8 11     11   35 use Package::Stash;
  11         11  
  11         11823  
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 73 my ( $class, $target ) = @_;
78              
79 45 50       143 return if exists $applied{$target};
80 45         67 $applied{$target} = undef;
81              
82 45         60 my @attributions;
83             my @topics;
84 0         0 my @primary_example_queries;
85 0         0 my @secondary_example_queries;
86 0         0 my $description;
87 0         0 my $source;
88 0         0 my $icon;
89 0         0 my $category;
90 0         0 my $name;
91 0         0 my $icon_url;
92 0         0 my $code_url;
93 0         0 my $status;
94 45         102 my $url_regex = url_match_regex();
95              
96 45         321 my $stash = Package::Stash->new($target);
97              
98              
99             $stash->add_symbol('&category', sub {
100 11 100   11   1143 croak "Only one category allowed."
101             unless scalar @_ == 1;
102 7         12 my $value = shift;
103             croak $value." is not a valid category (Supported: ".join(',',@supported_categories).")"
104 7 50       15 unless grep { $_ eq $value } @supported_categories;
  182         169  
105 7         16 $category = $value;
106            
107 45         480 });
108              
109              
110             $stash->add_symbol('&topics', sub {
111 7     7   27 while (@_) {
112 14         14 my $value = shift;
113             croak $value." is not a valid topic (Supported: ".join(',',@supported_topics).")"
114 14 50       18 unless grep { $_ eq $value } @supported_topics;
  280         237  
115 14         33 push @topics, $value;
116             }
117 45         325 });
118              
119              
120             $stash->add_symbol('&attribution', sub {
121 9     9   2199 while (@_) {
122 13         21 my $type = shift;
123 13         17 my $value = shift;
124             croak $type." is not a valid attribution type (Supported: ".join(',',keys %supported_types).")"
125 13 50       43 unless grep { $_ eq $type } keys %supported_types;
  78         107  
126 13         53 push @attributions, [ $type, $value ];
127             }
128 45         257 });
129              
130              
131             $stash->add_symbol('&name', sub {
132 7 50   7   29 croak 'Only one name allowed.'
        5      
133             unless scalar @_ == 1;
134 7         12 my $value = shift;
135 7         27 $name = $value;
136 45         248 });
137              
138              
139             $stash->add_symbol('&source', sub {
140 7 50   7   33 croak 'Only one source allowed.'
141             unless scalar @_ == 1;
142 7         11 my $value = shift;
143 7         13 $source = $value;
144 45         268 });
145              
146              
147             $stash->add_symbol('&description', sub {
148 7 50   7   25 croak 'Only one description allowed.'
149             unless scalar @_ == 1;
150 7         13 my $value = shift;
151 7         10 $description = $value;
152 45         230 });
153              
154              
155             $stash->add_symbol('&primary_example_queries', sub {
156 7     7   28 while(@_){
157 14         12 my $query = shift;
158 14         50 push @primary_example_queries, $query;
159             }
160 45         251 });
161              
162              
163             $stash->add_symbol('&secondary_example_queries', sub {
164 7     7   28 while(@_){
165 14         18 my $query = shift;
166 14         26 push @secondary_example_queries, $query;
167             }
168 45         272 });
169              
170              
171             $stash->add_symbol('&icon_url', sub {
172 11     11   664 my $value = shift;
173 11 100 66     604 croak $value." is not a valid URL."
174             unless ($value =~ m/$url_regex/g or $value =~ /^\/(i\/)?(.+)\.(ico|png|jpe?g)$/ig);
175 7         20 $icon_url = $value;
176 45         289 });
177              
178              
179             $stash->add_symbol('&code_url', sub {
180 7     7   18 my $value = shift;
181 7 50       55 croak $value." is not a valid URL."
182             unless $value =~ m/$url_regex/g;
183 7         15 $code_url = $value;
184 45         291 });
185              
186              
187             $stash->add_symbol('&status', sub {
188 7     7   18 my $value = shift;
189 7 50       50 croak $value." is not a valid status."
190             unless $value =~ m/^(enabled|disabled)$/ig;
191 7         15 $status = $value;
192 45         229 });
193              
194              
195              
196             $stash->add_symbol('&get_category', sub {
197 4     4   15 return $category;
198 45         241 });
199              
200              
201             $stash->add_symbol('&get_topics', sub {
202 4     4   17 return \@topics;
203 45         249 });
204              
205              
206             $stash->add_symbol('&get_meta_information', sub {
207 4     4   7 my %meta_information;
208            
209 4         11 $meta_information{name} = $name;
210 4         10 $meta_information{primary_example_queries} = \@primary_example_queries;
211 4         9 $meta_information{secondary_example_queries} = \@secondary_example_queries;
212 4         8 $meta_information{icon_url} = $icon_url;
213 4         10 $meta_information{description} = $description;
214 4         8 $meta_information{source} = $source;
215 4         9 $meta_information{code_url} = $code_url;
216 4         11 $meta_information{status} = $status;
217              
218 4         37 return \%meta_information;
219 45         427 });
220              
221              
222             $stash->add_symbol('&get_attributions', sub {
223 5     5   7232 my @attribution_links;
224 5         16 for (@attributions) {
225 8         9 my $type = shift @{$_};
  8         17  
226 8         12 my $value = shift @{$_};
  8         14  
227 8 100       33 my ( $a, $b ) = ref $value eq 'ARRAY' ? ( $value->[0], $value->[1] ) : ( $value, $value );
228 8         12 my ( $link, $val ) = @{$supported_types{$type}};
  8         22  
229 8         30 $link =~ s/\Q{{a}}/$a/;
230 8         13 $link =~ s/\Q{{b}}/$b/;
231 8         13 $val =~ s/\Q{{a}}/$a/;
232 8         17 $val =~ s/\Q{{b}}/$b/;
233 8         20 push @attribution_links, $link, $val;
234             }
235 5         29 return \@attribution_links;
236 45         296 });
237              
238             #
239             # apply role
240             #
241              
242 45         168 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 186 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__