| 1 | package CORION::Apache::Tika::Connection; | ||
| 2 | use strict; | ||
| 3 | use Moo::Role; | ||
| 4 | use JSON::XS; | ||
| 5 | use vars qw($VERSION); | ||
| 6 | $VERSION = '0.06'; | ||
| 7 | |||
| 8 | sub decode_response { | ||
| 9 | my( $self, $body ) = @_; | ||
| 10 | |||
| 11 | return decode_json( $body ); | ||
| 12 | } | ||
| 13 | |||
| 14 | sub process_response { | ||
| 15 | my ( $self, $params, $code, $msg, $body, $headers ) = @_; | ||
| 16 | |||
| 17 | my $mime_type = $headers->{"content-type"}; | ||
| 18 | |||
| 19 | my $is_encoded = $mime_type && $mime_type !~ m!^text/plain\b!; | ||
| 20 | |||
| 21 | # Request is successful | ||
| 22 | |||
| 23 | if ( $code >= 200 and $code <= 209 ) { | ||
| 24 | if ( defined $body and length $body ) { | ||
| 25 | # Let's hope it's JSON | ||
| 26 | $body = $self->decode_response($body) | ||
| 27 | if $is_encoded; | ||
| 28 | return $code, $body; | ||
| 29 | } | ||
| 30 | return ( $code, 1 ) if $params->{method} eq 'HEAD'; | ||
| 31 | return ( $code, '' ); | ||
| 32 | } | ||
| 33 | |||
| 34 | # Check if the error should be ignored | ||
| 35 | return ($code, $body); | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | =head1 REPOSITORY | ||
| 41 | |||
| 42 | The public repository of this module is | ||
| 43 | L<https://github.com/Corion/apache-tika>. | ||
| 44 | |||
| 45 | =head1 SUPPORT | ||
| 46 | |||
| 47 | The public support forum of this module is | ||
| 48 | L<https://perlmonks.org/>. | ||
| 49 | |||
| 50 | =head1 BUG TRACKER | ||
| 51 | |||
| 52 | Please report bugs in this module via the RT CPAN bug queue at | ||
| 53 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=CORION-Apache-Tika> | ||
| 54 | or via mail to L<corion-apache-tika-Bugs@rt.cpan.org>. | ||
| 55 | |||
| 56 | =head1 AUTHOR | ||
| 57 | |||
| 58 | Max Maischein C<corion@cpan.org> | ||
| 59 | |||
| 60 | =head1 COPYRIGHT (c) | ||
| 61 | |||
| 62 | Copyright 2014-2016 by Max Maischein C<corion@cpan.org>. | ||
| 63 | |||
| 64 | =head1 LICENSE | ||
| 65 | |||
| 66 | This module is released under the same terms as Perl itself. | ||
| 67 | |||
| 68 | =cut |