Class: Webhookdb::Message::Delivery

Inherits:
Object
  • Object
show all
Defined in:
lib/webhookdb/message/delivery.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDelivery

Returns a new instance of Delivery.



30
31
32
33
# File 'lib/webhookdb/message/delivery.rb', line 30

def initialize(*)
  super
  self[:extra_fields] ||= {}
end

Class Method Details

.preview(template_class_name, transport: :email, rack_env: Webhookdb::RACK_ENV, commit: false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/webhookdb/message/delivery.rb', line 77

def self.preview(template_class_name, transport: :email, rack_env: Webhookdb::RACK_ENV, commit: false)
  raise "Can only preview in development or commit:false" unless rack_env == "development" || !commit

  Gem.find_files("webhookdb/messages/**/*.rb").each do |path|
    require path
  end

  begin
    template_class = "Webhookdb::Messages::#{template_class_name}".constantize
  rescue NameError
    raise Webhookdb::Message::MissingTemplateError, "Webhookdb::Messages::#{template_class_name} not found"
  end

  require "webhookdb/fixtures"
  Webhookdb::Fixtures.load_all

  delivery = nil
  self.db.transaction(rollback: commit ? nil : :always) do
    to = Webhookdb::Fixtures.customer.create
    template = template_class.fixtured(to)
    delivery = template.dispatch(to, transport:)
    delivery.bodies # Fetch this ahead of time so it is there after rollback
  end
  return delivery
end

Instance Method Details

#body_with_mediatype(mt) ⇒ Object



35
36
37
# File 'lib/webhookdb/message/delivery.rb', line 35

def body_with_mediatype(mt)
  return self.bodies.find { |b| b.mediatype == mt }
end

#body_with_mediatype!(mt) ⇒ Object



39
40
41
42
# File 'lib/webhookdb/message/delivery.rb', line 39

def body_with_mediatype!(mt)
  (b = self.body_with_mediatype(mt)) or raise "Delivery #{self.id} has no body with mediatype #{mt}"
  return b
end

#send!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/webhookdb/message/delivery.rb', line 48

def send!
  return nil if self.sent? || self.soft_deleted?
  self.db.transaction do
    self.lock!
    return nil if self.sent? || self.soft_deleted?
    begin
      transport_message_id = self.transport.send!(self)
    rescue Webhookdb::Message::Transport::UndeliverableRecipient
      self.logger.warn("undeliverable_recipient", recipient: self.to, message_delivery_id: self.id)
      self.soft_delete
      return self
    end
    if transport_message_id.blank?
      self.logger.error("empty_transport_message_id", message_delivery_id: self.id)
      transport_message_id = "WARNING-NOT-SET"
    end
    self.update(transport_message_id:, sent_at: Time.now)
    return self
  end
end

#sent?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/webhookdb/message/delivery.rb', line 44

def sent?
  return self.sent_at ? true : false
end

#transportObject



69
70
71
# File 'lib/webhookdb/message/delivery.rb', line 69

def transport
  return Webhookdb::Message::Transport.for(self.transport_type)
end

#transport!Object



73
74
75
# File 'lib/webhookdb/message/delivery.rb', line 73

def transport!
  return Webhookdb::Message::Transport.for!(self.transport_type)
end