Class: Pushing::Base

Inherits:
AbstractController::Base
  • Object
show all
Includes:
AbstractController::AssetPaths, AbstractController::Callbacks, AbstractController::Helpers, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, ActionView::Rendering, Rescuable
Defined in:
lib/pushing/base.rb

Defined Under Namespace

Classes: NullNotification

Constant Summary collapse

PROTECTED_IVARS =
AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + [:@_action_has_layout]
@@delivery_notification_observers =
[]
@@delivery_interceptors =
[]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rescuable

#handle_exceptions

Class Attribute Details

.notifier_nameObject Also known as: controller_path



90
91
92
# File 'lib/pushing/base.rb', line 90

def notifier_name
  @notifier_name ||= anonymous? ? "anonymous" : name.underscore
end

Class Method Details

.deliver_notification(notification) ⇒ Object

Wraps a notification delivery inside of ActiveSupport::Notifications instrumentation.



98
99
100
101
102
103
# File 'lib/pushing/base.rb', line 98

def deliver_notification(notification) #:nodoc:
  ActiveSupport::Notifications.instrument("deliver.push_notification") do |payload|
    set_payload_for_notification(payload, notification)
    yield # Let NotificationDelivery do the delivery actions
  end
end

.inform_interceptors(notification) ⇒ Object



84
85
86
87
88
# File 'lib/pushing/base.rb', line 84

def inform_interceptors(notification)
  delivery_interceptors.each do |interceptor|
    interceptor.delivering_notification(notification)
  end
end

.inform_observers(notification, response) ⇒ Object



78
79
80
81
82
# File 'lib/pushing/base.rb', line 78

def inform_observers(notification, response)
  delivery_notification_observers.each do |observer|
    observer.delivered_notification(notification, response)
  end
end

.register_interceptor(interceptor) ⇒ Object

Register an Interceptor which will be called before notification is sent. Either a class, string or symbol can be passed in as the Interceptor. If a string or symbol is passed in it will be camelized and constantized.



72
73
74
75
76
# File 'lib/pushing/base.rb', line 72

def register_interceptor(interceptor)
  unless delivery_interceptors.include?(interceptor)
    delivery_interceptors << interceptor
  end
end

.register_interceptors(*interceptors) ⇒ Object

Register one or more Interceptors which will be called before notification is sent.



56
57
58
# File 'lib/pushing/base.rb', line 56

def register_interceptors(*interceptors)
  interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) }
end

.register_observer(observer) ⇒ Object

Register an Observer which will be notified when notification is delivered. Either a class, string or symbol can be passed in as the Observer. If a string or symbol is passed in it will be camelized and constantized.



63
64
65
66
67
# File 'lib/pushing/base.rb', line 63

def register_observer(observer)
  unless delivery_notification_observers.include?(observer)
    delivery_notification_observers << observer
  end
end

.register_observers(*observers) ⇒ Object

Register one or more Observers which will be notified when notification is delivered.



51
52
53
# File 'lib/pushing/base.rb', line 51

def register_observers(*observers)
  observers.flatten.compact.each { |observer| register_observer(observer) }
end

.supports_path?Boolean

Push notifications do not support relative path links.

Returns:

  • (Boolean)


106
107
108
# File 'lib/pushing/base.rb', line 106

def supports_path? # :doc:
  false
end

Instance Method Details

#_protected_ivarsObject

:nodoc:



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

def _protected_ivars # :nodoc:
  PROTECTED_IVARS
end

#process(method_name, *args) ⇒ Object

:nodoc:



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pushing/base.rb', line 130

def process(method_name, *args) #:nodoc:
  payload = {
    notifier: self.class.name,
    action: method_name,
    args: args
  }

  ActiveSupport::Notifications.instrument("process.push_notification", payload) do
    super
    @_notification ||= NullNotification.new
  end
end

#push(headers) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pushing/base.rb', line 155

def push(headers)
  return notification if notification && headers.blank?

  payload = {}
  ::Pushing::Platforms.config.select {|platform, _| headers[platform] }.each do |platform, config|
    payload_class = ::Pushing::Platforms.lookup(platform)

    if payload_class.should_render?(headers[platform])
      json = render_json(platform, headers)
      payload[platform] = payload_class.new(json, headers[platform], config)
    end
  end

  # TODO: Do not use OpenStruct
  @_notification = OpenStruct.new(payload)
end