| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 7 |  |  | 7 |  | 35 | use strict; | 
|  | 7 |  |  |  |  | 12 |  | 
|  | 7 |  |  |  |  | 193 |  | 
| 2 | 7 |  |  | 7 |  | 35 | use warnings; | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 278 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package WebService::Shippo::Config; | 
| 5 |  |  |  |  |  |  | require WebService::Shippo::Request; | 
| 6 | 7 |  |  | 7 |  | 35 | use Cwd; | 
|  | 7 |  |  |  |  | 14 |  | 
|  | 7 |  |  |  |  | 484 |  | 
| 7 | 7 |  |  | 7 |  | 35 | use Carp          ( 'croak' ); | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 277 |  | 
| 8 | 7 |  |  | 7 |  | 242310 | use File::HomeDir (); | 
|  | 7 |  |  |  |  | 1350815 |  | 
|  | 7 |  |  |  |  | 157 |  | 
| 9 | 7 |  |  | 7 |  | 5034 | use Path::Class   (); | 
|  | 7 |  |  |  |  | 381158 |  | 
|  | 7 |  |  |  |  | 162 |  | 
| 10 | 7 |  |  | 7 |  | 4861 | use YAML::XS      (); | 
|  | 7 |  |  |  |  | 19794 |  | 
|  | 7 |  |  |  |  | 4158 |  | 
| 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 | 18 | my ( $invocant, $new_value ) = @_; | 
| 29 | 7 | 50 |  |  |  | 44 | unless ( $value ) { | 
| 30 | 7 |  |  |  |  | 20 | for my $candidate ( @SEARCH_PATH ) { | 
| 31 | 28 | 50 |  |  |  | 602 | if ( -e $candidate ) { | 
| 32 | 0 |  |  |  |  | 0 | $value = $candidate; | 
| 33 | 0 |  |  |  |  | 0 | last; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 | 7 | 50 |  |  |  | 43 | 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 |  |  |  | 40 | 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 |  |  | 66 | my $class = ref( $invocant ) || $invocant; | 
| 77 | 7 |  |  |  |  | 25 | my $config_file = $class->config_file; | 
| 78 |  |  |  |  |  |  | # Return empty config if no config file exists | 
| 79 | 7 | 50 | 33 |  |  | 43 | 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 |  | 52 | no warnings 'once'; | 
|  | 7 |  |  |  |  | 17 |  | 
|  | 7 |  |  |  |  | 282 |  | 
| 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 |  | 234 | *Shippo::Config:: = *WebService::Shippo::Config::; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | 1; |