From: Matt S Trout Date: Wed, 12 Jan 2011 18:40:29 +0000 (+0000) Subject: get for add page works X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2fc10229895065ec6210a078dba7b1629eb3c653;p=scpubgit%2FCommentry.git get for add page works --- diff --git a/lib/App/Commentry.pm b/lib/App/Commentry.pm index f60074c..b812143 100644 --- a/lib/App/Commentry.pm +++ b/lib/App/Commentry.pm @@ -36,6 +36,14 @@ sub _build_template_dir { $template_dir; } +has 'captcha_handler' => (is => 'lazy'); + +sub _build_captcha_handler { + my ($self) = @_; + require App::Commentry::CaptchaHandler::Fixed; + App::Commentry::CaptchaHandler::Fixed->new; +} + sub dispatch_request { my ($self) = @_; sub (/fragments/*/**) { @@ -62,6 +70,27 @@ sub _fragment_show { } } +sub _fragment_add { + my ($self, $set) = @_; + my $captcha = $self->captcha_handler; + my %params = map +($_ => ''), qw(title body); + sub (POST+%*) { + my ($self, $params) = @_; + @params{qw(title body)} = @{$params}{qw(title body)}; + return unless $captcha->check_response($params); + $set->add($params); + $self->_zoom_response(fragment_add_complete => sub { }); + }, + sub (GET|POST) { + my $captcha_fields = $captcha->challenge_html; + $self->_zoom_response(fragment_add => sub { + $_->select('.captcha-fields')->replace(\$captcha_fields) + ->select('.new-comment-title')->set_attribute(value => $params{title}) + ->select('.new-comment-body')->set_attribute(value => $params{body}); + }); + }, +} + sub _zoom_response { my ($self, $template, $apply) = @_; my $zfh = ($self->{zoom_cache}{$template} ||= do { diff --git a/lib/App/Commentry/CaptchaHandler/Fixed.pm b/lib/App/Commentry/CaptchaHandler/Fixed.pm new file mode 100644 index 0000000..069da4c --- /dev/null +++ b/lib/App/Commentry/CaptchaHandler/Fixed.pm @@ -0,0 +1,31 @@ +package App::Commentry::CaptchaHandler::Fixed; + +use Moo; + +has challenge => (is => 'ro', default => sub { + q{What is mst the chainsaw wielder's weapon of choice?} +}); + +has field_name => (is => 'ro', default => sub { 'captcha_response' }); + +has response_match => (is => 'ro', default => sub { qr/(?i:chainsaw)/ }); + +sub challenge_html { + my ($self) = @_; + + my $field_name = $self->field_name; + + use HTML::Tags; + join '', HTML::Tags::to_html_string( +

, $self->challenge,

, "\n", + , "\n", + ); +} + +sub check_response { + my ($self, $params) = @_; + my $match = $self->response_match; + !!($params->{$self->field_name} =~ /$match/); +} + +1;