File Coverage

blib/lib/Role/REST/Client/Serializer.pm
Criterion Covered Total %
statement 34 38 89.4
branch 4 8 50.0
condition n/a
subroutine 11 13 84.6
pod 0 3 0.0
total 49 62 79.0


line stmt bran cond sub pod time code
1             package Role::REST::Client::Serializer;
2             $Role::REST::Client::Serializer::VERSION = '0.22';
3 4     4   7683 use Try::Tiny;
  4         1568  
  4         192  
4 4     4   461 use Moo;
  4         9301  
  4         28  
5 4     4   5401 use Types::Standard qw(Enum InstanceOf);
  4         55929  
  4         36  
6 4     4   4361 use Data::Serializer::Raw;
  4         2666  
  4         1127  
7              
8             has 'type' => (
9             isa => Enum[qw{application/json application/xml application/yaml application/x-www-form-urlencoded text/javascript}],
10             is => 'rw',
11             default => sub { 'application/json' },
12             );
13              
14             has 'serializer' => (
15             isa => InstanceOf['Data::Serializer::Raw'],
16             is => 'ro',
17             default => \&_set_serializer,
18             lazy => 1,
19             );
20              
21             our %modules = (
22             'application/json' => {
23             module => 'JSON',
24             },
25             'application/xml' => {
26             module => 'XML::Simple',
27             },
28             'application/yaml' => {
29             module => 'YAML',
30             },
31             'application/x-www-form-urlencoded' => {
32             module => 'FORM',
33             },
34             'text/javascript' => {
35             module => 'JSON',
36             },
37             );
38              
39             sub _set_serializer {
40 5     5   63 my $self = shift;
41 5 50       89 return unless $modules{$self->type};
42              
43 5         177 my $module = $modules{$self->type}{module};
44 5 50       67 return $module if $module eq 'FORM';
45              
46 5         57 return Data::Serializer::Raw->new(
47             serializer => $module,
48             );
49             }
50              
51             sub content_type {
52 4     4 0 377739 my ($self) = @_;
53 4         100 return $self->type;
54             }
55              
56             sub serialize {
57 7     7 0 2081 my ($self, $data) = @_;
58 7 50       159 return unless $self->serializer;
59              
60 7         16054 my $result;
61             try {
62 7     7   428 $result = $self->serializer->serialize($data)
63             } catch {
64 0     0   0 warn "Couldn't serialize data with " . $self->type . ": $_";
65 7         62 };
66              
67 7         11762 return $result;
68             }
69              
70             sub deserialize {
71 10     10 0 3143 my ($self, $data) = @_;
72 10 50       218 return unless $self->serializer;
73              
74 10         12423 my $result;
75             try {
76 10     10   675 $result = $self->serializer->deserialize($data);
77             } catch {
78 4     4   1997 use Data::Dumper 'Dumper';
  4         20750  
  4         438  
79 0     0   0 $Data::Dumper::Maxdepth = 4;
80 0         0 warn 'Data was ' . Dumper([ $data ]), ' ';
81 0         0 warn "Couldn't deserialize data with " . $self->type . ": $_";
82 10         90 };
83              
84 10         73213 return $result;
85             }
86              
87             1;
88              
89             __END__
90              
91             =pod
92              
93             =encoding UTF-8
94              
95             =head1 NAME
96              
97             Role::REST::Client::Serializer
98              
99             =head1 VERSION
100              
101             version 0.22
102              
103             =head1 AUTHOR
104              
105             Kaare Rasmussen <kaare at cpan dot org>
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is copyright (c) 2017 by Kaare Rasmussen.
110              
111             This is free software; you can redistribute it and/or modify it under
112             the same terms as the Perl 5 programming language system itself.
113              
114             =cut