mn_utils_gem 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c233647ceb909d90fcddcab30b19819d749d2d82a0ae05c00b91fcb62e6498ae
4
- data.tar.gz: 4c16da8fb1716345f068659a2043dc24848481ac05a6f09826fe845e4f80a9dc
3
+ metadata.gz: ad2f7397f4cf0a20fd6a4a2ab3a02827bc336ff19f84389b0418c3bf6e5c0475
4
+ data.tar.gz: 705a9af48e033b191bb1b3122f96a34cd730de35255f396d85a3ec6a390f0217
5
5
  SHA512:
6
- metadata.gz: 026c351ad8e12472d2fc0651e5c0f8706aca912764983976bb51a41fabce8b891028bb5d9ab4260e969c778f666f603c1ba22beb1d1f083ba2b7a0d5de774421
7
- data.tar.gz: dbcfb61093ac5200db3756a3abee4b6853754d2db488f18211c5d55903dcde77b5ce91d473e44ab57fafba91bfebf5b38d76ca584d9b26d4336e9d297b1c1d0c
6
+ metadata.gz: 6371fe07495e1581482c2c3f52c29ea3134b958c1301d39ee842a203d8be32b24ff150b83f5ab9adfff49023723744947d4b3950622ebaa4dce4a4b28d74189b
7
+ data.tar.gz: 00512bf9b87dfb58b928b53a84fdca92ce5607b33044335d23e3eabe8888f6c8af9c28c9c1aa39178e619887bb9fe707f3122d27aa82dffc9f1f280d3a22a6e7
@@ -2,6 +2,8 @@ require 'gelf'
2
2
  require 'aws-sdk-cloudwatch'
3
3
  require 'request_store'
4
4
  require 'singleton'
5
+ require 'active_support'
6
+ require 'active_support/core_ext'
5
7
 
6
8
  # SiteAction class to log important site actions to Graylog
7
9
  # and send a matching metric to Cloudwatch
@@ -79,6 +81,9 @@ module MnUtilsLogging
79
81
  :mention_notif_email_success,
80
82
  :mention_notif_email_fail
81
83
  ],
84
+ voting: [
85
+ :aibu_vote_success
86
+ ],
82
87
  admin: [
83
88
  :admin_email_success,
84
89
  :admin_email_fail,
@@ -106,6 +111,9 @@ module MnUtilsLogging
106
111
  # create a reverse map of all site actions and their corresponding group
107
112
  # for quick lookups by the code
108
113
  @_site_action_group_map = Hash[*(@_site_actions_and_groups.map {|k, arr| arr.map {|v| [v, k]}}.flatten)].freeze
114
+
115
+ # setup the logger
116
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
109
117
  end
110
118
 
111
119
  def log(message, site_action, payload = {})
@@ -196,10 +204,10 @@ module MnUtilsLogging
196
204
  n = GELF::Notifier.new(ENV['GRAYLOG_GELF_UDP_HOST'], ENV['GRAYLOG_GELF_UDP_PORT'])
197
205
  n.notify! payload
198
206
  else
199
- Rails.logger.debug("Payload for Graylog: #{payload}")
207
+ @logger.debug("Payload for Graylog: #{payload}")
200
208
  end
201
209
  rescue Exception => e
202
- Rails.logger.error e
210
+ @logger.error e
203
211
  end
204
212
 
205
213
  def send_to_cloudwatch(site_action_group, site_action, site_hostname)
@@ -223,10 +231,10 @@ module MnUtilsLogging
223
231
  cw = Aws::CloudWatch::Client.new
224
232
  cw.put_metric_data(cloudwatch_payload)
225
233
  else
226
- Rails.logger.debug("Payload for Cloudwatch: #{cloudwatch_payload}")
234
+ @logger.debug("Payload for Cloudwatch: #{cloudwatch_payload}")
227
235
  end
228
236
  rescue Exception => e
229
- Rails.logger.error e
237
+ @logger.error e
230
238
  end
231
239
 
232
240
  end # of class SiteAction
@@ -0,0 +1,75 @@
1
+ require 'aws-sdk-sqs'
2
+ require 'request_store'
3
+ require 'singleton'
4
+
5
+ # TransactionalEmail class to send transactional emails
6
+ # via the mail service.
7
+ #
8
+ # usage: MnUtilsEmail::TransactionalEmail.instance.enqueue(
9
+ # message_type:, to_address:, subject:, fallback_text:, template_fields: {}, cc_addresses: '')
10
+ #
11
+ # eg: MnUtilsEmail::TransactionalEmail.instance.enqueue(
12
+ # message_type: 'REGISTRATION_WELCOME',
13
+ # to_address: 'joe@example.com',
14
+ # subject: 'Welcome to Mumsnet',
15
+ # fallback_text: "Dear joe\nWelcome to Mumsnet\nMNHQ"
16
+ # )
17
+ # This puts the message onto the correct SQS queue
18
+ # which will then be picked up by the mail service and sent.
19
+
20
+ module MnUtilsEmail
21
+ class TransactionalEmail
22
+
23
+ include Singleton
24
+
25
+ def initialize
26
+ # setup the logger
27
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
28
+ end
29
+
30
+ def enqueue(message_type:, to_address:, subject:, fallback_text:, template_fields: {}, cc_addresses: '')
31
+
32
+ # validate the parameters
33
+ raise ArgumentError, "message_type cannot be blank" \
34
+ if message_type.blank?
35
+ raise ArgumentError, "to_address cannot be blank" \
36
+ if to_address.blank?
37
+ raise ArgumentError, "subject cannot be blank" \
38
+ if subject.blank?
39
+ raise ArgumentError, "fallback_text cannot be blank" \
40
+ if fallback_text.blank?
41
+ raise ArgumentError, "to_address #{to_address} is not a valid email address" \
42
+ unless to_address =~ /@/
43
+
44
+ # construct the message body for SQS
45
+ message_body = {
46
+ message_schema_version: 1,
47
+ message_type: message_type,
48
+ template_fields: template_fields.to_json,
49
+ to_address: to_address,
50
+ cc_addresses: cc_addresses,
51
+ subject: subject,
52
+ fallback_text: fallback_text
53
+ }
54
+
55
+ # add the request ID if available
56
+ request_id = ''
57
+ request_id = RequestStore.store[:request_id] if RequestStore.store[:request_id]
58
+ message_body[:request_id] = request_id
59
+
60
+ if ENV.key? 'SQS_MAIL2_QUEUE_URL'
61
+ # put it on the SQS queue
62
+ sqs = Aws::SQS::Client.new()
63
+ sqs.send_message(
64
+ queue_url: ENV['SQS_MAIL2_QUEUE_URL'],
65
+ message_body: message_body.to_json
66
+ )
67
+ else
68
+ @logger.debug("Payload for SQS: #{message_body}")
69
+ end
70
+
71
+ end # of method enqueue
72
+
73
+ end # of class TransactionalEmail
74
+
75
+ end # of module MnUtilsEmail
@@ -1,3 +1,3 @@
1
1
  module MnUtilsGem
2
- VERSION = "1.7.0"
2
+ VERSION = "1.8.0"
3
3
  end
data/lib/mn_utils_gem.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "mn_utils_gem/version"
2
2
  require "mn_utils_gem/site_action"
3
+ require "mn_utils_gem/transactional_email"
3
4
 
4
5
  module MnUtilsGem
5
6
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mn_utils_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shamim Mirzai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-09 00:00:00.000000000 Z
11
+ date: 2019-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gelf
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-sqs
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: request_store
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: bundler
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -102,6 +130,7 @@ extra_rdoc_files: []
102
130
  files:
103
131
  - lib/mn_utils_gem.rb
104
132
  - lib/mn_utils_gem/site_action.rb
133
+ - lib/mn_utils_gem/transactional_email.rb
105
134
  - lib/mn_utils_gem/version.rb
106
135
  homepage: https://github.com/mumsnet/mn_utils_gem
107
136
  licenses: []