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   39 use strict;
  7         17  
  7         194  
2 7     7   37 use warnings;
  7         11  
  7         200  
3 7     7   33 use MRO::Compat 'c3';
  7         15  
  7         285  
4              
5             package WebService::Shippo::Address;
6             require WebService::Shippo::Request;
7 7     7   38 use Carp ( 'confess' );
  7         12  
  7         387  
8 7     7   38 use Params::Callbacks ( 'callbacks' );
  7         15  
  7         339  
9 7     7   38 use Scalar::Util ( 'blessed' );
  7         16  
  7         371  
10 7         2440 use base qw(
11             WebService::Shippo::Resource
12             WebService::Shippo::Create
13             WebService::Shippo::Fetch
14 7     7   37 );
  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   44 no warnings 'once';
  7         16  
  7         262  
43 7     7   395 *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.19
59              
60             =head1 SYNOPIS
61              
62             B: Though you must C when importing the
63             Shippo API wrapper, you may freely discard the leading C
64             portion of the namespace when subsequently referring to classes that
65             make up this distribution. You will see this practice being employed
66             consistently in example code.
67              
68             # This file can be found in the distribution:
69             # bin/synopses/addresses.pl
70             #
71             use strict;
72             use WebService::Shippo;
73             use Test::More;
74            
75             # If your client doesn't use a configuration file and you haven't set the
76             # SHIPPO_TOKEN environment variable, you should provide authentication
77             # details below:
78             #
79             Shippo->api_key( 'PASTE YOUR PRIVATE AUTH TOKEN HERE' )
80             unless Shippo->api_key;
81            
82             diag 'Create an address';
83             my $address = Shippo::Address->create(
84             object_purpose => 'PURCHASE',
85             name => 'John Smith',
86             street1 => '6512 Greene Rd.',
87             street2 => '',
88             company => 'Initech',
89             phone => '+1 234 346 7333',
90             city => 'Woodridge',
91             state => 'IL',
92             zip => '60517',
93             country => 'US',
94             email => 'user@gmail.com',
95             metadata => 'Customer ID 123456'
96             );
97             diag $address->to_string;
98            
99             my $id = $address->object_id;
100             diag "Fetch an address object identified by its object_id ($id)";
101             $address = Shippo::Address->fetch( $id );
102             diag $address->to_string;
103            
104             diag 'Validate the address object';
105             my $val_1 = $address->validate;
106             diag $val_1->to_string;
107            
108             diag "Validate an address object identified by its object_id ($id)";
109             my $val_2 = Shippo::Address->validate( $id );
110             diag $val_2->to_string;
111            
112             diag "Get 'all' (but really the first 200 or less) address objects";
113             {
114             my $collection = Shippo::Addresses->all;
115             for my $address ( $collection->results ) {
116             print "$address->{object_id}\n";
117             }
118             }
119            
120             diag "Really get all address objects";
121             {
122             my $collection = Shippo::Addresses->all;
123             while ( $collection ) {
124             for my $address ( $collection->results ) {
125             print "$address->{object_id}\n";
126             }
127             $collection = $collection->next_page;
128             }
129             }
130            
131             diag "Simple iteration through entire object collection";
132             {
133             my $it = Shippo::Address->iterator();
134             while ( my ( $address ) = $it->() ) {
135             print "$address->{object_id}\n";
136             }
137             }
138            
139             diag "Specialised iterator using a sequence of lambda functions as a filter";
140             {
141             my $next_validated_purchase_address = Shippo::Address->iterator(
142             callback {
143             my ( $address ) = @_;
144             # Discard address unless validated and valid
145             return
146             unless $address->object_source eq 'VALIDATOR'
147             && $address->object_state eq 'VALID';
148             # Else, keep address
149             return $address;
150             }
151             callback {
152             my ( $address ) = @_;
153             # Discard address unless created for transaction
154             return
155             unless $address->object_purpose eq 'PURCHASE';
156             # Else, keep address
157             return $address;
158             }
159             );
160            
161             while ( my ( $address ) = $next_validated_purchase_address->() ) {
162             print $address; # Automatically stringified using "to_string";
163             }
164             }
165            
166             diag "Collector using a sequence of lambda functions as a filter";
167             {
168             my $all_validated_purchase_addresses = Shippo::Address->collector(
169             callback {
170             my ( $address ) = @_;
171             # Discard address unless validated and valid
172             return
173             unless $address->object_source eq 'VALIDATOR'
174             && $address->object_state eq 'VALID';
175             # Else, keep address
176             return $address;
177             }
178             callback {
179             my ( $address ) = @_;
180             # Discard address unless created for transaction
181             return
182             unless $address->object_purpose eq 'PURCHASE';
183             # Else, keep address
184             return $address;
185             }
186             );
187            
188             for my $address ( $all_validated_purchase_addresses->() ) {
189             print $address;
190             }
191             }
192            
193             =head1 DESCRIPTION
194              
195             Address objects are used for creating Shipments, obtaining Rates and printing
196             Labels, and thus are one of the fundamental building blocks of the Shippo
197             API.
198              
199             =head1 API DOCUMENTATION
200              
201             For more information about Addresses, consult the Shippo API documentation:
202              
203             =over 2
204              
205             =item * L
206              
207             =back
208              
209             =head1 REPOSITORY
210              
211             =over 2
212              
213             =item * L
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              
232             =cut