File Coverage

lib/WebService/Shippo/Object.pm
Criterion Covered Total %
statement 34 94 36.1
branch 0 30 0.0
condition 0 15 0.0
subroutine 12 23 52.1
pod 0 9 0.0
total 46 171 26.9


line stmt bran cond sub pod time code
1 7     7   31 use strict;
  7         13  
  7         168  
2 7     7   34 use warnings;
  7         12  
  7         173  
3 7     7   5112 use MRO::Compat 'c3';
  7         22413  
  7         268  
4              
5             package WebService::Shippo::Object;
6 7     7   44 use Carp ( 'croak' );
  7         11  
  7         365  
7 7     7   7093 use JSON::XS ();
  7         41215  
  7         195  
8 7     7   49 use Params::Callbacks ( 'callbacks' );
  7         12  
  7         370  
9 7     7   40 use Scalar::Util ( 'blessed', 'reftype' );
  7         14  
  7         362  
10 7     7   35 use Sub::Util ( 'set_subname' );
  7         14  
  7         321  
11 7     7   33 use overload ( fallback => 1, '""' => 'to_string' );
  7         13  
  7         44  
12              
13             our $AUTOLOAD;
14              
15             sub class
16             {
17 0     0 0   my ( $invocant ) = @_;
18 0   0       return ref( $invocant ) || $invocant;
19             }
20              
21             sub new
22             {
23 0     0 0   my ( $invocant, $id ) = @_;
24 0           my $self = bless {}, $invocant->class;
25             $id = $id->{object_id}
26 0 0 0       if ref( $id ) && reftype( $id ) eq 'HASH';
27 0 0         $self->{object_id} = $id
28             if $id;
29 0           return $self;
30             }
31              
32             {
33             my $json = JSON::XS->new->utf8->convert_blessed->allow_blessed;
34              
35             sub construct_from
36             {
37 0     0 0   my ( $callbacks, $invocant, $response ) = &callbacks;
38 0           my $ref_type = ref( $response );
39 0 0         return $ref_type
40             unless defined $ref_type;
41 0 0         if ( $ref_type eq 'HASH' ) {
42 0           my $invocant = $invocant->new( $response->{object_id} );
43 0           $invocant->refresh_from( $response );
44 0 0 0       if ( exists( $invocant->{count} )
45             && exists( $invocant->{results} ) )
46             {
47 0           my $item_class = $invocant->item_class;
48             $invocant->{results}
49 0           = [ map { $callbacks->smart_transform( bless( $_, $item_class ) ) }
50 0           @{ $invocant->{results} } ];
  0            
51 0           return bless( $invocant, $invocant->collection_class );
52             }
53             else {
54 0           return $callbacks->smart_transform( $invocant );
55             }
56             }
57             else {
58 0 0         croak $response->status_line
59             unless $response->is_success;
60 0           my $content = $response->decoded_content;
61 0           my $hash = $json->decode( $content );
62 0           return $invocant->construct_from( $hash, $callbacks );
63             }
64             }
65             }
66              
67             sub refresh_from
68             {
69 0     0 0   my ( $invocant, $hash ) = @_;
70 0           @{$invocant}{ keys %$hash } = values %$hash;
  0            
71 0           return $invocant;
72             }
73              
74             sub refresh
75             {
76 0     0 0   my ( $invocant ) = @_;
77 0           my $url = $invocant->url( $invocant->{object_id} );
78 0           my $response = Shippo::Request->get( $url );
79 0           my $update = $invocant->construct_from( $response );
80 0           return $invocant->refresh_from( $update );
81             }
82              
83             sub is_same_object
84             {
85 0     0 0   my ( $invocant, $object_id ) = @_;
86             return
87 0 0         unless defined $object_id;
88             return
89 0 0         unless blessed( $invocant );
90             return
91 0 0         unless reftype( $invocant ) eq 'HASH';
92             return
93 0 0         unless exists $invocant->{object_id};
94 0           return $invocant->{object_id} eq $object_id;
95             }
96              
97             {
98             my $json = JSON::XS->new->utf8->canonical->convert_blessed->allow_blessed;
99              
100             $json->pretty( 0 );
101              
102             sub to_json
103             {
104 0     0 0   my ( $data ) = @_;
105 0           return $json->encode( $data );
106             }
107             }
108              
109             {
110             my $json = JSON::XS->new->utf8->canonical->convert_blessed->allow_blessed;
111              
112             $json->pretty( 1 );
113              
114             sub to_string
115             {
116 0     0 0   my ( $data ) = @_;
117 0           return $json->encode( $data );
118             }
119             }
120              
121             sub TO_JSON
122             {
123 0     0 0   return { %{ $_[0] } };
  0            
124             }
125              
126             # Just in time creation of mutators for orphaned method calls, to facilitate
127             # access to object attributes of the same name.
128             sub AUTOLOAD
129             {
130 0     0     my ( $invocant, @args ) = @_;
131 0   0       my $class = ref( $invocant ) || $invocant;
132 0           ( my $method = $AUTOLOAD ) =~ s{^.*\::}{};
133             return
134 0 0         if $method eq 'DESTROY';
135 7     7   5850 no strict 'refs';
  7         35  
  7         1471  
136 0           my $sym = "$class\::$method";
137             *$sym = set_subname(
138             $sym => sub {
139 0     0     my ( $invocant ) = @_;
140             return ''
141 0 0         unless defined $invocant->{$method};
142 0 0 0       if ( wantarray && ref( $invocant->{$method} ) ) {
143 0           return %{ $invocant->{$method} }
144 0 0         if reftype( $invocant->{$method} ) eq 'HASH';
145 0           return @{ $invocant->{$method} }
146 0 0         if reftype( $invocant->{$method} ) eq 'ARRAY';
147             }
148 0           return $invocant->{$method};
149             }
150 0           );
151 0           goto &$sym;
152             }
153              
154             BEGIN {
155 7     7   38 no warnings 'once';
  7         15  
  7         265  
156 7     7   283 *Shippo::Object:: = *WebService::Shippo::Object::;
157             }
158              
159             1;