Class: NeetoMonitorRuby::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/neeto_monitor_ruby/configuration.rb

Constant Summary collapse

BASE_URL =
"https://neetomonitor.com".freeze
DEFAULT_PING_TIMEOUT =
8
DEFAULT_CONFIG_PATH =
"config/neetomonitor.yml"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/neeto_monitor_ruby/configuration.rb', line 64

def initialize(options = {})
  @options = options
  @base_url = fetch_or_set_value(:base_url, BASE_URL)
  @api_key = fetch_or_set_value(:api_key)
  @environment = fetch_or_set_value(:environment)
  @ping_timeout = fetch_or_set_value(:ping_timeout, DEFAULT_PING_TIMEOUT)
  @logger = Logger.new($stdout)
  @sidekiq_enabled = fetch_or_set_value(:sidekiq_enabled, true)
  @config_path = fetch_or_set_value(:config_path)
  @monitors = options[:monitors]
  @key_prefix_enabled = fetch_or_set_value(:key_prefix_enabled, true)
  @key_prefix = fetch_or_set_value(:key_prefix, app_key_prefix)
end

Class Attribute Details

.configObject

Returns the value of attribute config.



14
15
16
# File 'lib/neeto_monitor_ruby/configuration.rb', line 14

def config
  @config
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def base_url
  @base_url
end

#config_pathObject

Returns the value of attribute config_path.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def config_path
  @config_path
end

#environmentObject

Returns the value of attribute environment.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def environment
  @environment
end

#key_prefixObject

Returns the value of attribute key_prefix.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def key_prefix
  @key_prefix
end

#key_prefix_enabledObject

Returns the value of attribute key_prefix_enabled.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def key_prefix_enabled
  @key_prefix_enabled
end

#loggerObject

Returns the value of attribute logger.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def logger
  @logger
end

#monitorsObject

Returns the value of attribute monitors.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def monitors
  @monitors
end

#optionsObject (readonly)

Returns the value of attribute options.



62
63
64
# File 'lib/neeto_monitor_ruby/configuration.rb', line 62

def options
  @options
end

#ping_timeoutObject

Returns the value of attribute ping_timeout.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def ping_timeout
  @ping_timeout
end

#sidekiq_enabledObject

Returns the value of attribute sidekiq_enabled.



59
60
61
# File 'lib/neeto_monitor_ruby/configuration.rb', line 59

def sidekiq_enabled
  @sidekiq_enabled
end

Class Method Details

.configure!(options = {}, reset = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/neeto_monitor_ruby/configuration.rb', line 16

def configure!(options = {}, reset = false)
  return config if !config.nil? && !reset

  reset! if reset

  config_options = options.dup

  config_path = if options[:config_path]
    options[:config_path]
  else
    file_path = ENV.fetch("NEETO_MONITOR_CONFIG_PATH", DEFAULT_CONFIG_PATH)

    defined?(Rails) && Rails.respond_to?(:root) ? Rails.root.join(file_path) : file_path
  end

  config_options = load_config(config_path).merge(config_options) if config_path && load_config?(config_path)

  self.config = Configuration.new(config_options)
  config
end

.load_config(file_path) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/neeto_monitor_ruby/configuration.rb', line 47

def load_config(file_path)
  config = YAML.load(ERB.new(File.read(file_path)).result(binding))

  JSON.parse(JSON[config], symbolize_names: true)

rescue Errno::ENOENT
  raise ConfigurationError.new("#{file_path} not found")
rescue Psych::SyntaxError => e
  raise ConfigurationError.new("#{file_path} is malformed: #{e.message}")
end

.load_config?(config_path) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/neeto_monitor_ruby/configuration.rb', line 43

def load_config?(config_path)
  File.exist?(config_path)
end

.reset!Object



37
38
39
40
41
# File 'lib/neeto_monitor_ruby/configuration.rb', line 37

def reset!
  self.config = Configuration.new

  config
end