File Coverage

blib/lib/Acme/Free/Dog/API.pm
Criterion Covered Total %
statement 20 48 41.6
branch 0 10 0.0
condition n/a
subroutine 7 12 58.3
pod 5 5 100.0
total 32 75 42.6


line stmt bran cond sub pod time code
1             package Acme::Free::Dog::API;
2              
3 1     1   334523 use v5.10;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         17  
5 1     1   2 use warnings;
  1         14  
  1         67  
6              
7             our $VERSION = '0.9.10';
8              
9 1     1   780 use HTTP::Tiny;
  1         51644  
  1         76  
10 1     1   1066 use JSON qw/decode_json/;
  1         10003  
  1         4  
11 1     1   623 use Util::H2O::More qw/baptise ddd HTTPTiny2h2o h2o/;
  1         9594  
  1         134  
12              
13             use constant {
14 1         601 BASEURL => "https://dog.ceo/api",
15 1     1   11 };
  1         2  
16              
17             sub new {
18 0     0 1   my $pkg = shift;
19 0           my $self = baptise { ua => HTTP::Tiny->new }, $pkg;
20 0           return $self;
21             }
22              
23             # used by:bin/fletch breeds
24             sub breeds {
25 0     0 1   my $self = shift;
26              
27             # https://dog.ceo/api/breeds/list/all
28 0           my $URL = sprintf "%s/%s", BASEURL, "breeds/list/all";
29              
30 0           my $resp = HTTPTiny2h2o $self->ua->get($URL);
31 0 0         die sprintf( "fatal: API did not provide a useful response (status: %d)\n", $resp->status ) if ( $resp->status != 200 );
32              
33 0           return $resp->content->message;
34             }
35              
36             # used by: bin/fletch subbreeds --breed BREED
37             sub subbreeds {
38 0     0 1   my $self = shift;
39 0           my $params = h2o {@_}, qw/breed/;
40              
41             # https://dog.ceo/api/breed/hound/list
42 0           my $URL = sprintf "%s/breed/%s/list", BASEURL, $params->breed;
43              
44 0           my $resp = HTTPTiny2h2o $self->ua->get($URL);
45 0 0         die sprintf( "fatal: API did not provide a useful response (status: %d)\n", $resp->status ) if ( $resp->status != 200 );
46              
47 0           return $resp->content->message;
48             }
49              
50             # used by: bin/fletch images --breed BREED
51             # Note: API doesn't support getting images by subbreed
52             sub images {
53 0     0 1   my $self = shift;
54 0           my $params = h2o {@_}, qw/breed/;
55              
56             # https://dog.ceo/api/breed/hound/images
57 0           my $URL = sprintf "%s/breed/%s/images", BASEURL, $params->breed;
58              
59 0           my $resp = HTTPTiny2h2o $self->ua->get($URL);
60 0 0         die sprintf( "fatal: API did not provide a useful response (status: %d)\n", $resp->status ) if ( $resp->status != 200 );
61              
62 0           return $resp->content->message;
63             }
64              
65             # used by: bin/fletch random [--breed BREED]
66             sub random {
67 0     0 1   my $self = shift;
68 0           my $params = h2o {@_}, qw/breed/;
69              
70             # https://dog.ceo/api/breeds/image/random Fetch!
71 0           my $URL = sprintf "%s/breeds/image/random", BASEURL;
72              
73             # handle optional, 'breed => BREED'
74 0 0         if ($params->breed) {
75             # https://dog.ceo/api/breed/affenpinscher/images/random
76 0           $URL = sprintf "%s/breed/%s/images/random", BASEURL, lc $params->breed;
77             }
78              
79 0           my $resp = HTTPTiny2h2o $self->ua->get($URL);
80 0 0         die sprintf( "fatal: API did not provide a useful response (status: %d)\n", $resp->status ) if ( $resp->status != 200 );
81              
82 0           return $resp->content->message;
83             }
84              
85             1;
86              
87             __END__