File Coverage

blib/lib/WebService/Bukget.pm
Criterion Covered Total %
statement 21 65 32.3
branch 0 26 0.0
condition 0 38 0.0
subroutine 7 13 53.8
pod 4 4 100.0
total 32 146 21.9


line stmt bran cond sub pod time code
1 1     1   1387 use strict;
  1         2  
  1         88  
2 1     1   8 use warnings;
  1         2  
  1         69  
3             package WebService::Bukget;
4             {
5             $WebService::Bukget::VERSION = '1.00';
6             }
7             # ABSTRACT: Provides access to the v3 Bukget API
8 1     1   839 use Mojo::Base '-base';
  1         25986  
  1         9  
9 1     1   1239 use Mojo::JSON;
  1         426849  
  1         58  
10 1     1   1993 use Mojo::UserAgent;
  1         308780  
  1         20  
11 1     1   1123 use Try::Tiny qw/try catch/;
  1         1890  
  1         64  
12 1     1   792 use boolean;
  1         1665  
  1         6  
13              
14             has '_ua' => sub { Mojo::UserAgent->new };
15              
16             # these are here so they can be overridden if needed
17             has 'url_base' => 'http://dev.bukget.org/3';
18              
19             sub _fetch {
20 0     0     my $self = shift;
21 0           my $e = shift;
22 0           my $args = shift;
23 0   0       my $p = $args->{'params'} || {};
24              
25 0           my $u = Mojo::URL->new(sprintf('%s/%s', $self->url_base, $e));
26 0           foreach my $pk (keys(%$p)) {
27 0           $u->query->param($pk => $p->{$pk});
28             }
29              
30 0 0         if(my $tx = $self->_ua->get($u)) {
31 0 0         if(my $res = $tx->success) {
32 0 0         $args->{on_success}->($self => $res->json) if(defined($args->{on_success}));
33             } else {
34 0           my ($err, $code) = $tx->error;
35 0 0         $args->{on_failure}->($self => $err => $code) if(defined($args->{on_failure}));
36             }
37             } else {
38 0 0         $args->{on_failure}->($self) if(defined($args->{on_failure}));
39             }
40             }
41              
42             sub _fix_fields {
43 0     0     my $self = shift;
44 0           my $s = shift;
45              
46 0 0 0       $s->{params}->{fields} = join(',', @{$s->{params}->{fields}}) if(defined($s->{params}->{fields}) && ref($s->{params}->{fields}) eq 'ARRAY');
  0            
47 0           return $s;
48             }
49              
50             # endpoint accessors
51 0     0 1   sub geninfo { shift->_fetch('geninfo' => shift) }
52              
53             sub plugins {
54 0     0 1   my $self = shift;
55 0           my $s = shift;
56 0           my $e = 'plugins';
57              
58 0 0 0       unless(defined($s) && ref($s) eq 'HASH') {
59 0           $e .= '/' . $s;
60 0           $s = shift;
61             }
62 0           $self->_fetch($e => $self->_fix_fields($s));
63             }
64              
65             sub categories {
66 0     0 1   my $self = shift;
67 0           my $e = 'categories';
68 0           my $scn = shift;
69 0           my $cn = shift;
70 0           my $s = shift;
71              
72 0 0 0       if(defined($scn) && ref($scn) eq 'HASH') {
    0 0        
    0 0        
      0        
      0        
73             # plain categories
74 0           $self->_fetch('categories' => $self->_fix_fields($scn));
75             } elsif(defined($cn) && ref($cn) eq 'HASH') {
76             # categories/categoryname
77 0           $self->_fetch(sprintf('categories/%s', $scn) => $self->_fix_fields($cn));
78             } elsif(defined($scn) && defined($cn) && defined($s) && ref($s) eq 'HASH') {
79             # categories/server/categoryname
80 0           $self->_fetch(sprintf('categories/%s/%s', $scn, $cn) => $self->_fix_fields($s));
81             }
82             }
83              
84             sub authors {
85 0     0 1   my $self = shift;
86 0           my $e = 'authors';
87 0           my $san = shift;
88 0           my $an = shift;
89 0           my $s = shift;
90              
91 0 0 0       if(defined($san) && ref($san) eq 'HASH') {
    0 0        
    0 0        
      0        
      0        
92             # plain authors
93 0           $self->_fetch('authors' => $self->_fix_fields($san));
94             } elsif(defined($an) && ref($an) eq 'HASH') {
95             # authors/authorname
96 0           $self->_fetch(sprintf('authors/%s', $san) => $self->_fix_fields($an));
97             } elsif(defined($san) && defined($an) && defined($s) && ref($s) eq 'HASH') {
98             # authors/server/authorname
99 0           $self->_fetch(sprintf('authors/%s/%s', $san, $an) => $self->_fix_fields($s));
100             }
101             }
102              
103             1;
104              
105             __END__