usps-support 0.2.66 → 0.2.68

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: fe5075012e0988ba212db3443f7e168933e1477def45cfcecab1dfe20926d823
4
- data.tar.gz: 93316be136f7099ee57549e282c8f46df57e2cf7111163f91688a962d3b6f20b
3
+ metadata.gz: 9ab084dcb52c5a1119ae74ca610dfe8b2a085e4696cff6ce8b1ff5762f127e70
4
+ data.tar.gz: ef0b3eb3c2adaf9d14dce1a21edd28c9d4b75d4e44ddac21fc8784e64c8c5be5
5
5
  SHA512:
6
- metadata.gz: aa3cec6e37810a31590322d78d3a67fbe6e4fc284561616dc7023274eaaf3061db96222e1d1a8c3f8a532af5b3b95f2b2752f5cef25ef5c54e7ffbc7cef2b3c7
7
- data.tar.gz: '09ebdb0cffb6cdedea903c65e9f3fa8fc2f2512823aad557733b121ca5a241f503e0caf16372d323071e63e2eae3f7aeb1c4cdac8c4ad83ee8e2fde8f326f847'
6
+ metadata.gz: 8cc4c79d0e24bf6ce257071e3bd43eeb34f776ab93277e80a36051da47f34d239d804b76db7c90df1555b8c661c2f73cdd3aa98b0b592a8e005ff22fdac488db
7
+ data.tar.gz: cf6505c09e4feb247910c4fea0c4a11334e665b868c96d2d66c6879b05651256004316dd8a1714b933dd50a0d90e6f3594d1e94c76683796a92ba9273ef5acba
data/Readme.md CHANGED
@@ -66,3 +66,19 @@ Dir.glob('lib/proto/extensions/*.rb').sort.each { |f| require_relative("../../#{
66
66
 
67
67
  Usps::Example::SerializableMessage.extend(Usps::Support::Lib::ProtoSerialize)
68
68
  ```
69
+
70
+ ### Blocked Email Domains
71
+
72
+ `Usps::Support::BlockedDomainInterceptor` drops `to`/`cc`/`bcc` recipients at domains that never accept mail
73
+ (`abcboating.com`, `abcemail.com`, `example.com`, and their subdomains), and suppresses delivery entirely if no
74
+ recipients remain. Each interception is logged and posted to the `#email-problems` Slack channel.
75
+
76
+ Register it in production only, since test suites routinely address mail to `example.com`:
77
+
78
+ ```ruby
79
+ # config/environments/production.rb
80
+ config.action_mailer.interceptors = %w[Usps::Support::BlockedDomainInterceptor]
81
+ ```
82
+
83
+ The Slack notice needs `SLACK_URL_EMAIL_PROBLEMS` (or `credentials.slack.email_problems`) in the app's production
84
+ environment. Without it, the interception still happens and is logged; only the Slack post is skipped, with a warning.
@@ -183,6 +183,26 @@ ActiveRecord::Schema[8.1].define do
183
183
  t.index ["id"], name: "id_index"
184
184
  end
185
185
 
186
+ create_table "mbrall", primary_key: "certno", id: { type: :string, limit: 7 }, charset: "utf8mb4", comment: "dtb-all certificate numbers", force: :cascade do |t|
187
+ t.string "first", limit: 15
188
+ t.string "last", limit: 25
189
+ t.string "rank", limit: 3
190
+ t.string "pastrank", limit: 3
191
+ t.string "grade", limit: 6
192
+ t.string "squad", limit: 4
193
+ t.string "mbrtype", limit: 2
194
+ t.string "mbrstatus", limit: 3
195
+ t.string "mbrdate", limit: 8
196
+ t.string "city", limit: 30
197
+ t.string "st", limit: 2
198
+ t.string "died", limit: 8
199
+ t.string "lastbdu", limit: 8
200
+ t.string "lasthq", limit: 8
201
+ t.string "mm", limit: 2
202
+ t.string "lastmm", limit: 4
203
+ t.index ["certno"], name: "certno", unique: true
204
+ end
205
+
186
206
  create_table "njdesc", primary_key: "jobcode", id: { type: :string, limit: 7 }, charset: "latin1", comment: "dtb-created daily assigned active nat job desc", force: :cascade do |t|
187
207
  t.string "jdesc", limit: 100
188
208
  t.string "rank", limit: 3, null: false
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'slack_notification'
4
+
5
+ module Usps::Support
6
+ # ActionMailer interceptor that drops recipients at domains known never to accept mail (placeholder
7
+ # addresses entered for members with no real email), so a send never goes out to them and bounces.
8
+ #
9
+ # Only the to/cc/bcc fields that actually held a blocked address are rewritten; the rest of the
10
+ # message is left untouched. If no recipients remain, delivery is suppressed. Either way, a notice is
11
+ # logged and posted to the #email-problems Slack channel.
12
+ #
13
+ # Register it in production only, from config/environments/production.rb:
14
+ #
15
+ # config.action_mailer.interceptors = %w[Usps::Support::BlockedDomainInterceptor]
16
+ #
17
+ # Test suites routinely address mail to example.com, so registering it in every environment would
18
+ # silently empty ActionMailer::Base.deliveries.
19
+ #
20
+ class BlockedDomainInterceptor
21
+ # Subdomains of these are blocked too.
22
+ BLOCKED_DOMAINS = %w[abcboating.com abcemail.com example.com].freeze
23
+
24
+ RECIPIENT_FIELDS = %i[to cc bcc].freeze
25
+ SLACK_CHANNEL = 'email-problems'
26
+
27
+ def self.delivering_email(message) = new(message).intercept
28
+
29
+ # Whether a bare email address (no display name) is at a blocked domain or one of its subdomains.
30
+ def self.blocked?(address)
31
+ domain = address.to_s.split('@').last.to_s.downcase
32
+ BLOCKED_DOMAINS.any? { domain == it || domain.end_with?(".#{it}") }
33
+ end
34
+
35
+ def initialize(message)
36
+ @message = message
37
+ end
38
+
39
+ def intercept
40
+ blocked = RECIPIENT_FIELDS.flat_map { remove_blocked(it) }
41
+ return if blocked.empty?
42
+
43
+ @message.perform_deliveries = false if no_recipients?
44
+ report(blocked)
45
+ end
46
+
47
+ private
48
+
49
+ # Rewrites the field from the formatted addresses it keeps, so display names survive.
50
+ def remove_blocked(field)
51
+ blocked, kept = addresses(field).partition { self.class.blocked?(it.address) }
52
+ @message.public_send(:"#{field}=", kept.empty? ? nil : kept.map(&:format)) if blocked.any?
53
+ blocked.map(&:address)
54
+ end
55
+
56
+ # A header that failed to parse as addresses carries no address list; leave it for delivery to judge.
57
+ def addresses(field)
58
+ header = @message[field]
59
+ header.respond_to?(:addrs) ? header.addrs : []
60
+ end
61
+
62
+ def no_recipients? = RECIPIENT_FIELDS.all? { Array(@message.public_send(it)).empty? }
63
+
64
+ def report(blocked)
65
+ Rails.logger.info("[#{self.class.name}] #{summary(blocked)}")
66
+ notify_slack(blocked)
67
+ end
68
+
69
+ def notify_slack(blocked)
70
+ SlackNotification.new(
71
+ channel: SLACK_CHANNEL, type: :info, title: 'Blocked Email Domain', fallback: summary(blocked),
72
+ fields: [
73
+ { title: 'Application', value: application_name, short: true },
74
+ { title: 'Delivery', value: delivery_status, short: true },
75
+ { title: 'Subject', value: @message.subject.to_s, short: false },
76
+ { title: 'Blocked Recipients', value: blocked.join("\n"), short: false }
77
+ ]
78
+ ).notify!
79
+ rescue StandardError => e
80
+ # Best effort: a Slack outage or a missing channel URL must never block the delivery itself.
81
+ Rails.logger.warn("[#{self.class.name}] Slack notification failed: #{e.class}: #{e.message}")
82
+ end
83
+
84
+ def summary(blocked)
85
+ "#{application_name}: removed #{blocked.join(', ')} from \"#{@message.subject}\"; #{delivery_status.downcase}"
86
+ end
87
+
88
+ def delivery_status
89
+ @message.perform_deliveries ? 'Sent to remaining recipients' : 'Suppressed (no recipients left)'
90
+ end
91
+
92
+ def application_name = Rails.application.class.module_parent_name.titleize
93
+ end
94
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps::Support::Models::Hq::Members
4
+ # Every certificate HQ has issued, current or not. `mbftp` (Member) holds current members only,
5
+ # so a certificate found here and not there belongs to someone whose membership has lapsed.
6
+ #
7
+ # `mbrtype` carries two values `mbftp` never does: `XX` for a member who has died, and `SX` for a
8
+ # certificate that was deactivated.
9
+ #
10
+ class AllMember < BaseRecord
11
+ self.table_name = :mbrall
12
+ self.primary_key = :certno
13
+
14
+ alias_attribute :certificate, :certno
15
+ end
16
+ end
@@ -5,6 +5,7 @@
5
5
  module Usps::Support::Models::Hq::Members; end
6
6
 
7
7
  require_relative 'members/base_record'
8
+ require_relative 'members/all_member'
8
9
  require_relative 'members/associate_member'
9
10
  require_relative 'members/district_job_description'
10
11
  require_relative 'members/district_job'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Support
5
- VERSION = '0.2.66'
5
+ VERSION = '0.2.68'
6
6
  end
7
7
  end
data/lib/usps/support.rb CHANGED
@@ -20,6 +20,7 @@ require_relative 'support/models'
20
20
  require_relative 'support/lib'
21
21
  require_relative 'support/health_diagnostics'
22
22
  require_relative 'support/health_monitor'
23
+ require_relative 'support/blocked_domain_interceptor'
23
24
 
24
25
  # The Engine require lives in `usps/all`, not here. This file can
25
26
  # legitimately be required before Rails is loaded (e.g. from .simplecov to pull
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.66
4
+ version: 0.2.68
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -37,6 +37,26 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '1'
40
+ - !ruby/object:Gem::Dependency
41
+ name: slack-notification
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.1'
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.1.18
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '0.1'
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.1.18
40
60
  - !ruby/object:Gem::Dependency
41
61
  name: usps-imis-api
42
62
  requirement: !ruby/object:Gem::Requirement
@@ -96,6 +116,7 @@ files:
96
116
  - db/hq/websites_schema.rb
97
117
  - lib/usps/all.rb
98
118
  - lib/usps/support.rb
119
+ - lib/usps/support/blocked_domain_interceptor.rb
99
120
  - lib/usps/support/engine.rb
100
121
  - lib/usps/support/health_diagnostics.rb
101
122
  - lib/usps/support/health_monitor.rb
@@ -115,6 +136,7 @@ files:
115
136
  - lib/usps/support/models/git_hub.rb
116
137
  - lib/usps/support/models/hq.rb
117
138
  - lib/usps/support/models/hq/members.rb
139
+ - lib/usps/support/models/hq/members/all_member.rb
118
140
  - lib/usps/support/models/hq/members/associate_member.rb
119
141
  - lib/usps/support/models/hq/members/base_record.rb
120
142
  - lib/usps/support/models/hq/members/district_job.rb
@@ -158,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
180
  - !ruby/object:Gem::Version
159
181
  version: '0'
160
182
  requirements: []
161
- rubygems_version: 3.7.2
183
+ rubygems_version: 3.6.9
162
184
  specification_version: 4
163
185
  summary: USPS Rails Support
164
186
  test_files: []