Class: NotificationModule

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ NotificationModule

Returns a new instance of NotificationModule.



10
11
12
# File 'lib/modules/rotation_emailer/rotation_emailer.rb', line 10

def initialize(config)
  @config = config
end

Instance Method Details

#runObject



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/modules/rotation_emailer/rotation_emailer.rb', line 14

def run

  c = @config

  if c[:global]['debug']
    puts 'DEBUG:'
    puts 'DEBUG: Starting rotation_emailer logic...'
  end


  # Setup Mailer

  if c[:global]['debug']
    puts 'DEBUG:'
    puts 'DEBUG: Setting up Mail.defaults with the following configuration:'
    puts "DEBUG:   host: #{c[:global]['smtp']['host']}"
    puts "DEBUG:   port: #{c[:global]['smtp']['port']}"
  end
  Mail.defaults do
    delivery_method :smtp, {
        :address => c[:global]['smtp']['host'],
        :port => c[:global]['smtp']['port']
    }
  end

  # Load Email Addresses
  if c[:global]['debug']
    puts 'DEBUG:'
    puts 'DEBUG: Loading support rotation from:'
    puts "DEBUG:   #{c[:config]['support_rotation']}"
  end

  send_list = []
  CSV.new(open(c[:config]['support_rotation']), :headers => :first_row).each do |email|
    send_list.push("#{email['Email Address']}") if active_email(email, c[:global]['timezone'])
  end

  if send_list.empty?
    puts 'ERROR: No email addresses are setup for the current time.'
    return false
  end

  if c[:global]['debug']
    puts 'DEBUG:'
    puts 'DEBUG: Found the following email addresses to notify:'

    send_list.each do |recipient|
      puts "DEBUG:   #{recipient}"
    end
  end

  # Load Template
  if c[:global]['debug']
    puts 'DEBUG:'
    puts 'DEBUG: Loading email template from:'
    puts "DEBUG:   #{c[:config]['email_template']}"
  end

  email_template = open(c[:config]['email_template']).read

  if c[:global]['debug']
    puts 'DEBUG:'
    puts 'DEBUG: Loaded the following email template:'
    email_template.each_line do |line|
      puts "DEBUG:    #{line}"
    end
  end

  # Populate Template
  b = binding
  b.local_variable_set(:data, c[:data])
  message = ERB.new(email_template).result(b)

  if c[:global]['debug']
    puts 'DEBUG:'
    puts 'DEBUG: Prepared the following email message:'
    message.each_line do |line|
      puts "DEBUG:    #{line}"
    end
  end

  # Send The Emails
  if c[:global]['dryrun']
    puts 'DEBUG:'
    puts 'DEBUG: Not sending email because dryrun is enabled.'
  else
    title = c[:data]['title'].nil? ? 'Rotation Emailer Notification' : c[:data]['title']
    send_email(title, c[:global]['smtp']['from'], send_list, message)
  end
end