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