Class: SmartNotify

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/smartnotify.rb', line 9

def config
  @config
end

Instance Method Details

#run(template, json, debug = false, dryrun = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/smartnotify.rb', line 11

def run(template, json, debug = false, dryrun = false)

  # Ensure Options Provided
  if json.nil? or template.nil?
    puts 'ERROR: Must provide template, and JSON data.'
    return false
  end

  # Load Configuration
  c = YAML.load_file(File.join(__dir__, 'config/config.yml'))
  if debug
    puts 'DEBUG: Loading SmartNotify config from:'
    puts "DEBUG:   #{c['config-url']}"
  end
  config = YAML.load(open(c['config-url']).read)

  # Load Template Configuration
  template_config = config['templates'][template]
  if template_config.nil?
    puts "ERROR: Template '#{template}' not found."
    return false
  end

  # Configure Development Flags
  config['global']['debug'] = debug || dryrun
  config['global']['dryrun'] = dryrun


  # Load JSON
  begin
    template_data = JSON.parse(json)
  rescue JSON::ParserError => e
    puts 'ERROR: JSON provided is unable to be parsed.'
    return false
  end

  # Prepare Module Configuration
  module_config = Hash.new
  module_config[:config] = template_config
  module_config[:data] = template_data
  module_config[:global] = config['global']

  # Ensure Fields Present
  unless module_config[:config]['fields'].nil?
    module_config[:config]['fields'].each do |field|
      if module_config[:data][field].nil?
        puts "ERROR: JSON provided is missing required key '#{field}'"
        return false
      end
    end
  end

  if module_config[:config]['module'] == 'rotation_emailer'
    require_relative 'modules/rotation_emailer/rotation_emailer'
  else
    puts "'#{module_config[:config]['module']}' module does not exist."
    puts 'Ensure the correct module name is defined in the configuration.'
    return false
  end

  # Run Module
  NotificationModule.new(module_config).run
end