| 1 | use 5.010; # _Pulp__5010_qr_m_propagate_properly | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | package Net::Travis::API::UA::Response; | ||
| 7 | $Net::Travis::API::UA::Response::VERSION = '0.001001'; | ||
| 8 | # ABSTRACT: Subclass of HTTP::Tiny::UA::Response for utility methods | ||
| 9 | |||
| 10 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | use Moo qw( extends has ); | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | use Encode qw( FB_CROAK ); | ||
| 54 | |||
| 55 | extends 'HTTP::Tiny::UA::Response'; | ||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | has 'json' => ( | ||
| 66 | is => ro =>, | ||
| 67 | lazy => 1, | ||
| 68 | builder => sub { | ||
| 69 | require JSON; | ||
| 70 | return JSON->new(); | ||
| 71 | }, | ||
| 72 | ); | ||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | sub content_type { | ||
| 87 | my ($self) = @_; | ||
| 88 | return unless exists $self->headers->{'content-type'}; | ||
| 89 | return | ||
| 90 | unless my ($type) = $self->headers->{'content-type'} =~ qr{ \A ( [^/]+ / [^;]+ ) }msx; | ||
| 91 | return $type; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | sub content_type_params { | ||
| 110 | my ($self) = @_; | ||
| 111 | return [] unless exists $self->headers->{'content-type'}; | ||
| 112 | return [] | ||
| 113 | unless my (@params) = $self->headers->{'content-type'} =~ qr{ (?:;([^;]+))+ }msx; | ||
| 114 | return [@params]; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | sub decoded_content { | ||
| 134 | my ( $self, $force_encoding ) = @_; | ||
| 135 | if ( not $force_encoding ) { | ||
| 136 | return $self->content if not my $type = $self->content_type; | ||
| 137 | return $self->content unless $type =~ qr{ \Atext/ }msx; | ||
| 138 | for my $param ( @{ $self->content_type_params } ) { | ||
| 139 | if ( $param =~ qr{ \Acharset=(.+)\z }msx ) { | ||
| 140 | $force_encoding = $param; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | return $self->content if not $force_encoding; | ||
| 144 | } | ||
| 145 | return Encode::decode( $force_encoding, $self->content, Encode::FB_CROAK ); | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | sub content_json { | ||
| 166 | my ( $self, $force ) = @_; | ||
| 167 | my ($has_force) = ( @_ > 1 ); | ||
| 168 | |||
| 169 | my %whitelist = ( 'application/json' => 1 ); | ||
| 170 | return unless $has_force or exists $whitelist{ $self->content_type }; | ||
| 171 | my $charset = 'utf-8'; | ||
| 172 | if ( $has_force and defined $force ) { | ||
| 173 | $charset = $force; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | for my $param ( @{ $self->content_type_params } ) { | ||
| 177 | next unless $param =~ /\Acharset=(.+)\z/msx; | ||
| 178 | $charset = $1; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | return $self->json->utf8(0)->decode( $self->decoded_content($charset) ); | ||
| 182 | } | ||
| 183 | |||
| 184 | no Moo; | ||
| 185 | |||
| 186 | 1; | ||
| 187 | |||
| 188 | __END__ |