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