File Coverage

blib/lib/PayProp/API/Public/Client/Role/JSON.pm
Criterion Covered Total %
statement 30 31 96.7
branch 9 10 90.0
condition 6 10 60.0
subroutine 4 4 100.0
pod 0 1 0.0
total 49 56 87.5


line stmt bran cond sub pod time code
1             package PayProp::API::Public::Client::Role::JSON;
2              
3 29     29   326853 use strict;
  29         65  
  29         1422  
4 29     29   173 use warnings;
  29         49  
  29         1363  
5              
6 29     29   2522 use Mouse::Role;
  29         6973  
  29         195  
7              
8             sub TO_JSON {
9 23     23 0 6383 my ( $self, $value, $structure ) = @_;
10              
11 23 100 33     60 $value //= $self unless $structure;
12 23   100     67 $structure //= {};
13              
14 23         47 my $ref_type = ref $value;
15              
16 23 100 66     94 if ( ! $ref_type || $ref_type =~ m/Bool/ ) {
    100          
    100          
    50          
17 14         40 return $value;
18             }
19             elsif ( $ref_type eq 'ARRAY' ) {
20 4         7 my @items;
21 4         8 foreach my $item ( $value->@* ) {
22 11         36 push @items, $self->TO_JSON( $item, $structure );
23             }
24 4         14 return \@items;
25             }
26             elsif ( $ref_type eq 'HASH' ) {
27 3         11 foreach my $key ( keys $value->%* ) {
28 4         20 $structure->{ $key } = $self->TO_JSON( $value->{ $key }, $structure );
29             }
30 3         10 return $structure;
31             }
32             elsif ( $ref_type =~ m/PayProp::API::Public::Client::Response/ ) {
33 2         10 foreach my $Attribute (
34 7         151 ( map { $value->meta->get_attribute( $_ ) } $value->meta->get_attribute_list )
35             ) {
36 7         40 my $key = $Attribute->name;
37              
38 7         22 my $reader = $Attribute->get_read_method;
39 7         54 my $item = $value->$reader;
40              
41 7   50     68 $structure->{ $key } //= {};
42 7         20 $structure->{ $key } = $self->TO_JSON( $item, $structure->{ $key } );
43             }
44             }
45             else {
46 0         0 die "Unhandled ref_type: $ref_type";
47             }
48              
49 2         21 return $structure;
50             }
51              
52             1;
53              
54             __END__