File Coverage

blib/lib/Dancer/Plugin/Captcha/AreYouAHuman.pm
Criterion Covered Total %
statement 9 25 36.0
branch 0 14 0.0
condition n/a
subroutine 3 9 33.3
pod n/a
total 12 48 25.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Dancer::Plugin::Captcha::AreYouAHuman - Easily integrate AreYouAHuman captcha into your Dancer applications
4              
5             =head1 SYNOPSIS
6              
7             use Dancer::Plugin::Captcha::AreYouAHuman "is_a_human";
8              
9             # in config.yml
10             plugins:
11             Captcha::AreYouAHuman:
12             publisher_key: BAADBEEFBAADBEEF
13             scoring_key: BEEFBEEFBEEFBEEF
14              
15             # before template render
16             human_checker true;
17             template "file.tt";
18              
19             # In your template (TT2)
20             [% are_you_a_human %]
21              
22             # In your validation code....
23             if ( is_a_human ) {
24             print "You're a human!\n";
25             }
26             else {
27             print "Not a human\n":
28             }
29              
30             =head1 METHODS
31              
32             =head2 human_checker
33              
34             set ture when you need it.
35              
36             but by default is false
37              
38             Enable the directive "are_you_a_human" in the template
39              
40             e.g.: human_checker true;
41              
42             =head2 is_a_human
43              
44             check the inputs are coming from a human visitor or not.
45              
46             return value is 1 or 0 or undef
47              
48             =head2 [% are_you_a_human %]
49              
50             template directive to print out the validating html
51              
52             =head2 TODO
53            
54             Add a real test suite.
55              
56             =head1 BUGS
57              
58             Please report any bugs or feature requests to C, or through
59             the web interface at L. I will be notified, and then you'll
60             automatically be notified of progress on your bug as I make changes.
61              
62             =head1 SUPPORT
63              
64             You can find documentation for this module with the perldoc command.
65              
66             perldoc Dancer::Plugin::Captcha::AreYouAHuman
67              
68             You can also look for information at:
69              
70             =over 4
71              
72             =item * RT: CPAN's request tracker
73              
74             L
75              
76             =item * AnnoCPAN: Annotated CPAN documentation
77              
78             L
79              
80             =item * CPAN Ratings
81              
82             L
83              
84             =item * Search CPAN
85              
86             L
87              
88             =item * GIT Respority
89              
90             L
91              
92             =back
93              
94             =head1 SEE ALSO
95            
96             =over 4
97            
98             =item *
99            
100             L
101            
102             =item *
103            
104             L
105            
106             =item *
107            
108             L
109            
110             =back
111              
112             =head1 AUTHOR
113              
114             Michael Vu, C<< >>
115              
116             =head1 ACKNOWLEDGEMENTS
117              
118             This program is free software; you can redistribute it and/or modify it
119             under the same terms as Perl itself.
120              
121             =cut
122              
123             package Dancer::Plugin::Captcha::AreYouAHuman;
124             {
125             $Dancer::Plugin::Captcha::AreYouAHuman::VERSION = '1';
126             }
127 1     1   1901 use Dancer ":syntax";
  1         658972  
  1         5  
128 1     1   1138 use Dancer::Plugin;
  1         1289  
  1         68  
129 1     1   2058 use Captcha::AreYouAHuman;
  1         38499  
  1         496  
130              
131             hook before_template_render => sub {
132             if ( !human_checker() ) { return }
133             my $stash = shift;
134             my $ayah = _ayah();
135             my $html = $ayah->getPublisherHTML;
136             my $code = _conversion_code();
137             $stash->{ayah} = $ayah;
138             $stash->{ayah_publisher_html} = $html;
139             $stash->{ayah_conversion_code} = $code;
140             $stash->{are_you_a_human} = $html . $code;
141             };
142              
143             register human_checker => sub {
144 0     0     my $set = shift;
145 0 0         if ( defined $set ) {
146 0           var use_ayah_form => $set;
147             }
148 0           return var "use_ayah_form";
149             };
150              
151             register is_a_human => sub {
152 0 0   0     my $session_secret = _session_secret()
153             or return;
154 0 0         my $client_ip = request->env->{REMOTE_ADDR}
155             or return;
156 0           return _ayah()->scoreResult(
157             session_secret => $session_secret,
158             client_ip => $client_ip,
159             );
160             };
161              
162             sub _conversion_code {
163 0 0   0     my $session_secret = _session_secret()
164             or return q{};
165 0           return _ayah()->recordConversion( session_secret => $session_secret );
166             };
167              
168             sub _session_secret {
169 0     0     return param "session_secret";
170             }
171              
172             sub _ayah {
173 0 0   0     var("ayah") or _build_ayah();
174             }
175              
176             sub _build_ayah {
177 0     0     my $setting = plugin_setting;
178 0 0         my $publisher_key = $setting->{publisher_key}
179             or die "Missing publisher_key";
180 0 0         my $scoring_key = $setting->{scoring_key}
181             or die "Missing scoring_key";
182 0           my $captcha = Captcha::AreYouAHuman->new(
183             publisher_key => $publisher_key,
184             scoring_key => $scoring_key,
185             );
186 0           return var ayah => $captcha;
187             }
188              
189             register_plugin;
190