| 1 | package WebService::Zaqar::Middleware::Format::JSONSometimes; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: middleware for JSON format | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | extends 'Net::HTTP::Spore::Middleware::Format'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | has _json_parser => ( | ||||||||||||||||||||||||||||
| 10 | is => 'rw', | ||||||||||||||||||||||||||||
| 11 | isa => 'JSON', | ||||||||||||||||||||||||||||
| 12 | lazy => 1, | ||||||||||||||||||||||||||||
| 13 | 3 | ▶ | default => sub { JSON->new->utf8(1)->allow_nonref }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | sub encode { $_[0]->_json_parser->encode( $_[1] ); } | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | sub decode { $_[0]->_json_parser->decode( $_[1] ); } | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | sub accept_type { ( 'Accept' => 'application/json' ) } | ||||||||||||||||||||||||||
|
Subroutine
accept_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sub content_type { ( 'Content-Type' => 'application/json;' ) } | ||||||||||||||||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | sub should_serialize { | ||||||||||||||||||||||||||
|
Subroutine
should_serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 23 | 5 | $self->_check_serializer( shift->env, $self->serializer_key ); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | sub should_deserialize { | ||||||||||||||||||||||||||
|
Subroutine
should_deserialize: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 4 | my ($self, $response) = @_; | |||||||||||||||||||||||||||
| 28 | 4 | ▶ | $self->_check_is_authenticated( $response ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | and $self->_check_serializer( $response->env, $self->deserializer_key ); | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | sub _check_is_authenticated { | ||||||||||||||||||||||||||
|
Subroutine
_check_is_authenticated: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 4 | my ($self, $response) = @_; | |||||||||||||||||||||||||||
| 34 | 4 | $response->status < 400; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 9 | ▶ | sub _check_serializer { | ||||||||||||||||||||||||||
|
Subroutine
_check_serializer: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 38 | 9 | my ( $self, $env, $key ) = @_; | |||||||||||||||||||||||||||
| 39 | 9 | ▶ | if ( exists $env->{$key} && $env->{$key} == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $env->{$key} and $env->{$key} == 1
MC/DC0%0/2
exists $env->{$key} and $env->{$key} == 1
exists $env->{$key}
$env->{$key} == 1
Truth table33%1/3
A and B
A= exists $env->{$key}B= $env->{$key} == 1
| |||||||||||||||||||||||||||||
| 40 | 0 | return 0; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | else { | ||||||||||||||||||||||||||||
| 43 | 9 | return 1; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 5 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 5 | ▶ | return unless $self->should_serialize( $req ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 5 | $req->header( $self->accept_type ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | if ( $req->env->{'spore.payload'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | $req->env->{'spore.payload'} = | ||||||||||||||||||||||||||||
| 56 | 2 | $self->encode( $req->env->{'spore.payload'} ); | |||||||||||||||||||||||||||
| 57 | 2 | $req->header( $self->content_type ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 5 | $req->env->{ $self->serializer_key } = 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | return $self->response_cb( | ||||||||||||||||||||||||||||
| 63 | 5 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 64 | 5 | my $res = shift; | |||||||||||||||||||||||||||
| 65 | 5 | ▶ | if ( $res->body ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | return if $res->code >= 500; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | return unless $self->should_deserialize( $res ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 4 | my $content = $self->decode( $res->body ); | |||||||||||||||||||||||||||
| 69 | 4 | $res->body($content); | |||||||||||||||||||||||||||
| 70 | 4 | $res->env->{ $self->deserializer_key } = 1; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 5 | ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | 1; | |||||||||||||||||||||||||||