App-Presto

 view release on metacpan or  search on metacpan

lib/App/Presto/Config.pm  view on Meta::CPAN

}

sub file {
    my $self = shift;
    (my $file = shift) =~ s{^[\./]+}{}g; # remove leading dots or slashes
    return sprintf('%s/%s', $self->endpoint_dir, $file);
}

sub is_set {
    my $self = shift;
    my $key  = shift;
    return exists $self->config->{$key};
}
sub set {
    my $self = shift;
    my $key  = shift;
    my $value = shift;
    if($key eq 'endpoint'){
        return $self->endpoint($value);
    }
    if(!defined $value){
        delete $self->config->{$key};
    } else {
        $self->config->{$key} = $value;
    }
    eval {
        $self->write_config;
        1;
    } or do {
        warn "unable to persist config: $@\n";
    };
    return;
}
sub unset {
    my $self = shift;
    return $self->set($_[0],undef);
}

sub get {
    my $self = shift;
    my $key  = shift;
    return $self->endpoint if $key eq 'endpoint';
    return exists $self->config->{$key} ? $self->config->{$key} : undef;
}

sub keys {
    my $self = shift;
    return keys %{ $self->config };
}

sub write_config {
    my $self = shift;
    my $config_file = $self->file('config.json');
    open(my $fh, '>', $config_file) or die "Unable to open $config_file for writing: $!";
    print $fh encode_json($self->config);
    close $fh;
    return;
}

my %DEFAULTS = (
    binmode        => 'utf8',
    pretty_printer => 'JSON',
    deserialize_response => 1,
);
sub init_defaults {
    my $self = shift;
    foreach my $k(CORE::keys %DEFAULTS){
        unless($self->is_set($k)){
            $self->set($k, $DEFAULTS{$k});
        }
    }
    return;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Presto::Config - Manage configuration for a given endpoint

=head1 VERSION

version 0.010

=head1 AUTHORS

=over 4

=item *

Brian Phillips <bphillips@cpan.org>

=item *

Matt Perry <matt@mattperry.com> (current maintainer)

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Brian Phillips and Shutterstock Images (http://shutterstock.com).

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



( run in 0.950 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )