Reflect new bounced mail structure

This commit is contained in:
Karl Erik Õunapuu 2020-09-21 13:47:57 +03:00
parent 659cb7f4e6
commit 98674ab381
No known key found for this signature in database
GPG key ID: C9DD647298A34764
2 changed files with 26 additions and 28 deletions

View file

@ -1,6 +1,5 @@
class BouncedMailAddress < ApplicationRecord
validates :email, presence: true
validates :bounce_reason, :recipient_json, :response_json, presence: true
validates :email, :message_id, :bounce_type, :bounce_subtype, :action, :status, presence: true
before_validation :assign_bounce_reason
def assign_bounce_reason
@ -9,31 +8,24 @@ class BouncedMailAddress < ApplicationRecord
self.bounce_reason = "#{action} (#{status} #{diagnostic})"
end
def diagnostic
return unless recipient_json
recipient_json['diagnosticCode']
end
def action
return unless recipient_json
recipient_json['action']
end
def status
return unless recipient_json
recipient_json['status']
end
def self.record(json)
bounced_records = json['bounce']['bouncedRecipients']
bounced_records.each do |record|
bounce_record = BouncedMailAddress.new(email: record['emailAddress'], recipient_json: record,
response_json: json)
bounce_record = BouncedMailAddress.new(params_from_json(json, record))
bounce_record.save
end
end
def params_from_json(json, bounced_record)
{
email: bounced_record['emailAddress'],
message_id: json['mail']['messageId'],
bounce_type: json['bounce']['bounceType'],
bounce_subtype: json['bounce']['bounceSubType'],
action: bounced_record['action'],
status: bounced_record['status'],
diagnostic: bounced_record['diagnosticCode'],
}
end
end

View file

@ -1,20 +1,26 @@
<p>
<strong>Email:</strong>
<%= @bounced_mail_address.email %>
</p>
<p>
<strong>Bounce reason:</strong>
<%= @bounced_mail_address.bounce_reason %>
<strong>Bounced message ID:</strong>
<%= @bounced_mail_address.message_id %>
</p>
<p>
<strong>Bounced recipient JSON:</strong>
<pre><%= JSON.pretty_generate(@bounced_mail_address.recipient_json) %></pre>
<strong>Overall bounce type:</strong>
<%= @bounced_mail_address.bounce_type %> (<%= @bounced_mail_address.bounce_subtype %> )
</p>
<p>
<strong>Bounce payload:</strong>
<pre><%= JSON.pretty_generate(@bounced_mail_address.response_json) %></pre>
<strong>Bounced recipient status:</strong>
<%= @bounced_mail_address.action %> (<%= @bounced_mail_address.status %>)
</p>
<p>
<strong>Bounced recipient diagnostic:</strong>
<pre><%= @bounced_mail_address.diagnostic %></pre>
</p>
<%= link_to 'Back', admin_bounced_mail_addresses_path %>