Acme-Insult-Glax
view release on metacpan or search on metacpan
lib/Acme/Insult/Glax.pm view on Meta::CPAN
use v5.38;
use HTTP::Tiny;
use JSON::Tiny qw[decode_json];
use URI;
use parent 'Exporter';
our %EXPORT_TAGS = ( all => [ our @EXPORT_OK = qw[insult adjective] ] );
#
use overload '""' => sub ( $s, $u, $b ) { $s->{insult} // () };
my $api = URI->new('https://insult.mattbas.org/api/');
#
sub _http ( $endpoint, %params ) {
state $http
//= HTTP::Tiny->new( default_headers => { Accept => 'application/json' }, agent => sprintf '%s/%.2f ', __PACKAGE__, our $VERSION );
( my $hey = $api->clone )->path( '/api/' . $endpoint . '.json' );
$hey->query_form(%params);
my $res = $http->get( $hey->as_string ); # {success} is true even when advice is not found but we'll at least know when we have valid JSON
$res->{success} ? decode_json( $res->{content} ) : ();
}
#
sub insult (%args) { my $ref = _http( insult => %args ); $ref ? bless $ref, __PACKAGE__ : $ref }
sub adjective ( $lang //= 'en' ) {
my $ref = _http( adjective => ( lang => $lang ) );
$ref ? bless $ref, __PACKAGE__ : $ref;
( run in 0.313 second using v1.01-cache-2.11-cpan-b61123c0432 )