| 1 | 3 | ▶ | use 5.006; # pragmas, our | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package HTTP::Tiny::Mech; | ||||||||||
| 6 | |||||||||||
| 7 | 3 | our $VERSION = '1.001001'; | |||||||||
| 8 | |||||||||||
| 9 | # ABSTRACT: Wrap a WWW::Mechanize instance in an HTTP::Tiny compatible interface. | ||||||||||
| 10 | |||||||||||
| 11 | 3 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||
| 12 | |||||||||||
| 13 | 3 | ▶ | use parent 'HTTP::Tiny'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | sub new { | ||||||||||
| 16 | 5 | ▶ | my ( $self, %args ) = @_; | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 17 | 5 | my ( $mechua, $has_mechua ); | |||||||||
| 18 | 5 | ▶ | if ( exists $args{mechua} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 2 | $has_mechua = 1; | |||||||||
| 20 | 2 | $mechua = delete $args{mechua}; | |||||||||
| 21 | } | ||||||||||
| 22 | 5 | my $instance = $self->SUPER::new(%args); | |||||||||
| 23 | 5 | ▶ | if ($has_mechua) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 2 | $instance->{mechua} = $mechua; | |||||||||
| 25 | } | ||||||||||
| 26 | 5 | return bless $instance, $self; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | ## no critic (Subroutines::RequireArgUnpacking) | ||||||||||
| 30 | sub mechua { | ||||||||||
| 31 | 15 | ▶ | my ( $self, $new_mechua, $has_new_mechua ) = ( $_[0], $_[1], @_ > 1 ); | ||||||||
|
Subroutine
mechua: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 32 | 15 | ▶ | if ($has_new_mechua) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 1 | $self->{mechua} = $new_mechua; | |||||||||
| 34 | } | ||||||||||
| 35 | 15 | ▶ | return $self->{mechua} if exists $self->{mechua}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 1 | require WWW::Mechanize; | |||||||||
| 37 | 1 | return ( $self->{mechua} = WWW::Mechanize->new() ); | |||||||||
| 38 | } | ||||||||||
| 39 | ## use critic | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | sub _unwrap_response { | ||||||||||
| 51 | 4 | ▶ | my ( undef, $response ) = @_; | ||||||||
|
Subroutine
_unwrap_response: called CC=1 SCAR=0.0
| |||||||||||
| 52 | return { | ||||||||||
| 53 | 4 | status => $response->code, | |||||||||
| 54 | reason => $response->message, | ||||||||||
| 55 | headers => $response->headers, | ||||||||||
| 56 | success => $response->is_success, | ||||||||||
| 57 | content => $response->content, | ||||||||||
| 58 | }; | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub _wrap_request { | ||||||||||
| 62 | 2 | ▶ | my ( undef, $method, $uri, $opts ) = @_; | ||||||||
|
Subroutine
_wrap_request: called CC=3 SCAR=11.0
| |||||||||||
| 63 | 2 | require HTTP::Request; | |||||||||
| 64 | 2 | my $req = HTTP::Request->new( $method, $uri ); | |||||||||
| 65 | 2 | ▶ | $req->headers( $opts->{headers} ) if $opts->{headers}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 2 | ▶ | $req->content( $opts->{content} ) if $opts->{content}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 2 | return $req; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | sub get { | ||||||||||
| 79 | 2 | ▶ | my ( $self, $uri, $opts ) = @_; | ||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 80 | 2 | ▶ | return $self->_unwrap_response( $self->mechua->get( $uri, ( $opts ? %{$opts} : () ) ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | sub request { | ||||||||||
| 90 | 2 | ▶ | my ( $self, @request ) = @_; | ||||||||
|
Subroutine
request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 91 | 2 | my $req = $self->_wrap_request(@request); | |||||||||
| 92 | 2 | my $response = $self->mechua->request($req); | |||||||||
| 93 | 2 | return $self->_unwrap_response($response); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | |||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | 3 | 1; | |||||||||
| 132 | |||||||||||
| 133 | __END__ | ||||||||||