Easypost/Shipment.pm

filter: Statementstmt 81.6% 49 / 60 Branchbran 66.6% 4 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 3 / 3 totaltotal 81.1% 69 / 85 SCAR 18.4
CC
6
Cov
80%
buy
7.2
clone
2.0
BEGIN
1.0
CRAP 6.3
« Easypost.pm ↑ Summary Easypost/Request.pm »
1 package Net::Easypost::Shipment;
2 6 $Net::Easypost::Shipment::VERSION = '0.20';
3 6 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Types::Standard qw/ArrayRef HashRef InstanceOf Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 with qw/Net::Easypost::PostOnBuild/;
8 6 with qw/Net::Easypost::Resource/;
9 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 has [qw/from_address to_address/] => (
12 is => 'ro',
13 isa => InstanceOf['Net::Easypost::Address'],
14 required => 1,
15 );
16  
17 6 has 'parcel' => (
18 is => 'rw',
19 isa => InstanceOf['Net::Easypost::Parcel'],
20 required => 1,
21 );
22  
23 6 has 'customs_info' => (
24 is => 'rw',
25 isa => InstanceOf['Net::Easypost::CustomsInfo'],
26 );
27  
28 6 has 'scan_form' => (
29 is => 'rw',
30 isa => InstanceOf['Net::Easypost::ScanForm'],
31 );
32  
33 6 has 'rates' => (
34 is => 'rwp',
35 isa => ArrayRef[InstanceOf['Net::Easypost::Rate']],
36 );
37  
38 6 has 'options' => (
39 is => 'rw',
40 isa => HashRef[Str],
41 );
42  
43 sub _build_fieldnames {
44 6 ▶ return [qw/to_address from_address parcel customs_info scan_form rates options/];
Subroutine _build_fieldnames: called CC=1 SCAR=0.0
45 }
46  
47 6 ▶sub _build_role { 'shipment' }
Subroutine _build_role: called CC=1 SCAR=0.0
48 6 ▶sub _build_operation { '/shipments' }
Subroutine _build_operation: called CC=1 SCAR=0.0
49  
50 sub BUILD {}
51 after BUILD => sub {
52 6 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 6 my $resp = $self->requester->post(
55 $self->operation,
56 $self->serialize,
57 );
58 6 $self->_set_id( $resp->{id} );
59 $self->_set_rates(
60 [ map {
61 Net::Easypost::Rate->new(
62 id => $_->{id},
63 carrier => $_->{carrier},
64 service => $_->{service},
65 rate => $_->{rate},
66 17 shipment_id => $self->id,
67 )
68 6 } @{ $resp->{rates} }
69 ]
70 );
71 6 };
72  
73 sub serialize {
74 12 ▶ my ($self) = @_;
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
75  
76 # want a hashref of e.g., shipment[to_address][id] => foo from all defined attributes
77 return {
78 38 map { $self->role . "[$_][id]" => $self->$_->id }
79 12 grep { defined $self->$_ }
80 qw(to_address from_address parcel customs_info)
81 };
82 }
83  
84 sub clone {
85 0 ▶ my ($self) = @_;
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
86  
87 return Net::Easypost::Shipment->new(
88 0 map { $_ => $self->$_ }
89 0 grep { defined $self->$_ }
90 0 'id', @{ $self->fieldnames }
91 );
92 }
93  
94 sub buy {
95 2 ▶ my ($self, %options) = @_;
Subroutine buy: called CC=6 SCAR=19.7
Pod documented
96  
97 2 my $rate;
98 2 ▶ if (exists $options{rate} && $options{rate} eq 'lowest') {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $options{'rate'} and $options{'rate'} eq 'lowest') { } 1 1 2
elsif (exists $options{'service_type'}) { } 1 0 1
Condition66%2/3
exists $options{"rate"} and $options{"rate"} eq "lowest"
!ll&&!rl&&r
101
MC/DC50%1/2
exists $options{"rate"} and $options{"rate"} eq "lowest"
exists $options{"rate"} $options{"rate"} eq "lowest"
Truth table66%2/3
A and B
A= exists $options{"rate"}B= $options{"rate"} eq "lowest"
ABresult
0X0
100
111
99 ($rate) =
100 1 sort { $a->{rate} <=> $b->{rate} } @{ $self->rates };
101 }
102 elsif (exists $options{service_type}) {
103 ($rate) =
104 1 grep { $options{service_type} eq $_->service } @{ $self->rates };
105 }
106 else {
107 0 croak "Missing 'service' or 'rate' from options hash";
108 }
109  
110 2 ▶ unless ($rate) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rate) 0 2 2
111 0 my $msg = "Allowed services and rates for this shipment are:\n";
112 0 foreach my $rate (@{ $self->rates }) {
113 0 $msg .= sprintf("\t%-15s: %4.2f\n", $rate->service, $rate->rate);
114 }
115  
116 0 croak "Invalid service '$options{service_type}' selected for shipment " . $self->id . "\n$msg";
117 }
118  
119 2 my $response = $self->requester->post(
120 $self->operation . '/' . $self->id . '/buy',
121 $rate->serialize
122 );
123  
124 2 my $label = $response->{postage_label};
125 return Net::Easypost::Label->new(
126 id => $label->{id},
127 tracking_code => $response->{tracking_code},
128 url => $label->{label_url},
129 filetype => $label->{label_file_type},
130 rate => Net::Easypost::Rate->new($response->{selected_rate}),
131 filename => 'EASYPOST_LABEL_'
132 . $label->{id}
133 . '.'
134 2 . substr($label->{label_file_type}, index($label->{label_file_type}, '/') + 1),
135 );
136 }
137  
138 6 1;
139  
140 __END__
« Easypost.pm ↑ Summary Easypost/Request.pm »