4 SGN::Controller::AJAX::Contact - a REST controller class to provide the
5 functions for posting the contact form as an issue on github
9 When the contact form is submitted it is posted as an issue to github
13 Nicolas Morales <nm529@cornell.edu>
17 package SGN
::Controller
::AJAX
::Contact
;
24 BEGIN { extends
'Catalyst::Controller::REST' }
27 default => 'application/json',
29 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
32 sub submit_contact_form
: Path
('/ajax/contact/submit') : ActionClass
('REST') { }
34 sub submit_contact_form_POST
: Args
(0) {
37 my $title = $c->req->param('title');
38 my $body = $c->req->param('body');
39 my $security_answer = $c->req->param('security_answer');
40 my $security_attempt = $c->req->param('security_attempt');
42 if (!$security_attempt || !$security_answer || $security_answer ne $security_attempt){
43 $c->stash->{rest
} = {error
=> "You must be a clever bot"};
47 my $github_access_token = $c->config->{github_access_token
};
48 my $website_name = $c->config->{project_name
};
49 my $ua = LWP
::UserAgent
->new;
51 my $server_endpoint = "https://api.github.com/repos/solgenomics/contactform/issues?access_token=$github_access_token";
52 my $req = HTTP
::Request
->new(POST
=> $server_endpoint);
53 $req->header('content-type' => 'application/json');
55 my $post_data = { "title"=>$title, "body"=> $body, "labels"=>[$website_name] };
56 $req->content( encode_json
$post_data);
58 my $resp = $ua->request($req);
59 if ($resp->is_success) {
60 my $message = $resp->decoded_content;
61 my $message_hash = decode_json
$message;
62 #print STDERR Dumper $message_hash;
63 if ($message_hash->{id
}){
64 $c->stash->{rest
} = {success
=> 1};
66 $c->stash->{rest
} = {error
=> 'The message was not posted to github correctly. Please try again.'};
69 $c->stash->{rest
} = {error
=> "There was an error submitting the message. Please try again."};