| 1 | package Acme::Coinbase::Config; | ||
| 2 | # vim: set ts=4 sw=4 expandtab showmatch | ||
| 3 | # | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | # FOR MOOSE | ||
| 7 | use Moose; # automatically turns on strict and warnings | ||
| 8 | #use Config::INI::Reader; | ||
| 9 | use Config::IniFiles; | ||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | has 'config_file' => (is => 'rw', isa => 'Str'); | ||
| 13 | has 'config_reader' => (is => 'rw'); | ||
| 14 | |||
| 15 | sub read_config { | ||
| 16 | my $self = shift; | ||
| 17 | die "$0: no config file" unless $self->config_file; | ||
| 18 | print "$0: Config file is " . $self->config_file . "\n"; | ||
| 19 | #my $config_hash = Config::INI::Reader->read_file( $self->config_file ); | ||
| 20 | my $config = Config::IniFiles->new(); # -file => $self->config_file); | ||
| 21 | $config->SetFileName( $self->config_file ) || die "$0: Can't set config filename\n";; | ||
| 22 | $config->ReadConfig(); | ||
| 23 | #print "READ: " . $config->OutputConfig(); | ||
| 24 | $self->config_reader( $config ); | ||
| 25 | } | ||
| 26 | sub get_param { | ||
| 27 | my ($self, $section, $param) = @_; | ||
| 28 | return "" unless($self->config_reader) ; | ||
| 29 | return $self->config_reader->val( $section, $param ) || ""; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub dump { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | my @sections = $self->config_reader->Sections(); | ||
| 36 | return "Sections: " . Dumper( \@sections ) . "\n"; | ||
| 37 | } | ||
| 38 | |||
| 39 | # dummy data | ||
| 40 | #has 'api_key' => (is => 'rw', isa => 'Str', default=>"lmnop"); | ||
| 41 | #has 'api_secret' => (is => 'rw', isa => 'Str', default=>"qwerty"); | ||
| 42 | |||
| 43 | ############################################# | ||
| 44 | # we expect a config file, if used, to look like: | ||
| 45 | # [default] | ||
| 46 | # api_key = keyasdakjb34234kj | ||
| 47 | # api_secret = secretk3j4204h554j2h3409u34bn | ||
| 48 | ############################################# | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ |