| 1 | use 5.006; # pragmas, our | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package HTTP::Tiny::Mech; | ||
| 6 | |||
| 7 | our $VERSION = '1.001001'; | ||
| 8 | |||
| 9 | # ABSTRACT: Wrap a WWW::Mechanize instance in an HTTP::Tiny compatible interface. | ||
| 10 | |||
| 11 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 12 | |||
| 13 | use parent 'HTTP::Tiny'; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ( $self, %args ) = @_; | ||
| 17 | my ( $mechua, $has_mechua ); | ||
| 18 | if ( exists $args{mechua} ) { | ||
| 19 | $has_mechua = 1; | ||
| 20 | $mechua = delete $args{mechua}; | ||
| 21 | } | ||
| 22 | my $instance = $self->SUPER::new(%args); | ||
| 23 | if ($has_mechua) { | ||
| 24 | $instance->{mechua} = $mechua; | ||
| 25 | } | ||
| 26 | return bless $instance, $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | ## no critic (Subroutines::RequireArgUnpacking) | ||
| 30 | sub mechua { | ||
| 31 | my ( $self, $new_mechua, $has_new_mechua ) = ( $_[0], $_[1], @_ > 1 ); | ||
| 32 | if ($has_new_mechua) { | ||
| 33 | $self->{mechua} = $new_mechua; | ||
| 34 | } | ||
| 35 | return $self->{mechua} if exists $self->{mechua}; | ||
| 36 | require WWW::Mechanize; | ||
| 37 | 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 | my ( undef, $response ) = @_; | ||
| 52 | return { | ||
| 53 | 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 | my ( undef, $method, $uri, $opts ) = @_; | ||
| 63 | require HTTP::Request; | ||
| 64 | my $req = HTTP::Request->new( $method, $uri ); | ||
| 65 | $req->headers( $opts->{headers} ) if $opts->{headers}; | ||
| 66 | $req->content( $opts->{content} ) if $opts->{content}; | ||
| 67 | return $req; | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | sub get { | ||
| 79 | my ( $self, $uri, $opts ) = @_; | ||
| 80 | return $self->_unwrap_response( $self->mechua->get( $uri, ( $opts ? %{$opts} : () ) ) ); | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | sub request { | ||
| 90 | my ( $self, @request ) = @_; | ||
| 91 | my $req = $self->_wrap_request(@request); | ||
| 92 | my $response = $self->mechua->request($req); | ||
| 93 | 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 | 1; | ||
| 132 | |||
| 133 | __END__ |