File Coverage

blib/lib/WWW/Tumblr/API.pm
Criterion Covered Total %
statement 34 34 100.0
branch 6 6 100.0
condition 10 12 83.3
subroutine 25 25 100.0
pod 0 1 0.0
total 75 78 96.1


line stmt bran cond sub pod time code
1             package WWW::Tumblr::API;
2              
3 16     16   120 use strict;
  16         35  
  16         654  
4 16     16   111 use warnings;
  16         49  
  16         1178  
5              
6 16     16   139 use Moose;
  16         39  
  16         188  
7 16     16   139720 use JSON 'decode_json';
  16         36  
  16         239  
8 16     16   2788 use Moose::Exporter;
  16         37  
  16         204  
9             Moose::Exporter->setup_import_methods(with_caller => ['tumblr_api_method']);
10 16     16   13699 use WWW::Tumblr::ResponseError;
  16         99  
  16         7378  
11              
12             sub tumblr_api_method ($$) {
13 272     272 0 6816 my $class = Moose::Meta::Class->initialize( shift );
14 272         4536 my $method_name = $_[0];
15 272         503 my $method_spec = $_[1];
16              
17             my $sub = sub {
18 25     25   59572 my $self = shift;
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
        25      
19 25         100 my $args = { @_ };
20              
21 25         62 my ( $http_method, $auth_method, $req_params, $url_param ) = @{ $method_spec };
  25         109  
22            
23 25         59 my $kind = lc( pop( @{ [ split '::', ref $self ] }));
  25         265  
24              
25             my $response = $self->_tumblr_api_request({
26             auth => $auth_method,
27             http_method => $http_method,
28             url_path => $kind . '/' . ( $kind eq 'blog' ? $self->base_hostname . '/' : '' ) .
29             join('/', split /_/, $method_name) .
30             ( defined $url_param && defined $args->{ $url_param } ?
31 25 100 100     1384 '/' . delete( $args->{ $url_param } ) : ''
    100          
32             ),
33             extra_args => $args,
34             });
35              
36 25 100 66     3135427 if ( $response->is_success || ( ($response->code == 301 || $response->code == 302) && $method_name eq 'avatar') ) {
      66        
      100        
37 24         907 return decode_json($response->decoded_content)->{response};
38             } else {
39 1         103 $self->error( WWW::Tumblr::ResponseError->new(
40             response => $response
41             ) );
42 1         16 return;
43             }
44 272         1881 };
45              
46 272         768 $class->add_method($method_name, $sub );
47              
48             }
49              
50             1;