Class: WhatsAppNotifier::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp_notifier/notification.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Notification

Returns a new instance of Notification.



43
44
45
# File 'lib/whatsapp_notifier/notification.rb', line 43

def initialize(params = {})
  @params = params
end

Class Attribute Details

.default_providerObject

Returns the value of attribute default_provider.



4
5
6
# File 'lib/whatsapp_notifier/notification.rb', line 4

def default_provider
  @default_provider
end

.default_templateObject

Returns the value of attribute default_template.



4
5
6
# File 'lib/whatsapp_notifier/notification.rb', line 4

def default_template
  @default_template
end

.default_toObject

Returns the value of attribute default_to.



4
5
6
# File 'lib/whatsapp_notifier/notification.rb', line 4

def default_to
  @default_to
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



41
42
43
# File 'lib/whatsapp_notifier/notification.rb', line 41

def params
  @params
end

Class Method Details

.deliver_later(params = {}) ⇒ Object



36
37
38
# File 'lib/whatsapp_notifier/notification.rb', line 36

def deliver_later(params = {})
  Jobs::SendMessageJob.perform_later(name, params)
end

.deliver_now(params = {}) ⇒ Object



32
33
34
# File 'lib/whatsapp_notifier/notification.rb', line 32

def deliver_now(params = {})
  with(params).deliver_now
end

.provider(value = nil) ⇒ Object



15
16
17
18
# File 'lib/whatsapp_notifier/notification.rb', line 15

def provider(value = nil)
  return default_provider if value.nil?
  self.default_provider = value
end

.template(name = nil, body = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/whatsapp_notifier/notification.rb', line 20

def template(name = nil, body = nil)
  @templates ||= {}
  return default_template if name.nil?

  self.default_template = name.to_sym
  @templates[name.to_sym] = body if body
end

.templatesObject



28
29
30
# File 'lib/whatsapp_notifier/notification.rb', line 28

def templates
  @templates ||= {}
end

.to(value = nil) ⇒ Object



10
11
12
13
# File 'lib/whatsapp_notifier/notification.rb', line 10

def to(value = nil)
  return default_to if value.nil?
  self.default_to = value
end

.with(params = {}) ⇒ Object



6
7
8
# File 'lib/whatsapp_notifier/notification.rb', line 6

def with(params = {})
  new(params)
end

Instance Method Details

#deliver_nowObject

Raises:



73
74
75
76
77
78
79
80
81
82
# File 'lib/whatsapp_notifier/notification.rb', line 73

def deliver_now
  raise ConfigurationError, "recipient is required" if to.nil? || to.to_s.strip.empty?

  WhatsAppNotifier.deliver(
    to: to,
    body: message,
    provider: provider,
    metadata: 
  )
end

#messageObject

Raises:

  • (NotImplementedError)


47
48
49
50
51
# File 'lib/whatsapp_notifier/notification.rb', line 47

def message
  return render_template if template_name

  raise NotImplementedError, "#{self.class.name} must implement #message or define template"
end

#metadataObject



61
62
63
# File 'lib/whatsapp_notifier/notification.rb', line 61

def 
  params[:metadata] || {}
end

#providerObject



57
58
59
# File 'lib/whatsapp_notifier/notification.rb', line 57

def provider
  params[:provider] || self.class.default_provider
end

#template_nameObject



65
66
67
# File 'lib/whatsapp_notifier/notification.rb', line 65

def template_name
  (params[:template] || self.class.default_template)&.to_sym
end

#template_paramsObject



69
70
71
# File 'lib/whatsapp_notifier/notification.rb', line 69

def template_params
  params[:params] || {}
end

#toObject



53
54
55
# File 'lib/whatsapp_notifier/notification.rb', line 53

def to
  params[:to] || self.class.default_to
end