Class: Nadir::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



12
13
14
15
16
17
18
19
# File 'lib/nadir/config.rb', line 12

def initialize
  @env         = ENV['NADIR_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV']
  @api_key     = ENV['NADIR_API_KEY']
  @api_url     = 'https://nadir.dev/api'.freeze
  @enabled_for = %w(production staging)

  @logger      = Logger.new(STDOUT)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/nadir/config.rb', line 5

def api_key
  @api_key
end

#api_urlObject

Returns the value of attribute api_url.



5
6
7
# File 'lib/nadir/config.rb', line 5

def api_url
  @api_url
end

#enabled_forObject

Returns the value of attribute enabled_for.



5
6
7
# File 'lib/nadir/config.rb', line 5

def enabled_for
  @enabled_for
end

#envObject

Returns the value of attribute env.



5
6
7
# File 'lib/nadir/config.rb', line 5

def env
  @env
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/nadir/config.rb', line 5

def logger
  @logger
end

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/nadir/config.rb', line 5

def root
  @root
end

Instance Method Details

#load_for(app) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nadir/config.rb', line 37

def load_for(app)
  @env  ||= Rails.env
  @root   = app.root

  config_file = @root.join 'config/nadir.yml'

  file_config = YAML.safe_load File.read config_file

  @api_key     = file_config['api_key'] if file_config['api_key']
  @api_url     = file_config['api_url'] if file_config['api_url']
  @enabled_for = file_config['enabled_for'] if file_config['enabled_for']
end

#validateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nadir/config.rb', line 21

def validate
  unless @api_key
    logger.warn '[Nadir] API-KEY not set, skipping notifications.'

    return false
  end

  unless @enabled_for.include? @env.to_s
    logger.warn "[Nadir] Reporting disabled for environment '#{@env}', skipping notification."

    return false
  end

  true
end