File Coverage

lib/Acme/Insult/Pirate.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 4 50.0
condition 5 9 55.5
subroutine 8 8 100.0
pod 1 1 100.0
total 48 54 88.8


line stmt bran cond sub pod time code
1             package Acme::Insult::Pirate 1.1 {
2 1     1   246001 use v5.38;
  1         6  
3 1     1   934 use HTTP::Tiny;
  1         73294  
  1         67  
4              
5             #~ use JSON::Tiny qw[decode_json]; # Not needed here (yet)
6 1     1   897 use URI;
  1         7817  
  1         48  
7 1     1   9 use parent 'Exporter';
  1         5  
  1         10  
8             our %EXPORT_TAGS = ( all => [ our @EXPORT_OK = qw[insult] ] );
9             #
10 1   33 1   187 use overload '""' => sub ( $s, $u, $b ) { $s->{insult} // () };
  1     4   2  
  1         9  
  4         37  
  4         702  
  4         9  
  4         7  
  4         7  
  4         6  
11             #
12 2     2   5 sub _http (%params) {
  2         5  
  2         3  
13 2   66     65 state $http
14             //= HTTP::Tiny->new( default_headers => { Accept => 'application/json' }, agent => sprintf '%s/%.2f ', __PACKAGE__, our $VERSION );
15 2   66     169 state $api //= URI->new('https://pirate.monkeyness.com/api/insult');
16              
17             #~ $api->query_form( %params );
18 2         12989 my $res = $http->get($api); # {success} is true even when advice is not found but we'll at least know when we have valid JSON
19              
20             # This API only returns strings for now
21             #~ $res->{success} ? decode_json( $res->{content} ) : ();
22 2 50       1096739 $res->{success} ? { insult => $res->{content} } : ();
23             }
24             #
25 2 50   2 1 299149 sub insult (%args) { my $ref = _http(%args); $ref ? bless $ref, __PACKAGE__ : $ref }
  2         5  
  2         4  
  2         13  
  2         43  
26             }
27             1;
28             __END__