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.4"

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, options = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/notification_hub.rb', line 36

def deliver(association_model_id, event_code, data, options=nil)   
  if NotificationHubJob.respond_to?("perform_later".to_sym)   
    NotificationHubJob.perform_later(association_model_id, event_code, data, options)      
  elsif NotificationHubJob.respond_to?("perform_async".to_sym)
    NotificationHubJob.perform_async(association_model_id, event_code, data, options)    
  end
end

.deliver_now(association_model_id, event_code, data, options = nil) ⇒ Object



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

def deliver_now(association_model_id, event_code, data, options=nil)
  send_message(association_model_id, event_code, data, options)      
end

.send_direct(event_code, data, device_details, channel_code, gateway_code = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/notification_hub.rb', line 77

def send_direct(event_code, data, device_details, channel_code, gateway_code=nil)   
  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
    e.backtrace.each { |line| NotificationHub.logger.error line }
  end     
end

.send_message(association_model_id, event_code, data_wrapper, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/notification_hub.rb', line 44

def send_message(association_model_id, event_code, data_wrapper, options)       
  # 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
        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