Module: NotificationHub

Defined in:
lib/notification_hub.rb,
lib/notification_hub/version.rb,
lib/notification_hub/channels/sms.rb,
lib/notification_hub/channels/email.rb,
lib/notification_hub/device_manager.rb,
lib/notification_hub/channels/sms/aws.rb,
lib/notification_hub/channels/webhook.rb,
lib/notification_hub/channels/sms/base.rb,
lib/notification_hub/channels/email/base.rb,
lib/notification_hub/subscription_manager.rb,
lib/notification_hub/channels/webhook/base.rb,
lib/generators/notification_hub/job_generator.rb,
lib/notification_hub/channels/webhook/httparty.rb,
lib/notification_hub/channels/email/action_mailer.rb,
lib/notification_hub/channels/mobile_push_notification.rb,
lib/notification_hub/channels/browser_push_notification.rb,
lib/generators/notification_hub/notification_hub_generator.rb,
lib/notification_hub/channels/mobile_push_notification/fcm.rb,
lib/notification_hub/channels/browser_push_notification/fcm.rb,
lib/notification_hub/channels/mobile_push_notification/base.rb,
lib/notification_hub/channels/browser_push_notification/base.rb

Defined Under Namespace

Modules: Channels, DeviceManager, Generators, SubscriptionManager

Constant Summary collapse

VERSION =
"0.1.8"

Class Method Summary collapse

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
# 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) ⇒ Object



36
37
38
39
40
41
42
# 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) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object



44
45
46
# 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) ⇒ Object



32
33
34
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



24
25
26
27
28
29
30
# 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