| 1 | package Net::HTTP::API::Role::Serialization; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | $Net::HTTP::API::Role::Serialization::VERSION = '0.14'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: do (de)serialization | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Net::HTTP::API::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has serializers => ( | ||||||||||||||||||||||||||||
| 15 | traits => ['Hash'], | ||||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | isa => 'HashRef[Net::HTTP::API::Parser]', | ||||||||||||||||||||||||||||
| 18 | 4 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | auto_deref => 1, | |||||||||||||||||||||||||||
| 20 | handles => { | ||||||||||||||||||||||||||||
| 21 | _add_serializer => 'set', | ||||||||||||||||||||||||||||
| 22 | _get_serializer => 'get', | ||||||||||||||||||||||||||||
| 23 | }, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | sub get_content { | ||||||||||||||||||||||||||
|
Subroutine
get_content: called CC=6 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 9 | my ($self, $result) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | return undef unless $result->content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | ▶ | my $content_type = $self->api_format // $result->header('Content-Type'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->api_format // $result->header("Content-Type")
MC/DC0%0/2
$self->api_format // $result->header("Content-Type")
$self->api_format
$result->header("Content-Type")
Truth table33%1/3
A // B
A= $self->api_formatB= $result->header("Content-Type")
| |||||||||||||||||||||||||||||
| 32 | 6 | $content_type =~ s/(;.+)$//; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | my $content; | |||||||||||||||||||||||||||
| 35 | 6 | ▶ | if ($result->is_success && $result->code != 204) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$result->is_success and $result->code != 204
MC/DC0%0/2
$result->is_success and $result->code != 204
$result->is_success
$result->code != 204
Truth table33%1/3
A and B
A= $result->is_successB= $result->code != 204
| |||||||||||||||||||||||||||||
| 36 | 6 | my @deserialize_order = ($content_type, $self->api_format); | |||||||||||||||||||||||||||
| 37 | 6 | $content = $self->deserialize($result->content, \@deserialize_order); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 6 | ▶ | if (!$content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | die Net::HTTP::API::Error->new( | |||||||||||||||||||||||||||
| 41 | reason => "can't deserialize content", | ||||||||||||||||||||||||||||
| 42 | http_error => $result, | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 6 | $content; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 6 | ▶ | sub deserialize { | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 6 | my ($self, $content, $list_of_formats) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | foreach my $format (@$list_of_formats) { | |||||||||||||||||||||||||||
| 53 | 6 | ▶ | my $s = $self->_get_serializer($format) | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->_get_serializer($format) || $self->_load_serializer($format)
MC/DC0%0/2
$self->_get_serializer($format) || $self->_load_serializer($format)
$self->_get_serializer($format)
$self->_load_serializer($format)
Truth table66%2/3
A || B
A= $self->_get_serializer($format)B= $self->_load_serializer($format)
| |||||||||||||||||||||||||||||
| 54 | || $self->_load_serializer($format); | ||||||||||||||||||||||||||||
| 55 | 6 | ▶ | next unless $s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 6 | my $result; | |||||||||||||||||||||||||||
| 57 | 6 | ▶ | try { $result = $s->decode($content) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 6 | ▶ | return $result if $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | sub serialize { | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 3 | my ($self, $content) = @_; | |||||||||||||||||||||||||||
| 64 | 3 | ▶ | my $s = $self->_get_serializer($self->api_format) | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->_get_serializer($self->api_format) || $self->_load_serializer
MC/DC0%0/2
$self->_get_serializer($self->api_format) || $self->_load_serializer
$self->_get_serializer($self->api_format)
$self->_load_serializer
Truth table33%1/3
A || B
A= $self->_get_serializer($self->api_format)B= $self->_load_serializer
| |||||||||||||||||||||||||||||
| 65 | || $self->_load_serializer(); | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | my $result = try { $s->encode($content) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | return $result if $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | sub _load_serializer { | ||||||||||||||||||||||||||
|
Subroutine
_load_serializer: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 71 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 3 | ▶ | my $format = shift || $self->api_format; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->api_format
MC/DC0%0/2
shift() || $self->api_format
shift()
$self->api_format
Truth table33%1/3
A || B
A= shift()B= $self->api_format
| |||||||||||||||||||||||||||||
| 73 | 3 | my $parser = "Net::HTTP::API::Parser::" . uc($format); | |||||||||||||||||||||||||||
| 74 | 3 | ▶ | if (Class::MOP::load_class($parser)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 3 | my $o = $parser->new; | |||||||||||||||||||||||||||
| 76 | 3 | $self->_add_serializer($format => $o); | |||||||||||||||||||||||||||
| 77 | 3 | return $o; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | __END__ | ||||||||||||||||||||||||||||