File Coverage

lib/WebService/Shippo/Address.pm
Criterion Covered Total %
statement 25 33 75.7
branch 0 8 0.0
condition 0 6 0.0
subroutine 9 10 90.0
pod 0 1 0.0
total 34 58 58.6


line stmt bran cond sub pod time code
1 7     7   33 use strict;
  7         14  
  7         173  
2 7     7   33 use warnings;
  7         14  
  7         196  
3 7     7   33 use MRO::Compat 'c3';
  7         13  
  7         266  
4              
5             package WebService::Shippo::Address;
6             require WebService::Shippo::Request;
7 7     7   41 use Carp ( 'confess' );
  7         14  
  7         335  
8 7     7   34 use Params::Callbacks ( 'callbacks' );
  7         11  
  7         275  
9 7     7   36 use Scalar::Util ( 'blessed' );
  7         12  
  7         333  
10 7         2226 use base qw(
11             WebService::Shippo::Resource
12             WebService::Shippo::Create
13             WebService::Shippo::Fetch
14 7     7   33 );
  7         13  
15              
16             sub api_resource () { 'addresses' }
17              
18             sub collection_class () { 'WebService::Shippo::Addresses' }
19              
20             sub item_class () { __PACKAGE__ }
21              
22             sub validate
23             {
24 0     0 0   my ( $callbacks, $invocant, $object_id, @params ) = &callbacks;
25 0 0         if ( blessed( $invocant ) ) {
26 0 0         $object_id = $invocant->id
27             unless $object_id;
28             # Don't make unnecessary call to API if address was already validated
29 0 0 0       return $invocant
      0        
30             if $invocant->id eq $object_id
31             && $invocant->source eq 'VALIDATOR'
32             && $invocant->state eq 'VALID';
33             }
34 0 0         confess 'Expected an object id'
35             unless $object_id;
36 0           my $url = $invocant->url( "$object_id/validate" );
37 0           my $response = WebService::Shippo::Request->get( $url, @params );
38 0           return $invocant->construct_from( $response, $callbacks );
39             }
40              
41             BEGIN {
42 7     7   38 no warnings 'once';
  7         16  
  7         269  
43 7     7   372 *Shippo::Address:: = *WebService::Shippo::Address::;
44             }
45              
46             1;
47              
48             =pod
49              
50             =encoding utf8
51              
52             =head1 NAME
53              
54             WebService::Shippo::Address - Address class
55              
56             =head1 VERSION
57              
58             version 0.0.20
59              
60             =head1 SYNOPIS
61              
62             use WebService::Shippo;
63            
64             # If your client doesn't use a configuration file and you haven't set the
65             # SHIPPO_TOKEN environment variable, you should provide authentication
66             # details below.
67              
68             Shippo->api_key( 'PASTE YOUR PRIVATE AUTH TOKEN HERE' )
69             unless Shippo->api_key;
70            
71             # Create a new address object.
72            
73             $address = Shippo::Address->create(
74             object_purpose => 'PURCHASE',
75             name => 'John Smith',
76             street1 => '6512 Greene Rd.',
77             street2 => '',
78             company => 'Initech',
79             phone => '+1 234 346 7333',
80             city => 'Woodridge',
81             state => 'IL',
82             zip => '60517',
83             country => 'US',
84             email => 'user@gmail.com',
85             metadata => 'Customer ID 123456'
86             );
87            
88             # Serialize the address object as JSON.
89            
90             print $address->to_json; # Not Pretty
91             print $address->to_string; # Pretty
92             print $address; # Pretty and automatically stringified
93              
94             # Fetch an address object using its object_id.
95            
96             $object_id = $address->object_id;
97             $address = Shippo::Address->fetch($object_id);
98              
99             # Validate an address object (returns a new validated address object).
100            
101             $address = $address->validate;
102              
103             # Validate an address object using its object_id.
104            
105             $address = Shippo::Address->validate($object_id);
106            
107             # Get all my address objects. Particularly with a collection as
108             # potentially large as for addresses, "all" is actually a bit of a
109             # fib. The maximum size for any cursor is 200 items, so the "all"
110             # method will return a collection containing 200 or fewer objects.
111            
112             $collection = Shippo::Address->all;
113             for $address ( $collection->results ) {
114             print $address;
115             }
116            
117             # To cover the entire collection, you could use two loops and combine
118             # the "all" with the "next_page" methods. This example lists only the
119             # addresses that were validated and used for a transaction.
120            
121             $collection = Shippo::Address->all;
122             while ( $collection ) {
123             for $address ( $collection->results ) {
124             next unless $address->object_source eq 'VALIDATOR'
125             && $address->object_state eq 'VALID';
126             next unless $address->object_purpose eq 'PURCHASE';
127             print $address;
128             }
129             $collection = $collection->next_page;
130             }
131            
132             # Or, you can dispense with the potentially ugly pagination and
133             # use a single loop and a simple iterator. Again, we place the
134             # filter code inside the loop, which does the job well enough.
135            
136             $next_address = Shippo::Address->iterate();
137             while ( my ( $address ) = $next_address->() ) {
138             next unless $address->object_source eq 'VALIDATOR'
139             && $address->object_state eq 'VALID';
140             next unless $address->object_purpose eq 'PURCHASE';
141             print $address;
142             }
143            
144             # Or, single loop again with a named and specialised iterator. The filter
145             # code is a sequence of lambda functions called by the iterator. Suitable
146             # objects pass through the filter sequence while others are discarded. In
147             # this case the lambda sequence could be expressed as a single callback,
148             # but has been presented as a sequence for illustrative purposes.
149            
150             my $next_validated_purchase_address = Shippo::Address->iterate(
151             callback {
152             return unless $_[0]->object_source eq 'VALIDATOR'
153             && $_[0]->object_state eq 'VALID';
154             return $_[0];
155             }
156             callback {
157             return unless $_[0]->object_purpose eq 'PURCHASE';
158             return $_[0];
159             }
160             );
161            
162             while ( my ( $address ) = $next_validated_purchase_address->() ) {
163             print $address;
164             }
165              
166             # Or, single loop again with a named and specialised collector. The
167             # collector uses an iterator to gather the result set, returning the
168             # entire set once it is complete. The filter code is a sequence of
169             # lambda functions called by the collector. Suitable objects pass
170             # through the filter while others are discarded.
171            
172             my $all_validated_purchase_addresses = Shippo::Address->collect(
173             callback {
174             return unless $_[0]->object_source eq 'VALIDATOR'
175             && $_[0]->object_state eq 'VALID'
176             && $_[0]->object_purpose eq 'PURCHASE';
177             return $_[0];
178             }
179             );
180              
181             for my $address ( $all_validated_purchase_addresses->() ) {
182             print $address;
183             }
184            
185             =head1 DESCRIPTION
186              
187             Address objects are used for creating Shipments, obtaining Rates and printing
188             Labels, and thus are one of the fundamental building blocks of the Shippo
189             API.
190              
191             =head1 SEE ALSO
192              
193             =over 2
194              
195             =item * L
196              
197             =item * L
198              
199             =back
200              
201             =head1 API DOCUMENTATION
202              
203             For more information about Addresses, consult the Shippo API documentation:
204              
205             =over 2
206              
207             =item * L
208              
209             =back
210              
211             =head1 REPOSITORY
212              
213             =over 2
214              
215             =item * L
216              
217             =back
218              
219             =head1 AUTHOR
220              
221             Iain Campbell
222              
223             =head1 COPYRIGHT AND LICENSE
224              
225             This software is copyright (c) 2015 by Iain Campbell.
226              
227             You may distribute this software under the terms of either the GNU General
228             Public License or the Artistic License, as specified in the Perl README
229             file.
230              
231             =cut