File Coverage

blib/lib/Parcel/Track.pm
Criterion Covered Total %
statement 57 57 100.0
branch 20 28 71.4
condition 15 18 83.3
subroutine 11 11 100.0
pod 1 2 50.0
total 104 116 89.6


line stmt bran cond sub pod time code
1             package Parcel::Track;
2             # ABSTRACT: Driver-based API for tracking parcel
3              
4 4     4   63174 use Moo;
  4         59928  
  4         23  
5 4     4   8528 use Types::Standard qw( Object );
  4         254069  
  4         51  
6              
7 4     4   2724 use Carp;
  4         7  
  4         332  
8 4     4   22 use Module::Runtime;
  4         6  
  4         37  
9 4     4   2556 use Params::Util 0.14 ();
  4         9329  
  4         134  
10 4     4   2121 use Try::Tiny;
  4         4083  
  4         2458  
11              
12             our $VERSION = '0.005';
13              
14             has driver => (
15             is => 'ro',
16             isa => Object,
17             handles => [qw( id uri )],
18             );
19              
20             sub BUILDARGS {
21 29     29 0 14358 my ( $class, $driver_class, $id, @args ) = @_;
22              
23 29 100 100     207 unless ( defined $driver_class && !ref $driver_class && length $driver_class ) {
      100        
24 6         775 Carp::croak("Did not provide a Parcel::Track driver name");
25             }
26 23 100       479 unless ( Params::Util::_CLASS($driver_class) ) {
27 5         777 Carp::croak("Not a valid Parcel::Track driver name");
28             }
29              
30 18 100 100     320 unless ( defined $id && !ref $id && length $id ) {
      100        
31 6         836 Carp::croak("Did not provide a Parcel::Track tracking number");
32             }
33              
34 12         17 my $driver_name = $driver_class;
35 12         29 $driver_class = "Parcel::Track::$driver_class";
36             try {
37 12     12   418 Module::Runtime::require_module($driver_class);
38             }
39             catch {
40 4 100   4   2764 if (m/^Can't locate /) {
41             # Driver does not exist
42 2         364 Carp::croak("Parcel::Track driver $driver_name does not exist, or is not installed");
43             }
44             else {
45             # Fatal error within the driver itself
46             # Pass on without change
47 2         243 Carp::croak($_);
48             }
49 12         87 };
50 8 100       2244 unless ( $driver_class->can('new') ) {
51 1         111 Carp::croak("$driver_class does not have new method");
52             }
53 7         30 my $driver = $driver_class->new(
54             id => $id,
55             $class->_PRIVATE(@args),
56             );
57 6 100 66     176 unless ( $driver->can('does') && $driver->does('Parcel::Track::Role::Base') ) {
58 1         133 Carp::croak("$driver_class does not have Parcel::Track::Role::Base role");
59             }
60              
61 5         197 return +{ driver => $driver };
62             }
63              
64             sub track {
65 2     2 1 3 my $self = shift;
66              
67 2         10 my $rv = $self->driver->track;
68 2 50       10 Carp::croak("Driver did not return a result")
69             unless Params::Util::_HASH($rv);
70 2 50       9 Carp::croak("Driver returned an invalid \$result->{from}")
71             unless Params::Util::_SCALAR0( \$rv->{from} );
72 2 50       6 Carp::croak("Driver returned an invalid \$result->{to}")
73             unless Params::Util::_SCALAR0( \$rv->{to} );
74 2 50       6 Carp::croak("Driver returned an invalid \$result->{result}")
75             unless Params::Util::_SCALAR0( \$rv->{result} );
76 2 50       13 Carp::croak("Driver returned an invalid \$result->{descs}")
77             unless Params::Util::_ARRAY0( $rv->{descs} );
78 2 50       6 Carp::croak("Driver returned an invalid \$result->{htmls}")
79             unless Params::Util::_ARRAY0( $rv->{htmls} );
80              
81 2         3 return $rv;
82             }
83              
84             # Filter params for only the private params
85             sub _PRIVATE {
86 7 50   7   21 my $class = ref $_[0] ? ref shift : shift;
87 7         11 my @input = @_;
88 7         12 my @output = ();
89 7         19 while (@input) {
90 2         3 my $key = shift @input;
91 2         3 my $value = shift @input;
92 2 50 33     17 if ( Params::Util::_STRING($key) and $key =~ /^_/ ) {
93 2         5 $key =~ s/^_//;
94 2         6 push @output, $key, $value;
95             }
96             }
97 7         76 return @output;
98             }
99              
100             1;
101              
102             #
103             # This file is part of Parcel-Track
104             #
105             # This software is copyright (c) 2015 by Keedi Kim.
106             #
107             # This is free software; you can redistribute it and/or modify it under
108             # the same terms as the Perl 5 programming language system itself.
109             #
110              
111             __END__