smartnotify 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f3ba8f116155963a266287afed9cf2286d11068
4
+ data.tar.gz: cbbc33d7a21acb9232af8c1c71fa017244948715
5
+ SHA512:
6
+ metadata.gz: d46b5f331b5d9cffee1d31d31636ebca32da2fd30c76c2074d7238d2cdca7968733410fd589e317b9f2b2d98c9a9783bb07f921ce296f77cc24496ce7ae49ee3
7
+ data.tar.gz: 4512615ceec95c6fd80004fc44eaebf9e380e97b42aaf706db3ccc90d36a6e8d63961884fdebad97dc5b299d8a7cc203aff9a23ca1663d9906d8ac376b0409a5
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # SmartNotify
2
+
3
+ Ruby Gem designed for sending various types of notifications via a common configurable interface.
4
+
5
+ ## Overview
6
+ The gem makes use of two components, modules, and templates.
7
+
8
+ Modules are defined within this gem, and provide various forms of generic notification functionality. The modules are created to be reusable. A module could be responsible for sending emails, posting Slack notifications, making a REST call to an external API, whatever.
9
+
10
+ Templates on the other hand, consume a specific module, in conjunction with template specific configuration, creating very specific use cases. These use cases could be team specific, allowing different consumers to tweak who, and how notifications are distributed.
11
+
12
+ ## Why
13
+ There was a need to easily send SLA incident emails to the correct person on support, as well as to a MS Teams channel. A module was created `rotation_emailer` which could send emails, based on a given email template, and support rotation csv file. With that in place, a template was created `incident_start_email` which defines where the specific email template, and support rotation csv file is located for this particular instance. This is awesome, because a new team or solution could create a template, changing only the support rotation csv location, and get the same functionality for next to no setup cost.
14
+
15
+
16
+ ## Usage
17
+ Add `gem 'smartnotify'` to your Gemfile. *Latest version can be found internally at [gems.hax](http://gems.hax)*
18
+ ```ruby
19
+ require 'smartnotify'
20
+ # ... your logic ...
21
+ smartnotify = SmartNotify.new
22
+ smartnotify.run(template, json, [debug], [dryrun])
23
+ ```
24
+
25
+ * **template** - [String] The id of the template to use. The templates are configured externally in [PHPerfImp/smartnotify-config](https://github.cerner.com/PHPerfImp/smartnotify-config)
26
+ * **json** - [String] The JSON key/value pairs to be used as input to the selected template. Each template defines what keys must be provided.
27
+ * **debug** - [Boolean] *(optional, defaults false)* Enables debug logging within SmartNotify.
28
+ * **dryrun** - [Boolean] *(optional, defaults false)* Enables debug logging within SmartNotify, and disables sending any notifications for testing purposes.
29
+
30
+ ### Example
31
+ ```ruby
32
+ smartnotify = SmartNotify.new
33
+ smartnotify.run('incident_start_email', "{\"mnemonic\": \"sbmcin\", \"cerner_mnemonic\": \"UNVR_NY\", \"link\": \"https://jira2.cerner.com/browse/PERFIMPDEV-1349\", \"title\": \"Readmission SLA Exceeded\", \"message\": \"Readmission SLA exceeded, please check the JIRA for more information.\"}")
34
+ ```
35
+
36
+ ## Modules
37
+ The following are the currently available modules that can be consumed. Follow the link to get more information on
38
+ intended usage, and configuration.
39
+
40
+ * [Rotation Emailer](modules/roatation_emailer/README.md)
41
+
42
+ ## Releasing New Version
43
+
44
+ 1. Clone the gem locally.
45
+ ```
46
+ git clone https://github.cerner.com/PHPerfImp/smartnotify.git
47
+ cd smartnotify
48
+ bundle install
49
+ ```
50
+
51
+ 2. Make your change.
52
+
53
+ 3. Update [smartnotify.gemspec](smartnotify.gemspec]) gem version.
54
+
55
+ 4. Build the gem locally
56
+ ```
57
+ gem build smartnotify
58
+ ```
59
+ 5. Upload created `smartnotify-x.x.x.gem` internally to [gems.hax](http://gems.hax)
60
+
@@ -0,0 +1 @@
1
+ config-url: 'https://raw.github.cerner.com/PHPerfImp/smartnotify-config/master/config/config.yml'
@@ -0,0 +1,31 @@
1
+ # Module - Rotation Emailer
2
+
3
+ **Module ID:** `rotation_emailer`
4
+
5
+ ## Required Configuration
6
+
7
+ * *email_template* - A URL pointed to the template of the email message to send. This document should be a .erb file.
8
+ The data is fed into the template from the provided json, as a hash. *e.g accessing a title key would look like
9
+ `<%= data['title'] %>`*
10
+
11
+ * *support_rotation* - A URL pointed to a CSV file containing email addresses, combined with start/end times for
12
+ when the given address is valid to email. On each run, the file will be parsed looking for emails that are valid at that
13
+ time and will send the message to all the addresses as one group email. At a minimum, the following three columns
14
+ with headers must be specified:
15
+
16
+ * `Start Time` - Formatted as 12/Aug/2017. This is the date, starting at the beginning of the day, that the email
17
+ address can start being emailed.
18
+ * `End Time` - Formatted as 18/Aug/2017. This is the date, ending at midnight, that the email address is no longer
19
+ valid to notify after.
20
+ * `Email Address` - Email address to notify.
21
+
22
+ *Note:*
23
+ * *All times a calculated with the configured timezone of Central Time (US & Canada)*
24
+ * *Column headers are case sensitive.*
25
+
26
+ ## Required JSON keys
27
+ While most of the JSON key/value pairs are defined based on what
28
+ the template needs, this module requires that a `title` JSON key
29
+ be provided which is used as the subject of the email. If no title
30
+ key is defined, a fallback *'Rotation Emailer Notification'* title
31
+ will be used as the email subject instead.
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mail'
4
+ require 'yaml'
5
+ require 'csv'
6
+ require 'open-uri'
7
+
8
+ class NotificationModule
9
+
10
+ def initialize(config)
11
+ @config = config
12
+ end
13
+
14
+ def run
15
+ c = @config
16
+
17
+ # Setup Mailer
18
+ Mail.defaults do
19
+ delivery_method :smtp, {
20
+ :address => c[:global]['smtp']['host'],
21
+ :port => c[:global]['smtp']['port']
22
+ }
23
+ end
24
+
25
+ # Load Email Addresses
26
+ if c[:global]['debug']
27
+ puts 'DEBUG:'
28
+ puts 'DEBUG: Loading support rotation from:'
29
+ puts "DEBUG: #{c[:config]['support_rotation']}"
30
+ end
31
+
32
+ send_list = []
33
+ CSV.new(open(c[:config]['support_rotation']), :headers => :first_row).each do |email|
34
+ send_list.push("#{email['Email Address']}") if active_email(email, c[:global]['timezone'])
35
+ end
36
+
37
+ if send_list.empty?
38
+ puts 'ERROR: No email addresses are setup for the current time.'
39
+ return false
40
+ end
41
+
42
+ if c[:global]['debug']
43
+ puts 'DEBUG:'
44
+ puts 'DEBUG: Found the following email addresses to notify:'
45
+
46
+ send_list.each do |recipient|
47
+ puts "DEBUG: #{recipient}"
48
+ end
49
+ end
50
+
51
+ # Load Template
52
+ if c[:global]['debug']
53
+ puts 'DEBUG:'
54
+ puts 'DEBUG: Loading email template from:'
55
+ puts "DEBUG: #{c[:config]['email_template']}"
56
+ end
57
+
58
+ email_template = open(c[:config]['email_template']).read
59
+
60
+ if c[:global]['debug']
61
+ puts 'DEBUG:'
62
+ puts 'DEBUG: Loaded the following email template:'
63
+ email_template.each_line do |line|
64
+ puts "DEBUG: #{line}"
65
+ end
66
+ end
67
+
68
+ # Populate Template
69
+ b = binding
70
+ b.local_variable_set(:data, c[:data])
71
+ message = ERB.new(email_template).result(b)
72
+
73
+ if c[:global]['debug']
74
+ puts 'DEBUG:'
75
+ puts 'DEBUG: Prepared the following email message:'
76
+ message.each_line do |line|
77
+ puts "DEBUG: #{line}"
78
+ end
79
+ end
80
+
81
+ # Send The Emails
82
+ if c[:global]['dryrun']
83
+ puts 'DEBUG:'
84
+ puts 'DEBUG: Not sending email because dryrun is enabled.'
85
+ else
86
+ title = c[:data]['title'].nil? ? 'Rotation Emailer Notification' : c[:data]['title']
87
+ send_email(title, c[:global]['smtp']['from'], send_list, message)
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def active_email(email, tz)
94
+ start_time = DateTime.parse(email['Start Time'] + ' ' + tz).beginning_of_day
95
+ end_time = DateTime.parse(email['End Time'] + ' ' + tz).end_of_day
96
+ current_time = DateTime.now
97
+ start_time <= current_time and end_time > current_time
98
+ end
99
+
100
+ def send_email(subject_text, from_email, to_email, body_text)
101
+ Mail.deliver do
102
+ to to_email
103
+ from from_email
104
+ subject subject_text
105
+ html_part do
106
+ content_type 'text/html; charset=UTF-8'
107
+ body body_text
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'erb'
5
+ require 'json'
6
+ require 'open-uri'
7
+
8
+ class SmartNotify
9
+ attr_accessor :config
10
+
11
+ def run(template, json, debug = false, dryrun = false)
12
+
13
+ # Ensure Options Provided
14
+ if json.nil? or template.nil?
15
+ puts 'ERROR: Must provide template, and JSON data.'
16
+ return false
17
+ end
18
+
19
+ # Load Configuration
20
+ c = YAML.load_file(File.join(__dir__, 'config/config.yml'))
21
+ if debug
22
+ puts 'DEBUG: Loading SmartNotify config from:'
23
+ puts "DEBUG: #{c['config-url']}"
24
+ end
25
+ config = YAML.load(open(c['config-url']).read)
26
+
27
+ # Load Template Configuration
28
+ template_config = config['templates'][template]
29
+ if template_config.nil?
30
+ puts "ERROR: Template '#{template}' not found."
31
+ return false
32
+ end
33
+
34
+ # Configure Development Flags
35
+ config['global']['debug'] = debug || dryrun
36
+ config['global']['dryrun'] = dryrun
37
+
38
+
39
+ # Load JSON
40
+ begin
41
+ template_data = JSON.parse(json)
42
+ rescue JSON::ParserError => e
43
+ puts 'ERROR: JSON provided is unable to be parsed.'
44
+ return false
45
+ end
46
+
47
+ # Prepare Module Configuration
48
+ module_config = Hash.new
49
+ module_config[:config] = template_config
50
+ module_config[:data] = template_data
51
+ module_config[:global] = config['global']
52
+
53
+ # Ensure Fields Present
54
+ unless module_config[:config]['fields'].nil?
55
+ module_config[:config]['fields'].each do |field|
56
+ if module_config[:data][field].nil?
57
+ puts "ERROR: JSON provided is missing required key '#{field}'"
58
+ return false
59
+ end
60
+ end
61
+ end
62
+
63
+ # Load Module Resources
64
+ # begin
65
+ require_relative "modules/#{module_config[:config]['module']}/#{module_config[:config]['module']}"
66
+ # rescue LoadError
67
+ # puts "#{module_config[:config]['module']} module was not found."
68
+ # puts 'Ensure the correct module name is defined in the configuration.'
69
+ # return false
70
+ # end
71
+
72
+ # Run Module
73
+ NotificationModule.new(module_config).run
74
+ end
75
+ end
76
+
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smartnotify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.16
5
+ platform: ruby
6
+ authors:
7
+ - Performance Improvement Dev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Utility for sending various types of notifications via a common configurable
14
+ interface.
15
+ email:
16
+ - kristopher.williams@cerner.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - lib/config/config.yml
23
+ - lib/modules/rotation_emailer/README.md
24
+ - lib/modules/rotation_emailer/rotation_emailer.rb
25
+ - lib/smartnotify.rb
26
+ homepage: https://github.cerner.com/PHPerfImp/smartnotify
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.12
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Common interface notification utility
49
+ test_files: []