File Coverage

blib/lib/App/Beeminder/Hook.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package App::Beeminder::Hook;
2 2     2   22037 use Dancer ':syntax';
  2         608840  
  2         11  
3 2     2   2980 use JSON::Any;
  2         40647  
  2         14  
4 2     2   21106 use autodie;
  2         42206  
  2         14  
5 2     2   13384 use Data::Dumper;
  2         6  
  2         1016  
6              
7             # ABSTRACT: Integrate Github and Beeminder
8              
9             our $VERSION = '0.1';
10              
11             # inspired by https://github.com/solgenomics/sgn-devtools/blob/master/git/github_post_receive
12              
13             # This will eventually use WWW::Beeminder when that is ready, but for now, curl saves the day
14             any '/hook' => sub {
15             my $p = param('payload');
16              
17             unless ($p) {
18             my $response = JSON::Any->encode( { success => 0 } );
19             return $response;
20             }
21              
22             $p = JSON::Any->new->decode( $p );
23              
24             debug(Dumper($p));
25              
26             my $repo_name = $p->{repository}{name};
27             my $organization = $p->{repository}{organization};
28             my $num_commits = @{$p->{commits}};
29             my $day_of_month = (localtime(time))[3];
30              
31             my $cmd=<
32             curl -d 'origin=%s&datapoints_text=%s %s "%s"&sendmail=0' %s/%s/goals/%s/datapoints/create_all
33             CMD
34              
35             $cmd = sprintf($cmd, config->{beeminder_username},
36             $day_of_month,
37             $num_commits,
38             "$organization/$repo_name", # optional comment
39             config->{beeminder_api_url},
40             config->{beeminder_username},
41             config->{beeminder_goal},
42             );
43             debug "Running: $cmd";
44             system $cmd;
45              
46             my $response = JSON::Any->encode( { success => 1 } );
47             };
48              
49             get '/' => sub {
50             'This is App::Beeminder::Hook';
51             };
52              
53             true;
54              
55             __END__