Skip to content
This repository
Browse code

Combined DDG::Meta::Information with new Categories and Topics Metada…

…ta to make DDG::Meta::Information - added several passing and failing tests
  • Loading branch information...
commit 1b7fe94b91367f6821ee3fcac35550f6267c43c1 1 parent be29208
Zaahir Moolla authored July 21, 2012
10  lib/DDG/Meta.pm
@@ -10,8 +10,8 @@ use DDG::Meta::ZeroClickInfo;
10 10
 use DDG::Meta::ZeroClickInfoSpice;
11 11
 use DDG::Meta::ShareDir;
12 12
 use DDG::Meta::Block;
13  
-use	DDG::Meta::Attribution;
14  
-use	DDG::Meta::Helper;
  13
+use DDG::Meta::Information;
  14
+use DDG::Meta::Helper;
15 15
 
16 16
 use MooX ();
17 17
 
@@ -65,7 +65,7 @@ sub apply_goodie_keywords {
65 65
 	DDG::Meta::ZeroClickInfo->apply_keywords($target);
66 66
 	DDG::Meta::ShareDir->apply_keywords($target);
67 67
 	DDG::Meta::Block->apply_keywords($target);
68  
-	DDG::Meta::Attribution->apply_keywords($target);
  68
+	DDG::Meta::Information->apply_keywords($target);
69 69
 	DDG::Meta::Helper->apply_keywords($target);
70 70
 	DDG::Meta::RequestHandler->apply_keywords($target,sub {
71 71
 		shift->zci_new(
@@ -94,11 +94,11 @@ sub apply_spice_keywords {
94 94
 	DDG::Meta::ZeroClickInfoSpice->apply_keywords($target);
95 95
 	DDG::Meta::ShareDir->apply_keywords($target);
96 96
 	DDG::Meta::Block->apply_keywords($target);
97  
-	DDG::Meta::Attribution->apply_keywords($target);
  97
+	DDG::Meta::Information->apply_keywords($target);
98 98
 	DDG::Meta::Helper->apply_keywords($target);
99 99
 	DDG::Meta::RequestHandler->apply_keywords($target,sub {
100 100
 		shift->spice_new(@_);
101 101
 	},'DDG::IsSpice');
102 102
 }
103 103
 
104  
-1;
  104
+1;
67  lib/DDG/Meta/Attribution.pm
... ...
@@ -1,67 +0,0 @@
1  
-package DDG::Meta::Attribution;
2  
-
3  
-use strict;
4  
-use warnings;
5  
-use Carp qw( croak );
6  
-use Package::Stash;
7  
-
8  
-require Moo::Role;
9  
-
10  
-my %supported_types = (
11  
-	email => [ 'mailto:{{a}}', '{{b}}' ],
12  
-	twitter => [ 'https://twitter.com/{{a}}', '@{{b}}' ],
13  
-	web => [ '{{a}}', '{{b}}' ],
14  
-	github => [ 'https://github.com/{{a}}', '{{b}}' ],
15  
-	facebook => [ 'https://facebook.com/{{a}}', '{{b}}' ],
16  
-	cpan => [ 'https://metacpan.org/author/{{a}}', '{{a}}' ],
17  
-);
18  
-
19  
-my %applied;
20  
-
21  
-sub apply_keywords {
22  
-	my ( $class, $target ) = @_;
23  
-
24  
-	return if exists $applied{$target};
25  
-	$applied{$target} = undef;
26  
-
27  
-	my @attributions;
28  
-
29  
-	my $stash = Package::Stash->new($target);
30  
-
31  
-	$stash->add_symbol('&get_attributions', sub {
32  
-		my @attribution_links;
33  
-		for (@attributions) {
34  
-			my $type = shift @{$_};
35  
-			my $value = shift @{$_};
36  
-			my ( $a, $b ) = ref $value eq 'ARRAY' ? ( $value->[0], $value->[1] ) : ( $value, $value );
37  
-			my ( $link, $val ) = @{$supported_types{$type}};
38  
-			$link =~ s/{{a}}/$a/;
39  
-			$link =~ s/{{b}}/$b/;
40  
-			$val =~ s/{{a}}/$a/;
41  
-			$val =~ s/{{b}}/$b/;
42  
-			push @attribution_links, $link, $val;
43  
-		}
44  
-		return \@attribution_links;
45  
-	});
46  
-	$stash->add_symbol('&get_attributions_html', sub {
47  
-		# TODO
48  
-	});
49  
-	$stash->add_symbol('&attribution', sub {
50  
-		while (@_) {
51  
-			my $type = shift;
52  
-			my $value = shift;
53  
-			croak $type." is not a valid attribution type (Supported: ".join(',',keys %supported_types).")"
54  
-				unless grep { $_ eq $type } keys %supported_types;
55  
-			push @attributions, [ $type, $value ];
56  
-		}
57  
-	});
58  
-
59  
-	#
60  
-	# apply role
61  
-	#
62  
-
63  
-	Moo::Role->apply_role_to_package($target,'DDG::HasAttribution');
64  
-
65  
-}
66  
-
67  
-1;
281  lib/DDG/Meta/Information.pm
... ...
@@ -0,0 +1,281 @@
  1
+package DDG::Meta::Information;
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use Carp qw( croak );
  6
+use Package::Stash;
  7
+
  8
+require Moo::Role;
  9
+
  10
+my %supported_types = (
  11
+	email => [ 'mailto:{{a}}', '{{b}}' ],
  12
+	twitter => [ 'https://twitter.com/{{a}}', '@{{b}}' ],
  13
+	web => [ '{{a}}', '{{b}}' ],
  14
+	github => [ 'https://github.com/{{a}}', '{{b}}' ],
  15
+	facebook => [ 'https://facebook.com/{{a}}', '{{b}}' ],
  16
+	cpan => [ 'https://metacpan.org/author/{{a}}', '{{a}}' ],
  17
+);
  18
+
  19
+my @supported_categories = qw(
  20
+	bang
  21
+	calculations
  22
+	cheat_sheets
  23
+	computing_info
  24
+	computing_tools
  25
+	conversions
  26
+	dates
  27
+	entertainment
  28
+	facts
  29
+	finance
  30
+	food
  31
+	formulas
  32
+	forums
  33
+	geography
  34
+	ids
  35
+	language
  36
+	location_aware
  37
+	physical_properties
  38
+	programming
  39
+	q/a
  40
+	random
  41
+	reference
  42
+	software
  43
+	special_queries
  44
+	time_sensitive
  45
+	transformations
  46
+	wikia
  47
+	sources
  48
+);
  49
+
  50
+my @supported_topics = qw(
  51
+	everyday
  52
+	business
  53
+	cryptography
  54
+	entertainment
  55
+	food
  56
+	gaming
  57
+	geography
  58
+	fun
  59
+	linguistics
  60
+	math 
  61
+	music
  62
+	programming
  63
+	random
  64
+	science
  65
+	social
  66
+	sysadmin
  67
+	travel
  68
+	trivia
  69
+	web design
  70
+);
  71
+
  72
+=head1 DESCRIPTION
  73
+
  74
+TODO
  75
+
  76
+=method apply_keywords
  77
+
  78
+Uses a given classname to install the described keywords.
  79
+
  80
+=cut
  81
+
  82
+my %applied;
  83
+
  84
+sub apply_keywords {
  85
+	my ( $class, $target ) = @_;
  86
+
  87
+	return if exists $applied{$target};
  88
+	$applied{$target} = undef;
  89
+
  90
+	my @attributions;
  91
+	my @topics;
  92
+	my @example_query;
  93
+	my $icon;
  94
+	my $category;
  95
+	my $name;
  96
+	my $icon_url;
  97
+	my $code_url;
  98
+	my $url_regex = url_match_regex();
  99
+
  100
+	my $stash = Package::Stash->new($target);
  101
+
  102
+=keyword category
  103
+
  104
+This function sets the category for the plugin. Plugins are only allowed one category.
  105
+
  106
+=cut
  107
+
  108
+	$stash->add_symbol('&category', sub {
  109
+		croak "Only one category allowed."
  110
+			unless scalar @_ == 1;
  111
+		my $value = shift;
  112
+		croak $value." is not a valid category (Supported: ".join(',',@supported_categories).")"
  113
+			unless grep { $_ eq $value } @supported_categories;
  114
+		$category = $value;
  115
+		
  116
+	});
  117
+
  118
+=keyword topics
  119
+
  120
+This function sets the topics for the plugin. Plugins are allowed multiple topics.
  121
+
  122
+=cut
  123
+
  124
+	$stash->add_symbol('&topics', sub {
  125
+		while (@_) {
  126
+			my $value = shift;
  127
+			croak $value." is not a valid topic (Supported: ".join(',',@supported_topics).")"
  128
+				unless grep { $_ eq $value } @supported_topics;
  129
+			push @topics, $value;
  130
+		}
  131
+	});
  132
+
  133
+=keyword attribution
  134
+
  135
+This function sets the attribution information for the plugin. The allowed operators are:
  136
+	email, twitter, web, github, facebook and cpan.
  137
+The allowed formats for each are listed in the %support_types hash above.
  138
+
  139
+=cut
  140
+
  141
+	$stash->add_symbol('&attribution', sub {
  142
+		while (@_) {
  143
+			my $type = shift;
  144
+			my $value = shift;
  145
+			croak $type." is not a valid attribution type (Supported: ".join(',',keys %supported_types).")"
  146
+				unless grep { $_ eq $type } keys %supported_types;
  147
+			push @attributions, [ $type, $value ];
  148
+		}
  149
+	});
  150
+
  151
+=keyword name
  152
+
  153
+This function sets the name for the plugin.
  154
+
  155
+=cut
  156
+
  157
+	$stash->add_symbol('&name', sub {
  158
+		croak 'Only one name allowed.'
  159
+			unless scalar @_ == 1;
  160
+		my $value = shift;
  161
+		$name = $value;
  162
+	});
  163
+
  164
+=keyword example_query
  165
+
  166
+This function sets an array of example queries for the plugin. 
  167
+This is used to show users example queries for the plugin.
  168
+
  169
+=cut
  170
+
  171
+	$stash->add_symbol('&example_query', sub {
  172
+		while(@_){
  173
+			my $query = shift;
  174
+			push @example_query, $query;
  175
+		}
  176
+	});
  177
+
  178
+=keyword icon_urk
  179
+
  180
+This function sets the url used to fetch the icon for the plugin.
  181
+
  182
+=cut
  183
+
  184
+	$stash->add_symbol('&icon_url', sub {
  185
+		my $value = shift;
  186
+		croak $value." is not a valid URL."
  187
+			unless $value =~ m/$url_regex/g;
  188
+		$icon_url = $value;
  189
+	});
  190
+
  191
+=keyword code_urk
  192
+
  193
+This function sets the url which links the plugin's code on github.
  194
+
  195
+=cut
  196
+
  197
+	$stash->add_symbol('&code_url', sub {
  198
+		my $value = shift;
  199
+		croak $value." is not a valid URL."
  200
+			unless $value =~ m/$url_regex/g;
  201
+		$code_url = $value;
  202
+	});
  203
+
  204
+=keyword get_category
  205
+
  206
+This function returns the plugin's category
  207
+
  208
+=cut
  209
+
  210
+	$stash->add_symbol('&get_category', sub {
  211
+		return $category;
  212
+	});
  213
+
  214
+=keyword get_topics
  215
+
  216
+This function returns the plugin's topics in an array
  217
+
  218
+=cut
  219
+
  220
+	$stash->add_symbol('&get_topics', sub {
  221
+		return \@topics;
  222
+	});
  223
+
  224
+=keyword get_meta_information
  225
+
  226
+This function returns the plugin's meta information in a hash
  227
+
  228
+=cut
  229
+
  230
+	$stash->add_symbol('&get_meta_information', sub {
  231
+		my %meta_information;
  232
+		
  233
+		$meta_information{name} = $name;
  234
+		$meta_information{example_query} = \@example_query;
  235
+		$meta_information{icon_url} = $icon_url;
  236
+		$meta_information{code_url} = $code_url;
  237
+
  238
+		return \%meta_information;
  239
+	});
  240
+
  241
+=keyword get_attributions
  242
+
  243
+This function returns the plugin's attribution information in a hash
  244
+
  245
+=cut
  246
+
  247
+	$stash->add_symbol('&get_attributions', sub {
  248
+		my @attribution_links;
  249
+		for (@attributions) {
  250
+			my $type = shift @{$_};
  251
+			my $value = shift @{$_};
  252
+			my ( $a, $b ) = ref $value eq 'ARRAY' ? ( $value->[0], $value->[1] ) : ( $value, $value );
  253
+			my ( $link, $val ) = @{$supported_types{$type}};
  254
+			$link =~ s/{{a}}/$a/;
  255
+			$link =~ s/{{b}}/$b/;
  256
+			$val =~ s/{{a}}/$a/;
  257
+			$val =~ s/{{b}}/$b/;
  258
+			push @attribution_links, $link, $val;
  259
+		}
  260
+		return \@attribution_links;
  261
+	});
  262
+
  263
+	#
  264
+	# apply role
  265
+	#
  266
+
  267
+	Moo::Role->apply_role_to_package($target,'DDG::HasAttribution');
  268
+
  269
+}
  270
+
  271
+
  272
+#
  273
+# Function taken from URL::RegexMatching 
  274
+# - couldn't install due to bad Makefile
  275
+#
  276
+sub url_match_regex {
  277
+    return
  278
+      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`!()\[\]{};:'".,<>?«»“”‘’]))};
  279
+}
  280
+
  281
+1;
25  t/10-meta.t
@@ -30,4 +30,29 @@ is_deeply(DDGTest::Goodie::MetaOnly->get_attributions,[
30 30
 	'https://twitter.com/someone', '@someone',
31 31
 ],'Checking resulting get_attributions of DDGTest::Goodie::MetaOnly');
32 32
 
  33
+is_deeply(DDGTest::Goodie::MetaOnly->get_category,
  34
+	'software',
  35
+'Checking resulting get_category of DDGTest::Goodie::MetaOnly');
  36
+
  37
+is_deeply(DDGTest::Goodie::MetaOnly->get_meta_information,{
  38
+	name => 'myGoodie',
  39
+	example_query => ['trigger for myGoodie', 'another trigger for myGoodie'],
  40
+	icon_url => 'http://mysite.com/images/icon',
  41
+	code_url => 'http://github.com/myGoodie',
  42
+},'Checking resulting get_meta_information of DDGTest::Goodie::MetaOnly');
  43
+
  44
+is_deeply(DDGTest::Goodie::MetaOnly->get_topics,[
  45
+	'programming', 'fun',
  46
+],'Checking resulting get_topics of DDGTest::Goodie::MetaOnly');
  47
+
  48
+eval q{
  49
+	use DDGTest::Goodie::WrongMetaOnlyTwoCategories;
  50
+};
  51
+like($@, qr/Only one category allowed/, 'Checking DDGTest::Goodie::WrongMetaOnlyTwoCategories for crashing proper');
  52
+
  53
+eval q{
  54
+	use DDGTest::Goodie::WrongMetaOnlyBadURL;
  55
+};
  56
+like($@, qr/BROKEN is not a valid URL/, 'Checking DDGTest::Goodie::WrongMetaOnlyBadURL for crashing proper');
  57
+
33 58
 done_testing;
5  t/40-goodie.t
@@ -19,11 +19,6 @@ is_deeply(DDGTest::Goodie::Words->get_triggers,{
19 19
 	start => [ "bar", "baz", "buu", "abar", "abaz" ],
20 20
 },'Checking resulting get_triggers of DDGTest::Goodie::Words');
21 21
 
22  
-is_deeply(DDGTest::Goodie::Words->get_attributions,[
23  
-	'mailto:god@universe.org', 'god@universe.org',
24  
-	'https://github.com/github', 'github',
25  
-],'Checking resulting get_attributions of DDGTest::Goodie::Words');
26  
-
27 22
 my $re = DDGTest::Goodie::Regexp->new( block => undef );
28 23
 
29 24
 isa_ok($re,'DDGTest::Goodie::Regexp');
12  t/lib/DDGTest/Goodie/MetaOnly.pm
@@ -4,4 +4,16 @@ use DDG::Goodie;
4 4
 
5 5
 attribution twitter => 'someone';
6 6
 
  7
+category 'software';
  8
+
  9
+topics "programming", "fun";
  10
+
  11
+name "myGoodie";
  12
+
  13
+example_query "trigger for myGoodie", "another trigger for myGoodie";
  14
+
  15
+icon_url "http://mysite.com/images/icon";
  16
+
  17
+code_url "http://github.com/myGoodie";
  18
+
7 19
 1;
7  t/lib/DDGTest/Goodie/WrongMetaOnlyBadURL.pm
... ...
@@ -0,0 +1,7 @@
  1
+package DDGTest::Goodie::WrongMetaOnlyBadURL;
  2
+
  3
+use DDG::Goodie;
  4
+
  5
+icon_url 'BROKEN';
  6
+
  7
+1;
7  t/lib/DDGTest/Goodie/WrongMetaOnlyTwoCategories.pm
... ...
@@ -0,0 +1,7 @@
  1
+package DDGTest::Goodie::WrongMetaOnlyTwoCategories;
  2
+
  3
+use DDG::Goodie;
  4
+
  5
+category 'random', 'food';
  6
+
  7
+1;

0 notes on commit 1b7fe94

Please sign in to comment.
Something went wrong with that request. Please try again.