Class: Pdm::Rails::Delivery

Inherits:
Object
  • Object
show all
Defined in:
lib/pdm-rails/delivery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Delivery

Returns a new instance of Delivery.



10
11
12
13
# File 'lib/pdm-rails/delivery.rb', line 10

def initialize(settings = {})
  @settings = settings.dup
  @fallback = @settings.delete(:fallback)
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



7
8
9
# File 'lib/pdm-rails/delivery.rb', line 7

def fallback
  @fallback
end

#settingsObject

Returns the value of attribute settings.



8
9
10
# File 'lib/pdm-rails/delivery.rb', line 8

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pdm-rails/delivery.rb', line 29

def deliver!(mail)
  pdm_mail = Pdm::Mail.new(mail, settings)
  
  begin
    if (res = pdm_mail.deliver)
      case res
      when Net::HTTPSuccess
        logger.debug("Delivered #{mail.subject.inspect} to #{mail.to.inspect} " +
                     "via PDM #{pdm_mail.guid}.")
        return mail
      else
        logger.error("Unable to deliver mail via PDM: Got response #{res.inspect}.")
      end
    else
      logger.error("Unable to deliver mail via PDM. No response? :-(")
    end
  rescue Pdm::Error => e
    logger.error("Got an error talking to PDM: " +
                 [e.class.name, e.message].join(": "))
    raise(e) if @fallback.nil?
  end

  if @fallback
    deliver_without_pdm!(mail)
  else
    e = RuntimeError.new("No success from PDM. No fallback possible.")
    Pdm::Error.extend_and_raise(e)
  end
end

#deliver_without_pdm!(mail) ⇒ Object



24
25
26
27
# File 'lib/pdm-rails/delivery.rb', line 24

def deliver_without_pdm!(mail)
  ActionMailer::Base.wrap_delivery_behavior(mail, @fallback)
  mail.deliver!
end

#loggerObject



16
17
18
# File 'lib/pdm-rails/delivery.rb', line 16

def logger
  ::Rails.logger
end

#personalization?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/pdm-rails/delivery.rb', line 20

def personalization?
  settings[:personalization] == true
end