Class: Esgob::Config
- Inherits:
-
Object
- Object
- Esgob::Config
- Defined in:
- lib/esgob/config.rb
Constant Summary collapse
- DEFAULT_API_ENDPOINT =
"https://api.esgob.com/1.0/".freeze
Instance Attribute Summary collapse
- #account ⇒ String
- #endpoint ⇒ String
-
#filepath ⇒ String
Path to the configuration file.
- #key ⇒ String
Class Method Summary collapse
-
.default_filepaths ⇒ Array<String>
Get an ordered list of paths to possible Esgob configuration files.
-
.load(path = nil) ⇒ Object
Try and read Esgob configuration either from Environment variables or one of the config files.
Instance Method Summary collapse
-
#each_pair ⇒ Object
Calls block once for each configuration key value pair, passing the key and value as parameters.
-
#initialize(args = {}) ⇒ Config
constructor
A new instance of Config.
-
#save(path = nil) ⇒ Object
Save Esgob configuration to file If no filepath is given, save to the default filepath.
Constructor Details
#initialize(args = {}) ⇒ Config
Returns a new instance of Config.
17 18 19 |
# File 'lib/esgob/config.rb', line 17 def initialize(args={}) args.each_pair { |k, v| send("#{k}=", v) } end |
Instance Attribute Details
#account ⇒ String
8 9 10 |
# File 'lib/esgob/config.rb', line 8 def account @account end |
#endpoint ⇒ String
6 7 8 |
# File 'lib/esgob/config.rb', line 6 def endpoint @endpoint end |
#filepath ⇒ String
Path to the configuration file
4 5 6 |
# File 'lib/esgob/config.rb', line 4 def filepath @filepath end |
#key ⇒ String
10 11 12 |
# File 'lib/esgob/config.rb', line 10 def key @key end |
Class Method Details
.default_filepaths ⇒ Array<String>
Get an ordered list of paths to possible Esgob configuration files
28 29 30 31 32 33 34 |
# File 'lib/esgob/config.rb', line 28 def self.default_filepaths [ File.join(ENV['HOME'], '.esgob'), '/usr/local/etc/esgob.conf', '/etc/esgob.conf' ] end |
.load(path = nil) ⇒ Object
Try and read Esgob configuration either from Environment variables or one of the config files
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/esgob/config.rb', line 40 def self.load(path=nil) if !path.nil? load_file(path) elsif ENV['ESGOB_ACCOUNT'] and ENV['ESGOB_KEY'] self.new( :account => ENV['ESGOB_ACCOUNT'], :key => ENV['ESGOB_KEY'] ) else default_filepaths.each do |path| if File.exist?(path) return load_file(path) end end # No config file found, return nil nil end end |
Instance Method Details
#each_pair ⇒ Object
Calls block once for each configuration key value pair, passing the key and value as parameters.
79 80 81 82 83 84 |
# File 'lib/esgob/config.rb', line 79 def each_pair instance_variables.sort.each do |var| next if var.to_s == '@filepath' yield(var.to_s.sub(/^@/,''), instance_variable_get(var)) end end |
#save(path = nil) ⇒ Object
Save Esgob configuration to file If no filepath is given, save to the default filepath
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/esgob/config.rb', line 63 def save(path=nil) if !path.nil? self.filepath = path elsif filepath.nil? self.filepath = self.class.default_filepaths.first end File.open(filepath, 'wb') do |file| each_pair do |key,value| file.puts "#{key} #{value}" end end end |