Easypost/Shipment.pm

filter: Statementstmt 79.0% 49 / 62 Branchbran 62.5% 5 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 91.6% 11 / 12 Podpod 100% 4 / 4 totaltotal 79.1% 72 / 91 CC 7 SCAR 11.8
CC
7
Cov
77%
buy
7.2
serialize
2.2
clone
2.0
CRAP 3.3
↑ Summary Easypost/Request.pm »
1 package Net::Easypost::Shipment;
2 6 $Net::Easypost::Shipment::VERSION = '0.23';
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 6 ▶sub _build_fieldnames {
Subroutine _build_fieldnames: called CC=1 SCAR=0.0
44 6 return [qw/to_address from_address parcel customs_info scan_form rates options/];
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 6 ▶sub BUILD {}
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
51 6 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 6 my ($self) = @_;
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 12 ▶sub serialize {
Subroutine serialize: called CC=2 SCAR=7.9
Pod documented
74 12 my ($self) = @_;
75  
76 # want a hashref of e.g., shipment[to_address][id] => foo from all defined attributes
77 return {
78 (defined $self->options
79 0 ? map { $self->role . "[options][$_]" => $self->options->{$_} } %{$self->options}
80 : ()),
81 38 map { $self->role . "[$_][id]" => $self->$_->id }
82 12 ▶ grep { defined $self->$_ }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->options ? : 0 12 12
83 qw(to_address from_address parcel customs_info)
84 };
85 }
86  
87 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
88 0 my ($self) = @_;
89  
90 return Net::Easypost::Shipment->new(
91 0 map { $_ => $self->$_ }
92 0 grep { defined $self->$_ }
93 0 'id', @{ $self->fieldnames }
94 );
95 }
96  
97 2 ▶sub buy {
Subroutine buy: called CC=6 SCAR=19.7
Pod documented
98 2 my ($self, %options) = @_;
99  
100 2 my $rate;
101 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
102 ($rate) =
103 1 sort { $a->{rate} <=> $b->{rate} } @{ $self->rates };
104 }
105 elsif (exists $options{service_type}) {
106 ($rate) =
107 1 grep { $options{service_type} eq $_->service } @{ $self->rates };
108 }
109 else {
110 0 croak "Missing 'service' or 'rate' from options hash";
111 }
112  
113 2 ▶ unless ($rate) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rate) 0 2 2
114 0 my $msg = "Allowed services and rates for this shipment are:\n";
115 0 foreach my $rate (@{ $self->rates }) {
116 0 $msg .= sprintf("\t%-15s: %4.2f\n", $rate->service, $rate->rate);
117 }
118  
119 0 croak "Invalid service '$options{service_type}' selected for shipment " . $self->id . "\n$msg";
120 }
121  
122 2 my $response = $self->requester->post(
123 $self->operation . '/' . $self->id . '/buy',
124 $rate->serialize
125 );
126  
127 2 my $label = $response->{postage_label};
128 return Net::Easypost::Label->new(
129 id => $label->{id},
130 tracking_code => $response->{tracking_code},
131 url => $label->{label_url},
132 filetype => $label->{label_file_type},
133 rate => Net::Easypost::Rate->new($response->{selected_rate}),
134 filename => 'EASYPOST_LABEL_'
135 . $label->{id}
136 . '.'
137 2 . substr($label->{label_file_type}, index($label->{label_file_type}, '/') + 1),
138 );
139 }
140  
141 6 1;
142  
143 __END__
↑ Summary Easypost/Request.pm »