module NotificationHub
Constants
- VERSION
Public Class Methods
configure() { |self| ... }
click to toggle source
# File lib/notification_hub.rb, line 18 def configure @@association_model ||= "user" @@logger = Logger.new("#{Rails.root}/log/notification_hub.log") yield self end
deliver(association_model_id, event_code, data)
click to toggle source
# File lib/notification_hub.rb, line 36 def deliver(association_model_id, event_code, data) if NotificationHubJob.respond_to?("perform_later".to_sym) NotificationHubJob.perform_later("deliver", association_model_id, event_code, data) elsif NotificationHubJob.respond_to?("perform_async".to_sym) NotificationHubJob.perform_async("deliver", association_model_id, event_code, data) end end
deliver_global(event_code, data, device_details, channel_code, gateway_code=nil)
click to toggle source
# File lib/notification_hub.rb, line 48 def deliver_global(event_code, data, device_details, channel_code, gateway_code=nil) if NotificationHubJob.respond_to?("perform_later".to_sym) NotificationHubJob.perform_later("deliver_global", nil, event_code, data, device_details, channel_code, gateway_code) elsif NotificationHubJob.respond_to?("perform_async".to_sym) NotificationHubJob.perform_async("deliver_global", nil, event_code, data, device_details, channel_code, gateway_code) end end
deliver_global_now(event_code, data, device_details, channel_code, gateway_code=nil)
click to toggle source
# File lib/notification_hub.rb, line 44 def deliver_global_now(event_code, data, device_details, channel_code, gateway_code=nil) send_global_message(event_code, data, device_details, channel_code, gateway_code) end
deliver_now(association_model_id, event_code, data)
click to toggle source
# File lib/notification_hub.rb, line 32 def deliver_now(association_model_id, event_code, data) send_message(association_model_id, event_code, data) end
send_global_message(event_code, data_wrapper, device_details, channel_code, gateway_code=nil)
click to toggle source
# File lib/notification_hub.rb, line 96 def send_global_message(event_code, data_wrapper, device_details, channel_code, gateway_code=nil) # Fetch data from the database if record id is passed. data = {} data_wrapper.each do |key,value| if value.is_a?(Integer) data[key.to_sym] = key.to_s.classify.constantize.find_by_id(value) else data[key.to_sym] = value end end device_details = device_details.try :symbolize_keys if gateway_code.present? channel_const = "NotificationHub::Channels::#{channel_code.camelize}::#{gateway_code.camelize}".constantize else channel_const = "NotificationHub::Channels::#{channel_code.camelize}".constantize end begin channel_const.send_message(event_code, data, device_details) rescue => e NotificationHub.logger.error e.message NotificationHub.logger.error "Event:#{event_code} Channel:#{channel_code} Device:#{device_details.to_json}" e.backtrace.each { |line| NotificationHub.logger.error line } end end
send_message(association_model_id, event_code, data_wrapper)
click to toggle source
# File lib/notification_hub.rb, line 56 def send_message(association_model_id, event_code, data_wrapper) unless NotificationHub.events.keys.include? event_code NotificationHub.logger.error "Event code is not registered" NotificationHub.logger.error "Event:#{event_code}" raise "Event code is not registered" end # Fetch data from the database if record id is passed. data = {} data_wrapper.each do |key,value| if value.is_a?(Integer) data[key.to_sym] = key.to_s.classify.constantize.find_by_id(value) else data[key.to_sym] = value end end # Query subsccriptions for the relevant event. susbcriptions = NotificationHub::Subscription.where("#{association_model}_id" => association_model_id, event_code: event_code) susbcriptions.each do |susbcription| if susbcription.gateway_code.present? channel_const = "NotificationHub::Channels::#{susbcription.channel_code.camelize}::#{susbcription.gateway_code.camelize}".constantize else channel_const = "NotificationHub::Channels::#{susbcription.channel_code.camelize}".constantize end susbcription.notification_hub_devices.try(:each) do |device| device_details = device.attributes.symbolize_keys begin channel_const.send_message(event_code, data, device_details) rescue => e NotificationHub.logger.error e.message NotificationHub.logger.error "Event:#{event_code} Channel:#{susbcription.channel_code} Subscription: #{susbcription.id} Device:#{device.id}" e.backtrace.each { |line| NotificationHub.logger.error line } end end end end
set_channel(*args)
click to toggle source
# File lib/notification_hub.rb, line 24 def set_channel(*args) channel = args[0] options = args[1] gateway = options[:gateway] "NotificationHub::Channels::#{channel.to_s.camelize}::#{gateway.to_s.camelize}".constantize.new(options) end