File Coverage

lib/Acme/Insult/Glax.pm
Criterion Covered Total %
statement 42 42 100.0
branch 5 6 83.3
condition 3 6 50.0
subroutine 10 10 100.0
pod 2 2 100.0
total 62 66 93.9


line stmt bran cond sub pod time code
1             package Acme::Insult::Glax 1.1 { # https://www.freepublicapis.com/insult-api
2 1     1   197648 use v5.38;
  1         3  
3 1     1   646 use HTTP::Tiny;
  1         53025  
  1         61  
4 1     1   705 use JSON::Tiny qw[decode_json];
  1         19552  
  1         86  
5 1     1   757 use URI;
  1         5054  
  1         62  
6 1     1   13 use parent 'Exporter';
  1         2  
  1         12  
7             our %EXPORT_TAGS = ( all => [ our @EXPORT_OK = qw[insult adjective] ] );
8             #
9 1   33 1   143 use overload '""' => sub ( $s, $u, $b ) { $s->{insult} // () };
  1     11   2  
  1         8  
  11         83  
  11         11500  
  11         24  
  11         14  
  11         15  
  11         14  
10             my $api = URI->new('https://insult.mattbas.org/api/');
11             #
12 11     11   20 sub _http ( $endpoint, %params ) {
  11         20  
  11         23  
  11         17  
13 11   66     77 state $http
14             //= HTTP::Tiny->new( default_headers => { Accept => 'application/json' }, agent => sprintf '%s/%.2f ', __PACKAGE__, our $VERSION );
15 11         296 ( my $hey = $api->clone )->path( '/api/' . $endpoint . '.json' );
16 11         816 $hey->query_form(%params);
17 11         1031 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
18 11 100       1148919 $res->{success} ? decode_json( $res->{content} ) : ();
19             }
20             #
21 7 50   7 1 6275 sub insult (%args) { my $ref = _http( insult => %args ); $ref ? bless $ref, __PACKAGE__ : $ref }
  7         20  
  7         17  
  7         26  
  7         3380  
22              
23 4     4 1 199143 sub adjective ( $lang //= 'en' ) {
  4         16  
  4         4  
24 4         29 my $ref = _http( adjective => ( lang => $lang ) );
25 4 100       1162 $ref ? bless $ref, __PACKAGE__ : $ref;
26             }
27             }
28             1;
29             __END__