File Coverage

lib/WebService/Shippo/Shipment.pm
Criterion Covered Total %
statement 26 53 49.0
branch 0 16 0.0
condition 0 3 0.0
subroutine 9 11 81.8
pod 0 1 0.0
total 35 84 41.6


line stmt bran cond sub pod time code
1 7     7   32 use strict;
  7         14  
  7         166  
2 7     7   33 use warnings;
  7         12  
  7         168  
3 7     7   31 use MRO::Compat 'c3';
  7         8  
  7         226  
4              
5             package WebService::Shippo::Shipment;
6             require WebService::Shippo::Rate;
7 7     7   31 use Carp ( 'confess' );
  7         12  
  7         421  
8 7     7   33 use Params::Callbacks ( 'callbacks', 'callback' );
  7         10  
  7         426  
9 7     7   35 use Scalar::Util ( 'blessed' );
  7         11  
  7         450  
10 7         4820 use base qw(
11             WebService::Shippo::Resource
12             WebService::Shippo::Create
13             WebService::Shippo::Fetch
14             WebService::Shippo::Currency
15             WebService::Shippo::Async
16 7     7   50 );
  7         17  
17              
18             sub api_resource () { 'shipments' }
19              
20             sub collection_class () { 'WebService::Shippo::Shipments' }
21              
22             sub item_class () { __PACKAGE__ }
23              
24             sub get_shipping_rates
25             {
26 0     0 0   my ( $callbacks, $invocant, $shipment_id, @params ) = &callbacks;
27 0 0         confess "Expected a shipment id"
28             unless $shipment_id;
29 0           my $shipment;
30 0 0         if ( $invocant->is_same_object( $shipment_id ) ) {
31 0           $shipment = $invocant;
32             }
33             else {
34 0           $shipment = WebService::Shippo::Shipment->fetch( $shipment_id );
35             }
36 0           my $currency;
37 0 0 0       if ( @params && @params % 2 ) {
38 0           ( $currency, @params ) = @params;
39 0           $currency = $invocant->validate_currency( $currency );
40             }
41 0           my $rates_url = "$shipment_id/rates";
42 0 0         $rates_url .= "/$currency"
43             if $currency;
44 0           $rates_url = $invocant->url( $rates_url );
45 0           my $async;
46 0           my %params = @params;
47             $async = delete( $params{async} )
48 0 0         if exists $params{async};
49 0           @params = %params;
50              
51 0 0         unless ( $async ) {
52 0           WebService::Shippo::Request->get( $rates_url, @params );
53 0           $shipment->wait_if_status_in( 'QUEUED', 'WAITING' );
54             }
55 0           my $response = WebService::Shippo::Request->get( $rates_url, @params );
56             unshift @$callbacks, callback {
57 0 0   0     return unless @_;
58 0 0         return $_[0] unless defined $_[0];
59 0           return bless( $_[0], 'WebService::Shippo::Rate' );
60 0           };
61 0           my $rates = WebService::Shippo::Rate->construct_from( $response, $callbacks );
62 0           return $rates;
63             }
64              
65             BEGIN {
66 7     7   41 no warnings 'once';
  7         18  
  7         296  
67 7     7   16 *Shippo::Shipment:: = *WebService::Shippo::Shipment::;
68 7         257 *rates = *get_shipping_rates;
69             }
70              
71             1;
72              
73             =pod
74              
75             =encoding utf8
76              
77             =head1 NAME
78              
79             WebService::Shippo::Shipment - Shipment class
80              
81             =head1 VERSION
82              
83             version 0.0.19
84              
85             =head1 DESCRIPTION
86              
87             At the heart of the Shippo API is the Shipment object. It is made up
88             of sender and recipient addresses, details of the parcel to be shipped
89             and, for international shipments, the customs declaration. Once created,
90             a Shipment object can be used to retrieve shipping rates and purchase a
91             shipping label.
92              
93             =head1 API DOCUMENTATION
94              
95             For more information about Shipments, consult the Shippo API documentation:
96              
97             =over 2
98              
99             =item * L
100              
101             =back
102              
103             =head1 REPOSITORY
104              
105             =over 2
106              
107             =item * L
108              
109             =item * L
110              
111             =back
112              
113             =head1 AUTHOR
114              
115             Iain Campbell
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2015 by Iain Campbell.
120              
121             You may distribute this software under the terms of either the GNU General
122             Public License or the Artistic License, as specified in the Perl README
123             file.
124              
125              
126             =cut