| 1 | package Exobrain::Measurement::Beeminder; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use autodie; | ||
| 5 | use Moose; | ||
| 6 | use Carp qw(croak); | ||
| 7 | use Method::Signatures; | ||
| 8 | |||
| 9 | # ABSTRACT: Beeminder measurement packet | ||
| 10 | our $VERSION = '1.06'; # VERSION | ||
| 11 | |||
| 12 | # Declare that we will have a summary attribute. This is to make | ||
| 13 | # our roles happy. | ||
| 14 | sub summary; | ||
| 15 | |||
| 16 | # This needs to happen at begin time so it can add the 'payload' | ||
| 17 | # keyword. | ||
| 18 | BEGIN { with 'Exobrain::Message'; } | ||
| 19 | |||
| 20 | |||
| 21 | # TODO: Exobrain msgs normally have a source, which are *different* from | ||
| 22 | # the beeminder user/goal string used here. We need to tease them | ||
| 23 | # apart. | ||
| 24 | |||
| 25 | has source => (isa => 'Str', is => 'ro'); # Used to construct user/goal | ||
| 26 | |||
| 27 | payload user => ( isa => 'Str', builder => '_build_user', lazy => 1 ); | ||
| 28 | payload goal => ( isa => 'Str', builder => '_build_goal', lazy => 1 ); | ||
| 29 | |||
| 30 | payload id => ( isa => 'Str' ); | ||
| 31 | payload action => ( isa => 'Str' ); | ||
| 32 | payload value => ( isa => 'Str' ); | ||
| 33 | payload comment => ( isa => 'Str', required => 0 ); | ||
| 34 | |||
| 35 | # Tweak timestamp to be created from the bmndr return | ||
| 36 | has '+timestamp' => ( init_arg => 'created' ); | ||
| 37 | |||
| 38 | has summary => ( | ||
| 39 | isa => 'Str', builder => '_build_summary', lazy => 1, is => 'ro' | ||
| 40 | ); | ||
| 41 | |||
| 42 | # Methods for splitting the source into user and goal | ||
| 43 | |||
| 44 | method _build_user() { | ||
| 45 | return $self->_split_summary('user'); | ||
| 46 | } | ||
| 47 | |||
| 48 | method _build_goal() { | ||
| 49 | return $self->_split_summary('goal'); | ||
| 50 | } | ||
| 51 | |||
| 52 | method _split_summary($attr!) { | ||
| 53 | my %attrs; | ||
| 54 | @attrs{qw(user goal)} = split('/', $self->source); | ||
| 55 | |||
| 56 | return $attrs{$attr} or croak "No source attribute provided, can't auto-generate $attr attribute"; | ||
| 57 | } | ||
| 58 | |||
| 59 | method _build_summary() { | ||
| 60 | return join(" ", | ||
| 61 | $self->user, "added beeminder", $self->goal, "data: ", | ||
| 62 | $self->value, | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ |