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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/messengers/nuntius/base_messenger.rb', line 77

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



90
91
92
93
94
95
96
97
98
# File 'app/messengers/nuntius/base_messenger.rb', line 90

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



100
101
102
103
104
105
106
# File 'app/messengers/nuntius/base_messenger.rb', line 100

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



65
66
67
68
69
70
71
72
73
74
75
# File 'app/messengers/nuntius/base_messenger.rb', line 65

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



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

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

.messenger_for_class(name) ⇒ Object



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

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



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/messengers/nuntius/base_messenger.rb', line 119

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



115
116
117
# File 'app/messengers/nuntius/base_messenger.rb', line 115

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

.messenger_name_for_obj(obj) ⇒ Object



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

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

.template_scope(template_scope = nil) ⇒ Object



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

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

.timebased_scope(name, &scope_proc) ⇒ Object

Raises:

  • (ArgumentError)


151
152
153
154
155
156
157
158
# File 'app/messengers/nuntius/base_messenger.rb', line 151

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



160
161
162
163
164
165
166
167
168
169
170
# File 'app/messengers/nuntius/base_messenger.rb', line 160

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



147
148
149
# File 'app/messengers/nuntius/base_messenger.rb', line 147

def timebased_scopes
  @timebased_scopes ||= {}
end

Instance Method Details

#attach(attachment) ⇒ Object



53
54
55
# File 'app/messengers/nuntius/base_messenger.rb', line 53

def attach(attachment)
  @attachments << attachment
end

#callObject

Calls the event method on the messenger



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

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

#dispatch(filtered_templates) ⇒ Object

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/messengers/nuntius/base_messenger.rb', line 33

def dispatch(filtered_templates)
  filtered_templates.each do |template|
    template.layout = override_layout(template.layout)
    msg = template.new_message(@object, liquid_context, params)
    next unless msg.present?

    @attachments.each do |attachment|
      msg.add_attachment(attachment)
    end

    transport = Nuntius::BaseTransport.class_from_name(template.transport).new
    transport.deliver(msg)
  end
end

#override_layout(selected_layout) ⇒ Object

Allow messengers to override the selected layout



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

def override_layout(selected_layout)
  selected_layout
end