Class: Nuntius::BaseMessenger

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
app/messengers/nuntius/base_messenger.rb

Overview

Messengers select templates, can manipulate them and

Direct Known Subclasses

CustomMessenger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, event, params = {}) ⇒ BaseMessenger

Returns a new instance of BaseMessenger.



15
16
17
18
19
20
# File 'app/messengers/nuntius/base_messenger.rb', line 15

def initialize(object, event, params = {})
  @object = object
  @event = event
  @params = params
  @attachments = params.fetch(:attachments, [])
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



12
13
14
# File 'app/messengers/nuntius/base_messenger.rb', line 12

def attachments
  @attachments
end

#eventObject (readonly)

Returns the value of attribute event.



12
13
14
# File 'app/messengers/nuntius/base_messenger.rb', line 12

def event
  @event
end

#objectObject (readonly)

Returns the value of attribute object.



12
13
14
# File 'app/messengers/nuntius/base_messenger.rb', line 12

def object
  @object
end

#paramsObject (readonly)

Returns the value of attribute params.



12
13
14
# File 'app/messengers/nuntius/base_messenger.rb', line 12

def params
  @params
end

#templatesObject

Returns the value of attribute templates.



13
14
15
# File 'app/messengers/nuntius/base_messenger.rb', line 13

def templates
  @templates
end

Class Method Details

.class_name_for(obj) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/messengers/nuntius/base_messenger.rb', line 73

def class_name_for(obj)
  case obj
  when Array, ActiveRecord::Relation
    obj.first.class.name.demodulize
  when Hash
    "Custom"
  when Class
    obj.name.demodulize
  else
    obj.class.name
  end
end

.class_names_for(obj) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'app/messengers/nuntius/base_messenger.rb', line 86

def class_names_for(obj)
  main_class_name = class_name_for(obj)

  return [main_class_name] if !obj.class.respond_to?(:base_class?) || obj.class.base_class?

  list = [main_class_name]
  list << obj.class.base_class.name
  list
end

.event_name_for(obj, event) ⇒ Object



96
97
98
99
100
101
102
# File 'app/messengers/nuntius/base_messenger.rb', line 96

def event_name_for(obj, event)
  if obj.is_a?(Hash)
    "#{obj.keys.first}##{event}"
  else
    event
  end
end

.liquid_variable_name_for(obj) ⇒ String

Returns the variable name used in the liquid context

Parameters:

  • obj (Object)

    Any object with a backing drop

Returns:

  • (String)

    underscored, lowercase string



61
62
63
64
65
66
67
68
69
70
71
# File 'app/messengers/nuntius/base_messenger.rb', line 61

def liquid_variable_name_for(obj)
  return obj.keys.first.to_s if obj.is_a?(Hash)

  plural = obj.is_a?(Array) || obj.is_a?(ActiveRecord::Relation)
  list = plural ? obj : [obj]
  klass = list.first.class
  klass = klass.base_class if klass.respond_to?(:base_class)
  name = klass.name.demodulize
  name = name.pluralize if plural
  name.underscore
end

.locale(locale = nil) ⇒ Object



133
134
135
136
# File 'app/messengers/nuntius/base_messenger.rb', line 133

def locale(locale = nil)
  @locale = locale if locale
  @locale
end

.messenger_for_class(name) ⇒ Object



104
105
106
107
108
109
# File 'app/messengers/nuntius/base_messenger.rb', line 104

def messenger_for_class(name)
  klass = messenger_name_for_class(name).safe_constantize
  klass ||= messenger_name_for_class(name.safe_constantize.superclass).safe_constantize
  klass ||= messenger_name_for_class(name.safe_constantize.superclass.superclass).safe_constantize
  klass
end

.messenger_for_obj(obj) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/messengers/nuntius/base_messenger.rb', line 115

def messenger_for_obj(obj)
  return Nuntius::CustomMessenger if obj.is_a? Hash

  klass = messenger_name_for_obj(obj).safe_constantize

  # Lets check 2 levels above to see if a messager exists for a possible super class (think STI)
  klass ||= messenger_name_for_obj(obj.class.superclass).safe_constantize
  klass ||= messenger_name_for_obj(obj.class.superclass.superclass).safe_constantize

  raise Nuntius::MissingMessengerException.new(self), "messenger missing for #{obj.class.name}" unless klass

  klass
end

.messenger_name_for_class(name) ⇒ Object



111
112
113
# File 'app/messengers/nuntius/base_messenger.rb', line 111

def messenger_name_for_class(name)
  "#{name}Messenger"
end

.messenger_name_for_obj(obj) ⇒ Object



129
130
131
# File 'app/messengers/nuntius/base_messenger.rb', line 129

def messenger_name_for_obj(obj)
  "#{class_name_for(obj)}Messenger"
end

.template_scope(template_scope = nil) ⇒ Object



138
139
140
141
# File 'app/messengers/nuntius/base_messenger.rb', line 138

def template_scope(template_scope = nil)
  @template_scope = template_scope if template_scope
  @template_scope
end

.timebased_scope(name, &scope_proc) ⇒ Object

Raises:

  • (ArgumentError)


147
148
149
150
151
152
153
154
# File 'app/messengers/nuntius/base_messenger.rb', line 147

def timebased_scope(name, &scope_proc)
  raise ArgumentError, "timebased_scope must start with before or after" unless %w[before after].detect { |prefix| name.to_s.start_with?(prefix) }

  name = name.to_sym
  timebased_scopes[name] = scope_proc if scope_proc.present?
  define_method(name) { |object, params = {}| } unless respond_to?(name)
  timebased_scopes[name] || nil
end

.timebased_scope_for(template) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
# File 'app/messengers/nuntius/base_messenger.rb', line 156

def timebased_scope_for(template)
  return [] unless timebased_scopes.include?(template.event.to_sym)

  timebased_scope(template.event)
    .call(template.interval_time_range, template.)
    .where("#{template.klass.constantize.table_name}.created_at > ?", template.created_at)
    .where.not(
      id: Nuntius::Message.select(:nuntiable_id)
                          .where(template_id: template.id)
    )
end

.timebased_scopesObject



143
144
145
# File 'app/messengers/nuntius/base_messenger.rb', line 143

def timebased_scopes
  @timebased_scopes ||= {}
end

Instance Method Details

#attach(attachment) ⇒ Object



49
50
51
# File 'app/messengers/nuntius/base_messenger.rb', line 49

def attach(attachment)
  @attachments << attachment
end

#callObject

Calls the event method on the messenger



23
24
25
26
27
28
# File 'app/messengers/nuntius/base_messenger.rb', line 23

def call
  select_templates
  run_callbacks(:action) do
    send(@event.to_sym, @object, @params) if respond_to?(@event.to_sym)
  end
end

#dispatch(filtered_templates) ⇒ Object

Turns the templates in messages, and dispatches the messages to transports



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/messengers/nuntius/base_messenger.rb', line 31

def dispatch(filtered_templates)
  filtered_templates.each do |template|
    template.layout = override_layout(template.layout)
    msg = template.new_message(@object, liquid_context, params)
    @attachments.each do |attachment|
      msg.add_attachment(attachment)
    end

    transport = Nuntius::BaseTransport.class_from_name(template.transport).new
    transport.deliver(msg) if msg.to.present?
  end
end

#override_layout(selected_layout) ⇒ Object

Allow messengers to override the selected layout



45
46
47
# File 'app/messengers/nuntius/base_messenger.rb', line 45

def override_layout(selected_layout)
  selected_layout
end