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   57389 use Moo;
  4         59622  
  4         21  
5 4     4   7304 use Types::Standard qw( Object );
  4         265424  
  4         50  
6              
7 4     4   3093 use Carp;
  4         9  
  4         313  
8 4     4   20 use Module::Runtime;
  4         6  
  4         35  
9 4     4   2696 use Params::Util 0.14 ();
  4         10718  
  4         119  
10 4     4   4833 use Try::Tiny;
  4         4821  
  4         2837  
11              
12             our $VERSION = '0.003';
13              
14             has driver => (
15             is => 'ro',
16             isa => Object,
17             handles => [qw( id uri )],
18             );
19              
20             sub BUILDARGS {
21 29     29 0 21449 my ( $class, $driver_class, $id, @args ) = @_;
22              
23 29 100 100     266 unless ( defined $driver_class && !ref $driver_class && length $driver_class ) {
      100        
24 6         919 Carp::croak("Did not provide a Parcel::Track driver name");
25             }
26 23 100       691 unless ( Params::Util::_CLASS($driver_class) ) {
27 5         754 Carp::croak("Not a valid Parcel::Track driver name");
28             }
29              
30 18 100 100     310 unless ( defined $id && !ref $id && length $id ) {
      100        
31 6         1066 Carp::croak("Did not provide a Parcel::Track tracking number");
32             }
33              
34 12         17 my $driver_name = $driver_class;
35 12         31 $driver_class = "Parcel::Track::$driver_class";
36             try {
37 12     12   513 Module::Runtime::require_module($driver_class);
38             }
39             catch {
40 4 100   4   3082 if (m/^Can't locate /) {
41             # Driver does not exist
42 2         249 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         311 Carp::croak($_);
48             }
49 12         115 };
50 8 100       2472 unless ( $driver_class->can('new') ) {
51 1         192 Carp::croak("$driver_class does not have new method");
52             }
53 7         34 my $driver = $driver_class->new(
54             id => $id,
55             $class->_PRIVATE(@args),
56             );
57 6 100 66     220 unless ( $driver->can('does') && $driver->does('Parcel::Track::Role::Base') ) {
58 1         172 Carp::croak("$driver_class does not have Parcel::Track::Role::Base role");
59             }
60              
61 5         245 return +{ driver => $driver };
62             }
63              
64             sub track {
65 2     2 1 5 my $self = shift;
66              
67 2         11 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       10 Carp::croak("Driver returned an invalid \$result->{from}")
71             unless Params::Util::_SCALAR0( \$rv->{from} );
72 2 50       8 Carp::croak("Driver returned an invalid \$result->{to}")
73             unless Params::Util::_SCALAR0( \$rv->{to} );
74 2 50       8 Carp::croak("Driver returned an invalid \$result->{result}")
75             unless Params::Util::_SCALAR0( \$rv->{result} );
76 2 50       8 Carp::croak("Driver returned an invalid \$result->{descs}")
77             unless Params::Util::_ARRAY0( $rv->{descs} );
78 2 50       33 Carp::croak("Driver returned an invalid \$result->{htmls}")
79             unless Params::Util::_ARRAY0( $rv->{htmls} );
80              
81 2         7 return $rv;
82             }
83              
84             # Filter params for only the private params
85             sub _PRIVATE {
86 7 50   7   29 my $class = ref $_[0] ? ref shift : shift;
87 7         13 my @input = @_;
88 7         9 my @output = ();
89 7         23 while (@input) {
90 2         3 my $key = shift @input;
91 2         2 my $value = shift @input;
92 2 50 33     17 if ( Params::Util::_STRING($key) and $key =~ /^_/ ) {
93 2         5 $key =~ s/^_//;
94 2         5 push @output, $key, $value;
95             }
96             }
97 7         42 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__