Class: ActiveNotifier::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_notifier/base.rb

Defined Under Namespace

Classes: Configuration

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
# File 'lib/active_notifier/base.rb', line 5

def initialize(attributes={})
  attributes.each do |(key, value)|
    self.public_send("#{key}=", value)
  end
end

Class Attribute Details

.configurationsObject

Returns the value of attribute configurations.



32
33
34
# File 'lib/active_notifier/base.rb', line 32

def configurations
  @configurations
end

.locale_attributeObject

Returns the value of attribute locale_attribute.



32
33
34
# File 'lib/active_notifier/base.rb', line 32

def locale_attribute
  @locale_attribute
end

Instance Attribute Details

#channelsObject

Returns the value of attribute channels.



3
4
5
# File 'lib/active_notifier/base.rb', line 3

def channels
  @channels
end

#recipientObject

Returns the value of attribute recipient.



3
4
5
# File 'lib/active_notifier/base.rb', line 3

def recipient
  @recipient
end

Class Method Details

.deliver_now(attributes) ⇒ Object



34
35
36
# File 'lib/active_notifier/base.rb', line 34

def deliver_now(attributes)
  new(attributes).deliver_now
end

.deliver_through(channel, &blk) ⇒ Object



38
39
40
41
42
# File 'lib/active_notifier/base.rb', line 38

def deliver_through(channel, &blk)
  self.configurations ||= {}
  config = Configuration.new.tap(&blk)
  configurations[channel] = config
end

.locale_for(recipient) ⇒ Object



44
45
46
47
48
# File 'lib/active_notifier/base.rb', line 44

def locale_for(recipient)
  recipient.public_send(locale_attribute)
rescue
  I18n.default_locale
end

Instance Method Details

#deliver_nowObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_notifier/base.rb', line 13

def deliver_now
  delivered = false
  locale = self.class.locale_for(recipient)
  I18n.with_locale(locale) do
    until channels.empty? || delivered
      channel = channels.shift
      begin
        deliverable(channel).deliver_now
        delivered = true
      rescue ActiveNotifier::DeliveryImpossible => e
        delivered = false
        msg = "Unable to deliver to channel #{channel}: #{e.message}"
        ActiveNotifier.logger && ActiveNotifier.logger.warn(msg)
      end
    end
  end
end