Class: Emailbutler::Webhooks::Mappers::Sendgrid

Inherits:
Object
  • Object
show all
Defined in:
lib/emailbutler/webhooks/mappers/sendgrid.rb

Constant Summary collapse

DELIVERABILITY_MAPPER =
{
  'processed' => 'processed',
  'delivered' => 'delivered',
  'open' => 'delivered',
  'click' => 'delivered'
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(payload:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/emailbutler/webhooks/mappers/sendgrid.rb', line 14

def call(payload:)
  payload['_json'].filter_map { |message|
    message.stringify_keys!
    message_uuid = message['smtp-id'] || message['sg_message_id']
    status = DELIVERABILITY_MAPPER[message['event']] || Emailbutler::Message::FAILED
    next if message_uuid.nil? || status.nil?

    {
      message_uuid: message_uuid,
      status: status,
      timestamp: message['timestamp'] ? Time.at(message['timestamp'].to_i).utc.to_datetime : nil
    }
  }
end