class Esgob::Config

Constants

DEFAULT_API_ENDPOINT

Attributes

account[RW]

@return [String]

endpoint[RW]

@return [String]

filepath[RW]

Path to the configuration file @return [String]

key[RW]

@return [String]

Public Class Methods

default_filepaths() click to toggle source

Get an ordered list of paths to possible Esgob configuration files @return [Array<String>] Array of file paths

# 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) click to toggle source

Try and read Esgob configuration either from Environment variables or one of the config files @param [String] path Optional path to a configuration file @return Esgob::Config

# 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
new(args={}) click to toggle source

@param [Hash] args @option args [String] :account The account name @option args [String] :key The API key

# File lib/esgob/config.rb, line 17
def initialize(args={})
  args.each_pair { |k, v| send("#{k}=", v) }
end

Protected Class Methods

load_file(path) click to toggle source
# File lib/esgob/config.rb, line 89
def self.load_file(path)
  config = self.new(:filepath => path)

  File.foreach(path) do |line|
    if line =~ /^(\w+)\s+(.+)$/
      method, value = ["#{$1}=", $2]
      if config.respond_to?(method)
        config.send(method, value)
      end
    end
  end

  config
end

Public Instance Methods

each_pair() { |var.sub(/^@/,''), instance_variable_get(var)| ... } click to toggle source

Calls block once for each configuration key value pair, passing the key and value as parameters.

# 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) click to toggle source

Save Esgob configuration to file If no filepath is given, save to the default filepath @param [String] path Optional path to a configuration file

# 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