-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.pl
executable file
·91 lines (83 loc) · 2.54 KB
/
app.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::JSON qw(decode_json encode_json);
use Modern::Perl '2013';
use Try::Tiny;
use Data::Dumper;
use WebHooks::Schema;
my $config = plugin 'JSONConfig';
helper dbh => sub {
my $schema = WebHooks::Schema->connect( $config->{'pg_dsn'},
$config->{'pg_user'}, $config->{'pg_pass'}, );
return $schema;
};
helper find_or_new => sub {
my $self = shift;
my $subscriber = shift;
my $dbh = $self->dbh();
my $result;
try {
$result = $dbh->txn_do(
sub {
my $rs = $dbh->resultset( 'Wufoo' )
->find_or_new( { %$subscriber, } );
unless ( $rs->in_storage ) {
$rs->insert;
}
}
);
}
catch {
$self->app->log->debug( $_ );
};
return $result;
};
helper parse_webhooks_data => sub {
my $self = shift;
my $post = shift;
my $field = decode_json( $post->{'FieldStructure'} );
my $form = decode_json( $post->{'FormStructure'} );
my $data = {
entry_id => $form->{'Hash'} . '-' . $post->{'EntryId'},
date_created => $post->{'DateCreated'},
form_url => $form->{'Url'},
form_data => encode_json( $post ),
ip_address => $post->{'IP'},
form_name => $form->{'Name'},
};
for my $value ( @{ $field->{'Fields'} } ) {
if ( $value->{'Type'} eq 'email' ) {
$data->{'email'} = $post->{ $value->{'ID'} };
}
elsif ($value->{'Title'} eq 'Stay informed'
&& $value->{'Type'} eq 'radio' )
{
$data->{'subscription'} = $post->{ $value->{'ID'} };
}
}
return $data;
};
helper authorized => sub {
my $self = shift;
my $hook = shift;
my $handshake = 'none';
$handshake = $hook->{'HandshakeKey'};
my $auth;
$auth = $handshake eq $config->{'wufoo_handshake'} ? 'authorized' : '';
$self->app->log->debug(
'Unauthorzied access attempt: ' . Dumper( $hook ) )
if !$auth;
return $auth;
};
any '/wufoo' => sub {
my $self = shift;
my $hook = $self->req->params->to_hash;
return $self->render( { data => '', status => '401' } )
unless $self->authorized( $hook );
my $subscriber = $self->parse_webhooks_data( $hook );
my $result = $self->find_or_new( $subscriber );
$self->app->log->debug( Dumper( $subscriber ) );
$self->respond_to( any => { 'data' => '', status => 200 }, );
};
app->secrets([$config->{'app_secret'}]);
app->start;