App-RoboBot
view release on metacpan or search on metacpan
lib/App/RoboBot/Plugin/API/Kegerator.pm view on Meta::CPAN
$message->response->push(
sprintf('*Tap %d:* %s (%s) by %s, %s - %s ABV%s',
$tap->{'tap_id'},
($beer->{'beer_name'} // 'n/a'),
($beer->{'beer_style'} // 'n/a'),
($beer->{'brewery_name'} // 'n/a'),
($beer->{'brewery_loc'} // 'n/a'),
($beer->{'abv'} ? sprintf('%.1f%%', $beer->{'abv'}) : 'n/a'),
($tap->{'pct_full'} ? sprintf(', %d%% remaining', $tap->{'pct_full'} * 100) : ''),
)
);
}
return;
}
sub make_keg_api_call {
my ($self, $path, $args) = @_;
return unless $self->bot->config->plugins->{'kegerator'}{'api_host'};
my $uri = URI->new;
$uri->scheme($self->bot->config->plugins->{'kegerator'}{'api_scheme'} // 'https');
$uri->host($self->bot->config->plugins->{'kegerator'}{'api_host'});
if (ref($path) eq 'ARRAY') {
$uri->path_segments(@{$path});
} else {
$uri->path($path);
}
if (defined $args && ref($args) eq 'HASH' && scalar(keys(%{$args})) > 0) {
$uri->query_form($args);
}
my $req = HTTP::Request->new( GET => $uri->as_string );
my $response = $self->ua->request($req);
return unless $response->is_success;
my $json;
eval {
$json = decode_json($response->decoded_content);
};
return if $@;
return $json;
}
sub _run_watcher {
my ($self, $bot) = @_;
# TODO: Call base API path, get JSON
# TODO: Loop through keg list, compare each one's last_updated with the
# plugin's last_check attribute. Any with a new update should be
# added to a list of tap#->beerid
# TODO: If any tap#'s were marked as new, call the beer detail API endpoint
# to get beer name, ABV, IBU, etc.
# TODO: If any @output to send, construct a mock Response object for each
# plugin->notice[server+channel], and send notifications out.
# TODO: Update plugin's last_check timestamp.
$self->watcher(
AnyEvent->timer(
after => 30,
cb => sub { $self->_run_watcher($bot) },
)
);
}
__PACKAGE__->meta->make_immutable;
1;
( run in 0.743 second using v1.01-cache-2.11-cpan-39bf76dae61 )