#!/usr/bin/perl
use v5.10.0;
use strict;
use warnings;
use autodie;
use Exobrain;
use Dancer;
use Try::Tiny;

# PODNAME: beeminder-callback
# ABSTRACT: Turn Beeminder callbacks into exobrain measurements

use Data::Dumper;
use constant DEBUG => 1;

set port => 3000;
set logger => 'console';
set log => 'core';
set warnings => 1;


# Catch all requests.
post qr{.*} => sub {

    state $exobrain = Exobrain->new;

    # If we see what could be a valid response, but it's not
    # an 'ADD', then ignore it.
    if( (param('action')||"") ne 'ADD') {
        warning("Non-add packet received");
        return "IGNORED";
    }

    my $pktdump = Dumper ({ params() });
    debug("Receieved packet: $pktdump");

    # Exobrain just accepts the packet whole.

    try {
        debug("About to send packet on exobrain bus...");
        $exobrain->measure( 'Beeminder', params(), raw => { params() } );
        debug("Packet sent on exobrain bus");
    }
    catch {
        error("Sending on exobrain bus failed: $_");
        status 'error';
        return "Invalid packet";
    };

    return "OK";
};

dance;

__END__

=pod

=head1 NAME

beeminder-callback - Turn Beeminder callbacks into exobrain measurements

=head1 VERSION

version 1.04

=begin example

This is what a set of params look like when they come in.

'params' => {
            'source' => 'pjf/test',
            'urtext' => '2013 09 14.5 1 "Testy test test"',
            'value' => '1.0',
            'splat' => $VAR1->{'_route_params'}{'splat'},
            'origin' => 'web',
            'created' => '1379125551',
            'comment' => 'Testy test test',
            'action' => 'ADD',      # Have also seen 'DEL'
            'id' => '5233c92fcc19310bb5000008',
            'daystamp' => '15962'
},

=end example

=head1 AUTHOR

Paul Fenwick <pjf@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Paul Fenwick.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
