File Coverage

blib/lib/Shipment/Generic.pm
Criterion Covered Total %
statement 10 13 76.9
branch n/a
condition n/a
subroutine 4 5 80.0
pod n/a
total 14 18 77.7


line stmt bran cond sub pod time code
1             package Shipment::Generic;
2             $Shipment::Generic::VERSION = '2.00';
3 1     1   24330 use strict;
  1         2  
  1         53  
4 1     1   7 use warnings;
  1         1  
  1         41  
5              
6              
7 1     1   739 use Moo;
  1         18821  
  1         9  
8             extends 'Shipment::Base';
9              
10              
11             sub _build_services {
12 0     0     { generic => Shipment::Service->new(
13             id => 'generic',
14             name => 'Generic Service',
15             ),
16             };
17             }
18              
19              
20             sub rate {
21             my ($self, $service_id, $rate) = @_;
22              
23             $service_id ||= 'generic';
24             $rate ||= 0;
25              
26 1     1   3562 use Data::Currency;
  0            
  0            
27             use Shipment::Service;
28             $self->service(
29             new Shipment::Service(
30             id => $service_id,
31             name => $service_id,
32             cost => Data::Currency->new($rate, $self->currency),
33             )
34             );
35              
36             }
37              
38              
39             sub ship {
40             my ($self, $service_id, $tracking_id, $rate) = @_;
41              
42             $self->rate($service_id, $rate);
43              
44             if (!$tracking_id) {
45             foreach (@{$self->packages}) {
46             $tracking_id = $_->tracking_id if $_->tracking_id;
47             last if $_->tracking_id;
48             }
49             }
50              
51             $tracking_id ||= 'n/a';
52              
53             $self->tracking_id($tracking_id);
54              
55             use Shipment::Label;
56              
57             foreach (@{$self->packages}) {
58             $_->tracking_id($tracking_id) if !$_->tracking_id;
59             $_->label(
60             Shipment::Label->new(
61             { tracking_id => $_->tracking_id,
62             content_type => 'text/plain',
63             data => qq|
64             FROM:
65             | . $self->from_address->name . qq|
66             | . $self->from_address->company . qq|
67             | . $self->from_address->address1 . qq|
68             | . $self->from_address->address2 . qq|
69             |
70             . $self->from_address->city . qq|, |
71             . $self->from_address->province_code . qq| |
72             . $self->from_address->postal_code . qq|
73             | . $self->from_address->country_code . qq|
74             | . $self->from_address->phone . qq|
75              
76             TO:
77             | . $self->to_address->name . qq|
78             | . $self->to_address->company . qq|
79             | . $self->to_address->address1 . qq|
80             | . $self->to_address->address2 . qq|
81             |
82             . $self->to_address->city . qq|, |
83             . $self->to_address->province_code . qq| |
84             . $self->to_address->postal_code . qq|
85             | . $self->to_address->country_code . qq|
86             | . $self->to_address->phone . qq|
87             |,
88             file_name => $_->tracking_id . '.txt',
89             },
90             )
91             );
92             }
93              
94             }
95              
96              
97             sub cancel {
98             my $self = shift;
99              
100             return 'success';
101             }
102              
103              
104             1;
105              
106             __END__
107              
108             =pod
109              
110             =encoding UTF-8
111              
112             =head1 NAME
113              
114             Shipment::Generic
115              
116             =head1 VERSION
117              
118             version 2.00
119              
120             =head1 SYNOPSIS
121              
122             This module does not DO a whole lot,
123              
124             use Shipment::Generic;
125             use Shipment::Address;
126             use Shipment::Package;
127              
128             my $shipment = Shipment::Generic->new(
129             from_address => Shipment::Address->new( ... ),
130             to_address => Shipment::Address->new( ... ),
131             packages => [ Shipment::Package->new( ... ), ],
132             );
133              
134             $shipment->rate( 'ground' );
135              
136             $shipment->ship( 'ground' );
137              
138             =head1 NAME
139              
140             Shipment::Generic - a very Generic shipping interface
141              
142             =head1 ABOUT
143              
144             This class provides a Generic interface for a shipping service.
145              
146             =head1 Class Attributes
147              
148             This class does not override or provide any additional attributes.
149              
150             =head1 Class Methods
151              
152             =head2 _build_services
153              
154             This routine simply adds a single "generic" service
155              
156             =head2 rate
157              
158             This routine populates $self->service
159              
160             It optionally accepts the cost passed as the second argument
161              
162             $shipment->rate( 'ground', '12.34 );
163             print $shipment->service->cost . "\n";
164              
165             =head2 ship
166              
167             This routine sets $self->tracking_id and calls $self->rate
168             Also sets a label for each package
169              
170             $shipment->ship( 'ground', '123456789', '12.32' );
171              
172             TODO: create generic thermal, pdf, and png labels so that we can do:
173             $shipment->get_package(0)->label->save;
174              
175             =head2 cancel
176              
177             This routine really does nothing but return 'success'
178              
179             $shipment->tracking_id( '12345' );
180             $shipment->cancel;
181              
182             =head1 AUTHOR
183              
184             Andrew Baerg @ <andrew at pullingshots dot ca>
185              
186             http://pullingshots.ca/
187              
188             =head1 BUGS
189              
190             Please contact me directly.
191              
192             =head1 COPYRIGHT
193              
194             Copyright (C) 2010 Andrew J Baerg, All Rights Reserved
195              
196             =head1 NO WARRANTY
197              
198             Absolutely, positively NO WARRANTY, neither express or implied, is
199             offered with this software. You use this software at your own risk. In
200             case of loss, no person or entity owes you anything whatsoever. You
201             have been warned.
202              
203             =head1 LICENSE
204              
205             This program is free software; you can redistribute it and/or modify it
206             under the same terms as Perl itself.
207              
208             =head1 AUTHOR
209              
210             Andrew Baerg <baergaj@cpan.org>
211              
212             =head1 COPYRIGHT AND LICENSE
213              
214             This software is copyright (c) 2013 by Andrew Baerg.
215              
216             This is free software; you can redistribute it and/or modify it under
217             the same terms as the Perl 5 programming language system itself.
218              
219             =cut