Class: Chook::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chook/configuration.rb

Overview

The configuration object

Constant Summary collapse

DEFAULT_CONF_FILE =

The location of the default config file

Pathname.new '/etc/chook.conf'
SAMPLE_CONF_FILE =
Pathname.new(__FILE__).parent.parent.parent + 'data/chook.conf.example'
CONF_KEYS =

The attribute keys we maintain, and how they should be converted to the value used by chook internally.

For descriptions of the keys, see data/chook.conf.example

{
  port: :to_i,
  concurrency: Chook::Procs::STRING_TO_BOOLEAN,
  handler_dir: Chook::Procs::STRING_TO_PATHNAME,
  use_ssl: Chook::Procs::STRING_TO_BOOLEAN,
  ssl_cert_path: Chook::Procs::STRING_TO_PATHNAME,
  ssl_private_key_path: Chook::Procs::STRING_TO_PATHNAME,
  log_file: Chook::Procs::STRING_TO_PATHNAME,
  log_level: Chook::Procs::STRING_TO_LOG_LEVEL,
  log_max_megs:  :to_i,
  logs_to_keep: :to_i,
  webhooks_user: nil,
  webhooks_user_pw: nil
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize!



77
78
79
# File 'lib/chook/configuration.rb', line 77

def initialize
  read_global
end

Instance Method Details

#clear_allvoid

This method returns an undefined value.

Clear all values



88
89
90
# File 'lib/chook/configuration.rb', line 88

def clear_all
  CONF_KEYS.keys.each { |k| send "#{k}=".to_sym, nil }
end

This method returns an undefined value.

Print out the current settings to stdout



157
158
159
# File 'lib/chook/configuration.rb', line 157

def print
  CONF_KEYS.keys.sort.each { |k| puts "#{k}: #{send k}" }
end

#read_globalBoolean

(Re)read the global prefs, if it exists.

Returns:

  • (Boolean)

    was the file loaded?



96
97
98
99
# File 'lib/chook/configuration.rb', line 96

def read_global
  return false unless DEFAULT_CONF_FILE.file? && DEFAULT_CONF_FILE.readable?
  read DEFAULT_CONF_FILE
end

#reload(file = DEFAULT_CONF_FILE) ⇒ Boolean

Clear the settings and reload the prefs file, or another file if provided

Parameters:

  • file (String, Pathname) (defaults to: DEFAULT_CONF_FILE)

    a non-standard prefs file to load

Returns:

  • (Boolean)

    was the file reloaded?



107
108
109
110
111
112
# File 'lib/chook/configuration.rb', line 107

def reload(file = DEFAULT_CONF_FILE)
  file = Pathname.new file
  return false unless file.file? && file.readable?
  clear_all
  read file
end

#save(file) ⇒ void

This method returns an undefined value.

Save the prefs into a file

Parameters:

  • file (Symbol, String, Pathname)

    either :user, :global, or an arbitrary file to save.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chook/configuration.rb', line 120

def save(file)
  path = Pathname.new(file)

  # file already exists? read it in and update the values.
  # Don't overwrite it, since the user might have comments
  # in there.
  if path.readable?
    data = path.read

    # go thru the known attributes/keys
    CONF_KEYS.keys.sort.each do |k|
      # if the key exists, update it.
      if data =~ /^#{k}:/
        data.sub!(/^#{k}:.*$/, "#{k}: #{send k}")

      # if not, add it to the end unless it's nil
      else
        data += "\n#{k}: #{send k}" unless send(k).nil?
      end # if data =~ /^#{k}:/
    end # each do |k|

  else # not readable, make a new file
    data = ''
    CONF_KEYS.keys.sort.each do |k|
      data << "#{k}: #{send k}\n" unless send(k).nil?
    end
  end # if path readable

  # make sure we end with a newline, the save it.
  data << "\n" unless data.end_with?("\n")
  path.open('w') { |f| f.write data }
end