Acme-Insult-Glax

 view release on metacpan or  search on metacpan

lib/Acme/Insult/Glax.pm  view on Meta::CPAN

    #
    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;
    }
}
1;
__END__

=encoding utf-8

=head1 NAME

Acme::Insult::Glax - Programmatically Generate Insults

t/00_compile.t  view on Meta::CPAN

    ok adjective(),               'generate random adjective';
    ok adjective('en_corporate'), 'generate random corpo-jargon adjective';
    is my $adjective = adjective(), hash {
        field args => hash {
            field lang => string 'en';
            field template => string '<adjective>'
        };
        field error  => F();
        field insult => D();
    }, 'adjective is a hash';
    isa_ok $adjective, ['Acme::Insult::Glax'], 'adjective is a *blessed* hash';
    $adjective->{insult} = 'Just a test';
    is $adjective,           'Just a test', 'stringify';
    is adjective('garbage'), U(),           'fail to generate random garbage lang adjective';
};
subtest 'insult' => sub {
    is insult(), D(), 'totally random';
    is my $adjective = insult(), hash {
        field args => hash {
            field lang => string 'en';
            field template => D()    # template subject to change
        };
        field error  => F();
        field insult => D();
    }, 'insult is a hash';
    isa_ok $adjective, ['Acme::Insult::Glax'], 'insult is a *blessed* hash';
    $adjective->{insult} = 'Just a test';
    is $adjective, 'Just a test', 'stringify';
    like insult( who => 'Alex' ),                               qr[^Alex is],                   'provide a name';
    like insult( who => 'Peter, Paul, and Mary', plural => 1 ), qr[^Peter, Paul, and Mary are], 'plural';
    is insult( lang => 'en' ),           D(), 'english';
    is insult( lang => 'en_corporate' ), D(), 'corperate lingo';
    like insult( template => 'Jake the <adjective> dog and Finn the <adjective min=1 max=3 id=adj1> <animal>.' ),
        qr[^Jake the .+ dog and Finn the .+$], 'templated insult';
};
#



( run in 1.313 second using v1.01-cache-2.11-cpan-de7293f3b23 )