File Coverage

blib/lib/WebService/Tumblr/Dispatch.pm
Criterion Covered Total %
statement 48 64 75.0
branch 9 14 64.2
condition n/a
subroutine 11 14 78.5
pod 0 6 0.0
total 68 98 69.3


line stmt bran cond sub pod time code
1             package WebService::Tumblr::Dispatch;
2              
3 3     3   17 use strict;
  3         7  
  3         99  
4 3     3   15 use warnings;
  3         25  
  3         90  
5              
6 3     3   14 use Any::Moose;
  3         5  
  3         28  
7 3     3   4472 use Try::Tiny;
  3         4382  
  3         173  
8 3     3   20 use HTTP::Request;
  3         5  
  3         2075  
9              
10             has tumblr => qw/ is ro required 1 isa WebService::Tumblr weak_ref 1 /, handles => [qw/ agent /];
11             has request => qw/ is ro lazy_build 1 isa HTTP::Request /;
12             sub _build_request {
13 2     2   14 my $self = shift;
14              
15 2         7 my $method = $self->method;
16 2         8 my $uri = $self->uri;
17 2 50       18 $uri = $uri->uri if $uri->can( 'uri' );
18 2         29 my $query = $self->query;
19              
20 2         15 my $request = HTTP::Request->new( $method, $uri );
21            
22 2 100       200 if ( 'POST' eq uc $method ) {
23 1         9 require URI;
24 1         5 my $tmp = URI->new('http:');
25 1         50 $tmp->query_form( %$query );
26 1         205 my $content = $tmp->query;
27 1         25 $request->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
28 1         95 $request->header( 'Content-Length' => length $content );
29 1         44 $request->content( $content );
30             }
31             else {
32 1         5 $request->uri->query_form( %$query );
33             }
34              
35 2         125 return $request;
36             }
37              
38             has result => qw/ is ro lazy_build 1 /, handles => [qw/ content response is_success /];
39             sub _build_result {
40 0     0   0 my $self = shift;
41 0         0 return $self->_submit;
42             }
43              
44             has url => qw/ accessor _url required 1 /;
45             sub url {
46 2     2 0 3 my $self = shift;
47 2 50       14 return $self->_url unless @_;
48 0         0 $self->_url( @_ );
49 0         0 return $self;
50             }
51 2     2 0 7 sub uri { return shift->url( @_ ) }
52              
53             has method => qw/ accessor _method required 1 isa Str /;
54             sub method {
55 4     4 0 29 my $self = shift;
56 4 100       22 return $self->_method unless @_;
57 1         4 $self->_method( @_ );
58 1         2 return $self;
59             }
60              
61             has query => qw/ accessor _query isa HashRef /, default => sub { {} };
62             sub query {
63 4     4 0 6 my $self = shift;
64 4         11 my $query = $self->_query;
65 4 50       20 return $query unless @_;
66 0         0 my %set = @_;
67 0         0 @$query{ keys %set } = values %set;
68 0         0 $self->_query( $query ); # Probably do not need to do this, but, eh
69 0         0 return $self;
70             }
71              
72             sub _submit {
73 0     0   0 my $self = shift;
74            
75 0         0 my $agent = $self->agent;
76 0         0 my $request = $self->request;
77 0         0 my $response = $agent->request( $request );
78 0         0 my $result = WebService::Tumblr::Result->new( dispatch => $self, request => $request, response => $response );
79              
80 0         0 return $result;
81             }
82              
83             sub submit {
84 0     0 0 0 my $self = shift;
85            
86 0         0 return $self->result->content;
87             }
88              
89             sub authenticate {
90 1     1 0 3 my $self = shift;
91              
92 1         3 my $query = $self->query;
93 1         7 my ( $email, $password ) = $self->tumblr->identity;
94 1 50       9 defined $query->{ email } or $query->{ email } = $email;
95 1 50       4 defined $query->{ password } or $query->{ password } = $password;
96              
97 1         4 $self->method( 'POST' );
98              
99 1         2 return $self;
100             }
101              
102              
103             1;