Class: Esgob::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/esgob/config.rb

Constant Summary collapse

DEFAULT_API_ENDPOINT =
"https://api.esgob.com/1.0/".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Config

Returns a new instance of Config.

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :account (String)

    The account name

  • :key (String)

    The API key



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

#accountString

Returns:

  • (String)


8
9
10
# File 'lib/esgob/config.rb', line 8

def 
  @account
end

#endpointString

Returns:

  • (String)


6
7
8
# File 'lib/esgob/config.rb', line 6

def endpoint
  @endpoint
end

#filepathString

Path to the configuration file

Returns:

  • (String)


4
5
6
# File 'lib/esgob/config.rb', line 4

def filepath
  @filepath
end

#keyString

Returns:

  • (String)


10
11
12
# File 'lib/esgob/config.rb', line 10

def key
  @key
end

Class Method Details

.default_filepathsArray<String>

Get an ordered list of paths to possible Esgob configuration files

Returns:

  • (Array<String>)

    Array of file paths



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

Parameters:

  • path (String) (defaults to: nil)

    Optional path to a configuration file

Returns:

  • Esgob::Config



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_pairObject

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

Parameters:

  • path (String) (defaults to: nil)

    Optional path to a configuration file



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