File Coverage

lib/WebService/Shippo/Addresses.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 22 100.0


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