File Coverage

lib/WebService/Shippo/Config.pm
Criterion Covered Total %
statement 36 62 58.0
branch 5 18 27.7
condition 2 19 10.5
subroutine 12 13 92.3
pod 0 4 0.0
total 55 116 47.4


line stmt bran cond sub pod time code
1 7     7   35 use strict;
  7         13  
  7         188  
2 7     7   35 use warnings;
  7         14  
  7         311  
3              
4             package WebService::Shippo::Config;
5             require WebService::Shippo::Request;
6 7     7   37 use Cwd;
  7         10  
  7         476  
7 7     7   34 use Carp ( 'croak' );
  7         14  
  7         262  
8 7     7   5677 use File::HomeDir ();
  7         43032  
  7         147  
9 7     7   4979 use Path::Class ();
  7         233211  
  7         153  
10 7     7   4998 use YAML::XS ();
  7         20106  
  7         4081  
11              
12             #<<< PerlTidy, leave this:
13             ( my $temp = __FILE__ ) =~ s{\.\w+$}{.yml};
14              
15             our @SEARCH_PATH = (
16             Path::Class::Dir->new( getcwd )->file( '.shipporc' )->stringify,
17             Path::Class::Dir->new( File::HomeDir->my_home )->file( '.shipporc' )->stringify,
18             Path::Class::Dir->new( '', 'etc' )->file( 'shipporc' )->stringify,
19             $temp
20             );
21             #>>>
22              
23             {
24             my $value = undef;
25              
26             sub config_file
27             {
28 7     7 0 15 my ( $invocant, $new_value ) = @_;
29 7 50       43 unless ( $value ) {
30 7         22 for my $candidate ( @SEARCH_PATH ) {
31 28 50       604 if ( -e $candidate ) {
32 0         0 $value = $candidate;
33 0         0 last;
34             }
35             }
36             }
37 7 50       47 return $value unless @_ > 1;
38 0         0 $value = $new_value;
39 0         0 return $invocant;
40             }
41             }
42              
43             {
44             my $config = undef;
45              
46             sub reload_config
47             {
48 0     0 0 0 my ( $invocant ) = @_;
49 0         0 $invocant->load_config_file;
50 0         0 return $invocant;
51             }
52              
53             sub config
54             {
55 7     7 0 17 my ( $invocant, $new_value ) = @_;
56 7 50       42 return $config unless @_ > 1;
57 0   0     0 my $class = ref( $invocant ) || $invocant;
58 0         0 $config = $new_value;
59 0   0     0 my $default_token = $config->{default_token} || 'private_token';
60 0         0 my $api_key = $config->{$default_token};
61 0   0     0 my $user = $config->{username} || $config->{email};
62 0         0 my $pass = $config->{password};
63 0         0 Shippo::Resource->api_private_token( $config->{private_token} );
64 0         0 Shippo::Resource->api_public_token( $config->{public_token} );
65 0 0       0 Shippo::Resource->api_key( $api_key )
66             if $api_key;
67 0 0 0     0 Shippo::Resourse->api_credentials( $user, $pass )
68             if $user && !$api_key;
69 0         0 bless $config, $class;
70 0         0 return $invocant;
71             }
72              
73             sub load_config_file
74             {
75 7     7 0 19 my ( $invocant ) = @_;
76 7   33     69 my $class = ref( $invocant ) || $invocant;
77 7         26 my $config_file = $class->config_file;
78             # Return empty config if no config file exists
79 7 50 33     44 return bless( {}, $class )
80             unless $config_file && -e $config_file;
81             # Fetch the config content. By default, this should be defined in a
82             # file called Config.yml, located in the same folder as the Config.pm
83             # module.
84 0 0         open my $fh, '<:encoding(UTF-8)', $config_file
85             or croak "Can't open file '$config_file': $!";
86 0           my $config_yaml = do { local $/ = <$fh> };
  0            
87 0           close $fh;
88             # Return empty config if no YAML content was found
89 0 0         return bless( {}, $class )
90             unless $config_yaml;
91             # Parse the YAML content; use an empty config if that yields nothing.
92 0   0       $class->config( YAML::XS::Load( $config_yaml ) || {} );
93 0           return $invocant;
94             }
95             }
96              
97             __PACKAGE__->load_config_file;
98              
99             BEGIN {
100 7     7   55 no warnings 'once';
  7         17  
  7         300  
101             # Forcing the dev to always use CPAN's perferred "WebService::Shippo"
102             # namespace is just cruel; allow the use of "Shippo", too.
103 7     7   227 *Shippo::Config:: = *WebService::Shippo::Config::;
104             }
105              
106             1;