notification_hub 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +105 -0
  6. data/Rakefile +2 -0
  7. data/bin/console +14 -0
  8. data/bin/setup +8 -0
  9. data/lib/generators/active_record/notification_hub_generator.rb +48 -0
  10. data/lib/generators/active_record/templates/migrations/devices.rb +15 -0
  11. data/lib/generators/active_record/templates/migrations/subscription_devices.rb +10 -0
  12. data/lib/generators/active_record/templates/migrations/subscriptions.rb +11 -0
  13. data/lib/generators/active_record/templates/models/device.rb +15 -0
  14. data/lib/generators/active_record/templates/models/subscription.rb +16 -0
  15. data/lib/generators/active_record/templates/models/subscription_device.rb +8 -0
  16. data/lib/generators/notification_hub/job_generator.rb +18 -0
  17. data/lib/generators/notification_hub/notification_hub_generator.rb +25 -0
  18. data/lib/generators/notification_hub/templates/initializer.rb +56 -0
  19. data/lib/generators/notification_hub/templates/jobs/active_job.rb +7 -0
  20. data/lib/generators/notification_hub/templates/jobs/sidekiq_job.rb +8 -0
  21. data/lib/generators/notification_hub/templates/notification_hub/browser_push_notification/estimate/accepted.json.jbuilder +8 -0
  22. data/lib/generators/notification_hub/templates/notification_hub/browser_push_notification/estimate/expired.json.jbuilder +9 -0
  23. data/lib/generators/notification_hub/templates/notification_hub/browser_push_notification/estimate/viewed.json.jbuilder +8 -0
  24. data/lib/generators/notification_hub/templates/notification_hub/browser_push_notification/invoice/overdue.json.jbuilder +8 -0
  25. data/lib/generators/notification_hub/templates/notification_hub/browser_push_notification/invoice/payment_received.json.jbuilder +8 -0
  26. data/lib/generators/notification_hub/templates/notification_hub/browser_push_notification/invoice/unrealized_payment_received.json.jbuilder +9 -0
  27. data/lib/generators/notification_hub/templates/notification_hub/browser_push_notification/invoice/viewed.json.jbuilder +10 -0
  28. data/lib/generators/notification_hub/templates/notification_hub/email/estimate/accepted.html.erb +10 -0
  29. data/lib/generators/notification_hub/templates/notification_hub/email/estimate/accepted.text.erb +9 -0
  30. data/lib/generators/notification_hub/templates/notification_hub/email/estimate/expired.html.erb +9 -0
  31. data/lib/generators/notification_hub/templates/notification_hub/email/estimate/expired.text.erb +9 -0
  32. data/lib/generators/notification_hub/templates/notification_hub/email/estimate/viewed.html.erb +9 -0
  33. data/lib/generators/notification_hub/templates/notification_hub/email/estimate/viewed.text.erb +9 -0
  34. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/overdue.html.erb +9 -0
  35. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/overdue.text.erb +9 -0
  36. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/payment_received.html.erb +11 -0
  37. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/payment_received.text.erb +13 -0
  38. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/unrealized_payment_received.html.erb +10 -0
  39. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/unrealized_payment_received.text.erb +11 -0
  40. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/viewed.html.erb +9 -0
  41. data/lib/generators/notification_hub/templates/notification_hub/email/invoice/viewed.text.erb +9 -0
  42. data/lib/generators/notification_hub/templates/notification_hub/webhook/estimate/accepted.json.jbuilder +6 -0
  43. data/lib/generators/notification_hub/templates/notification_hub/webhook/estimate/expired.json.jbuilder +6 -0
  44. data/lib/generators/notification_hub/templates/notification_hub/webhook/estimate/viewed.json.jbuilder +6 -0
  45. data/lib/generators/notification_hub/templates/notification_hub/webhook/invoice/overdue.json.jbuilder +6 -0
  46. data/lib/generators/notification_hub/templates/notification_hub/webhook/invoice/payment_received.json.jbuilder +7 -0
  47. data/lib/generators/notification_hub/templates/notification_hub/webhook/invoice/unrealized_payment_received.json.jbuilder +7 -0
  48. data/lib/generators/notification_hub/templates/notification_hub/webhook/invoice/viewed.json.jbuilder +6 -0
  49. data/lib/notification_hub/channels/browser_push_notification/base.rb +19 -0
  50. data/lib/notification_hub/channels/browser_push_notification/fcm.rb +42 -0
  51. data/lib/notification_hub/channels/browser_push_notification.rb +16 -0
  52. data/lib/notification_hub/channels/email/action_mailer.rb +31 -0
  53. data/lib/notification_hub/channels/email/base.rb +17 -0
  54. data/lib/notification_hub/channels/email.rb +17 -0
  55. data/lib/notification_hub/channels/mobile_push_notification/base.rb +18 -0
  56. data/lib/notification_hub/channels/mobile_push_notification/fcm.rb +42 -0
  57. data/lib/notification_hub/channels/mobile_push_notification.rb +16 -0
  58. data/lib/notification_hub/channels/sms/aws.rb +32 -0
  59. data/lib/notification_hub/channels/sms/base.rb +18 -0
  60. data/lib/notification_hub/channels/sms.rb +16 -0
  61. data/lib/notification_hub/channels/webhook/base.rb +19 -0
  62. data/lib/notification_hub/channels/webhook/httparty.rb +39 -0
  63. data/lib/notification_hub/channels/webhook.rb +16 -0
  64. data/lib/notification_hub/device_manager.rb +19 -0
  65. data/lib/notification_hub/subscription_manager.rb +55 -0
  66. data/lib/notification_hub/version.rb +3 -0
  67. data/lib/notification_hub.rb +88 -0
  68. data/notification_hub.gemspec +26 -0
  69. metadata +167 -0
@@ -0,0 +1,10 @@
1
+ <%= render partial: "shared/system_emails/email_header", formats: :html %>
2
+
3
+ <p> <%= "#{I18n.t(:greeting, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@user.first_name)}," %> </p>
4
+ <p> <%= "#{I18n.t(:your_invoice, scope: LocaleScope::SCOPE_MESSAGES_SPECIFIC)} #{sanitize(@invoice.statement_no)} #{I18n.t(:has_recieved, scope: LocaleScope::SCOPE_MESSAGES_SPECIFIC)} #{sanitize(@invoice.business.connection_name)}." %> </p>
5
+ <p> <%= I18n.t(:realized, scope: LocaleScope::SCOPE_MESSAGES_SPECIFIC) %>. </p>
6
+ <p> <%= I18n.t(:more_details, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %>: </p>
7
+ <p> <%= link_to @invoice.formatted_invoice_url %> </p>
8
+ <p> &mdash; <%= I18n.t(:sent_by, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %> </p>
9
+
10
+ <%= render partial: "shared/system_emails/email_footer", formats: :html, locals: {notification_hub_message: true } %>
@@ -0,0 +1,11 @@
1
+ <%= "#{I18n.t(:greeting, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@user.first_name)}," %>
2
+
3
+ <%= "#{I18n.t(:your_invoice, scope: LocaleScope::SCOPE_MESSAGES_SPECIFIC)} #{sanitize(@invoice.statement_no)} #{I18n.t(:has_recieved, scope: LocaleScope::SCOPE_MESSAGES_SPECIFIC)} #{sanitize(@invoice.business.connection_name)}." %>
4
+
5
+ <%= I18n.t(:realized, scope: LocaleScope::SCOPE_MESSAGES_SPECIFIC) %>.
6
+
7
+ <%= I18n.t(:more_details, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %>:
8
+
9
+ <%= link_to @invoice.formatted_invoice_url %>
10
+
11
+ &mdash; <%= I18n.t(:sent_by, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %>
@@ -0,0 +1,9 @@
1
+ <%= render partial: "shared/system_emails/email_header", formats: :html %>
2
+
3
+ <p> <%= "#{I18n.t(:greeting, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@user.first_name)}," %> </p>
4
+ <p> <%= "#{I18n.t(:invoice, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@invoice.statement_no)} - #{I18n.t(:invoice, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{I18n.t(:viewed_by_downcase, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@invoice.business.connection_name)}." %> </p>
5
+ <p> <%= I18n.t(:more_details, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %>: </p>
6
+ <p> <%= link_to @invoice.formatted_invoice_url %> </p>
7
+ <p> &mdash; <%= I18n.t(:sent_by, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %> </p>
8
+
9
+ <%= render partial: "shared/system_emails/email_footer", formats: :html, locals: {notification_hub_message: true } %>
@@ -0,0 +1,9 @@
1
+ <%= "#{I18n.t(:greeting, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@user.first_name)}," %>
2
+
3
+ <%= "#{I18n.t(:invoice, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@invoice.statement_no)} - #{I18n.t(:invoice, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{I18n.t(:viewed_by_downcase, scope: LocaleScope::SCOPE_MESSAGES_GENERIC)} #{sanitize(@invoice.business.connection_name)}." %>
4
+
5
+ <%= I18n.t(:more_details, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %>:
6
+
7
+ <%= link_to @invoice.formatted_invoice_url %>
8
+
9
+ &mdash; <%= I18n.t(:sent_by, scope: LocaleScope::SCOPE_MESSAGES_GENERIC) %>
@@ -0,0 +1,6 @@
1
+ company = estimate.company
2
+ json.company_id company.id
3
+ json.company_subdomain company.subdomain
4
+ json.data do
5
+ json.estimate API::EstimateSerializer.new(estimate)
6
+ end
@@ -0,0 +1,6 @@
1
+ company = estimate.company
2
+ json.company_id company.id
3
+ json.company_subdomain company.subdomain
4
+ json.data do
5
+ json.estimate API::EstimateSerializer.new(estimate)
6
+ end
@@ -0,0 +1,6 @@
1
+ company = estimate.company
2
+ json.company_id company.id
3
+ json.company_subdomain company.subdomain
4
+ json.data do
5
+ json.estimate API::EstimateSerializer.new(estimate)
6
+ end
@@ -0,0 +1,6 @@
1
+ company = invoice.company
2
+ json.company_id company.id
3
+ json.company_subdomain company.subdomain
4
+ json.data do
5
+ json.invoice API::InvoiceSerializer.new(invoice)
6
+ end
@@ -0,0 +1,7 @@
1
+ company = invoice.company
2
+ json.company_id company.id
3
+ json.company_subdomain company.subdomain
4
+ json.data do
5
+ json.invoice API::InvoiceSerializer.new(invoice)
6
+ json.payment API::InvoicePaymentSerializer.new(payment)
7
+ end
@@ -0,0 +1,7 @@
1
+ company = invoice.company
2
+ json.company_id company.id
3
+ json.company_subdomain company.subdomain
4
+ json.data do
5
+ json.invoice API::InvoiceSerializer.new(invoice)
6
+ json.payment API::InvoicePaymentSerializer.new(payment)
7
+ end
@@ -0,0 +1,6 @@
1
+ company = invoice.company
2
+ json.company_id company.id
3
+ json.company_subdomain company.subdomain
4
+ json.data do
5
+ json.invoice API::InvoiceSerializer.new(invoice)
6
+ end
@@ -0,0 +1,19 @@
1
+ module NotificationHub
2
+ module Channels
3
+ module BrowserPushNotification
4
+ class Base
5
+ def initialize(options)
6
+ BrowserPushNotification.default_gateway = self.class.gateway_code
7
+ options[:template_path] ||= "notification_hub/browser_push_notification"
8
+ options[:timeout_time] ||= 10
9
+ self.class.gateway_options = options
10
+ end
11
+
12
+ class << self
13
+ attr_accessor :gateway_options
14
+ attr_accessor :gateway_code
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ require 'notification_hub/channels/browser_push_notification/base'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module BrowserPushNotification
6
+ class Fcm < Base
7
+ @gateway_code = :fcm
8
+
9
+ def initialize(options)
10
+ super
11
+ end
12
+
13
+ class << self
14
+ def send_message(event_code, data, options)
15
+ event = event_code.split(".")
16
+
17
+ begin
18
+ json_string = ActionController::Base.new.
19
+ render_to_string("#{gateway_options[:template_path]}/#{event[0]}/#{event[1]}", locals: data)
20
+ json_object = JSON.parse(json_string)
21
+ json_object[:to] = options[:push_token]
22
+ json_object[:registration_ids] = options[:push_tokens]
23
+
24
+ response = HTTParty.post("https://fcm.googleapis.com/fcm/send", {
25
+ :body => json_object.to_json,
26
+ :headers => {
27
+ 'Content-Type' => 'application/json',
28
+ 'Authorization' => "key=#{gateway_options[:server_key]}"
29
+ },
30
+ :timeout => gateway_options[:timeout_time]
31
+ })
32
+
33
+ raise "BrowserPushNotification::FCM Error: #{response.body}" if response.code != 200
34
+ rescue => exception
35
+ raise "BrowserPushNotification::FCM Error: #{exception.message}"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ require 'notification_hub/channels/browser_push_notification/fcm'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module BrowserPushNotification
6
+ class << self
7
+ attr_accessor :default_gateway
8
+
9
+ def send_message(event_code, data, options, gateway = nil)
10
+ gateway ||= default_gateway
11
+ "NotificationHub::Channels::BrowserPushNotification::#{gateway.to_s.camelize}".constantize.send_message(event_code, data, options)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ require 'notification_hub/channels/email/base'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module Email
6
+ class ActionMailer < Base
7
+ @gateway_code = :action_mailer
8
+
9
+ def initialize(configuration)
10
+ super
11
+ end
12
+
13
+ class << self
14
+ def send_message(event_code, data, options)
15
+ event = event_code.split(".")
16
+
17
+ begin
18
+ if Rails.version.to_f >= 4.2
19
+ "#{event[0].camelize}Mailer".constantize.send(event[1].to_sym, data, options[:email]).deliver_now
20
+ else
21
+ "#{event[0].camelize}Mailer".constantize.send(event[1].to_sym, data, options[:email]).deliver
22
+ end
23
+ rescue => exception
24
+ raise "Email::ActionMailer Error: #{exception.message}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module NotificationHub
2
+ module Channels
3
+ module Email
4
+ class Base
5
+ def initialize(options)
6
+ Email.default_gateway = self.class.gateway_code
7
+ self.class.gateway_options = options
8
+ end
9
+
10
+ class << self
11
+ attr_accessor :gateway_options
12
+ attr_accessor :gateway_code
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'notification_hub/channels/email/action_mailer'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module Email
6
+ class << self
7
+ attr_accessor :default_gateway
8
+
9
+ def send_message(event_code, data, options, gateway = nil)
10
+ gateway ||= default_gateway
11
+ "NotificationHub::Channels::Email::#{gateway.to_s.camelize}".constantize.send_message(event_code, data, options)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,18 @@
1
+ module NotificationHub
2
+ module Channels
3
+ module MobilePushNotification
4
+ class Base
5
+ def initialize(options)
6
+ MobilePushNotification.default_gateway = self.class.gateway_code
7
+ options[:template_path] ||= "notification_hub/mobile_push_notification"
8
+ self.class.gateway_options = options
9
+ end
10
+
11
+ class << self
12
+ attr_accessor :gateway_options
13
+ attr_accessor :gateway_code
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ require 'notification_hub/channels/mobile_push_notification/base'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module MobilePushNotification
6
+ class Fcm < Base
7
+ @gateway_code = :fcm
8
+
9
+ def initialize(options)
10
+ super
11
+ end
12
+
13
+ class << self
14
+ def send_message(event_code, data, options)
15
+ event = event_code.split(".")
16
+
17
+ begin
18
+ json_string = ActionController::Base.new.
19
+ render_to_string("#{gateway_options[:template_path]}/#{event[0]}/#{event[1]}", locals: data)
20
+ json_object = JSON.parse(json_string)
21
+ json_object[:to] = options[:push_token]
22
+ json_object[:registration_ids] = options[:push_tokens]
23
+
24
+ response = HTTParty.post("https://fcm.googleapis.com/fcm/send", {
25
+ :body => json_object.to_json,
26
+ :headers => {
27
+ 'Content-Type' => 'application/json',
28
+ 'Authorization' => "key=#{gateway_options[:server_key]}"
29
+ },
30
+ :timeout => gateway_options[:timeout_time]
31
+ })
32
+
33
+ raise "MobilePushNotification::Fcm Error: #{response.body}" if response.code != 200
34
+ rescue => exception
35
+ raise "MobilePushNotification::Fcm Error: #{exception.message}"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ require 'notification_hub/channels/mobile_push_notification/fcm'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module MobilePushNotification
6
+ class << self
7
+ attr_accessor :default_gateway
8
+
9
+ def send_message(event_code, data, options, gateway = nil)
10
+ gateway ||= default_gateway
11
+ "NotificationHub::Channels::MobilePushNotification::#{gateway.to_s.camelize}".constantize.send_message(event_code, data, options)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require 'notification_hub/channels/sms/base'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module Sms
6
+ class Aws < Base
7
+ @gateway_code = :aws
8
+
9
+ def initialize(configuration)
10
+ super
11
+ end
12
+
13
+ class << self
14
+ def send_message(event_code, data, options)
15
+ event = event_code.split(".")
16
+
17
+ begin
18
+ string = ActionController::Base.new.
19
+ render_to_string("#{gateway_options[:template_path]}/#{event[0]}/#{event[1]}", locals: data)
20
+
21
+ sns_client = ::Aws::SNS::Client.new
22
+ sns_client.publish(phone_number: options[:phone_number].gsub(/[^0-9]+/, ''),
23
+ message: string)
24
+ rescue => exception
25
+ raise "Sms::Aws Error: #{exception.message}"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module NotificationHub
2
+ module Channels
3
+ module Sms
4
+ class Base
5
+ def initialize(options)
6
+ Sms.default_gateway = self.class.gateway_code
7
+ options[:template_path] ||= "notification_hub/sms"
8
+ self.class.gateway_options = options
9
+ end
10
+
11
+ class << self
12
+ attr_accessor :gateway_options
13
+ attr_accessor :gateway_code
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'notification_hub/channels/sms/aws'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module Sms
6
+ class << self
7
+ attr_accessor :default_gateway
8
+
9
+ def send_message(event_code, data, options, gateway = nil)
10
+ gateway ||= default_gateway
11
+ "NotificationHub::Channels::Sms::#{default_gateway.to_s.camelize}".constantize.send_message(event_code, data, options)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module NotificationHub
2
+ module Channels
3
+ module Webhook
4
+ class Base
5
+ def initialize(options)
6
+ Webhook.default_gateway = self.class.gateway_code
7
+ options[:template_path] ||= "notification_hub/webhook"
8
+ options[:timeout_time] ||= 10
9
+ self.class.gateway_options = options
10
+ end
11
+
12
+ class << self
13
+ attr_accessor :gateway_options
14
+ attr_accessor :gateway_code
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ require 'notification_hub/channels/webhook/base'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module Webhook
6
+ class Httparty < Base
7
+ @gateway_code = :httparty
8
+
9
+ def initialize(configuration)
10
+ super
11
+ end
12
+
13
+ class << self
14
+ def send_message(event_code, data, options)
15
+ event = event_code.split(".")
16
+
17
+ begin
18
+ json_string = ActionController::Base.new.
19
+ render_to_string("#{gateway_options[:template_path]}/#{event[0]}/#{event[1]}", locals: data)
20
+ json_object = JSON.parse(json_string)
21
+ json_object[:event] = event_code
22
+ json_object[:timestamp] = Time.zone.now
23
+
24
+ response = HTTParty.post(options[:webhook_url], {
25
+ :body => json_object.to_json,
26
+ :headers => { 'Content-Type' => 'application/json' },
27
+ :timeout => gateway_options[:timeout_time]
28
+ })
29
+
30
+ raise "Webhook::Httparty Error: #{response.body}" if response.code != 200
31
+ rescue => exception
32
+ raise "Webhook::Httparty Error Error: #{exception.message}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ require 'notification_hub/channels/webhook/httparty'
2
+
3
+ module NotificationHub
4
+ module Channels
5
+ module Webhook
6
+ class << self
7
+ attr_accessor :default_gateway
8
+
9
+ def send_message(event_code, data, options, gateway = nil)
10
+ gateway ||= default_gateway
11
+ "NotificationHub::Channels::Webhook::#{gateway.to_s.camelize}".constantize.send_message(event_code, data, options)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module NotificationHub
2
+ module DeviceManager
3
+ class << self
4
+
5
+ def create_device(association_model_id, channel_code, device_details)
6
+ NotificationHub::Device.where("#{NotificationHub.association_model}_id" => association_model_id,
7
+ channel_code: channel_code).where(device_details).first_or_create!
8
+ end
9
+
10
+ def update_device(id, device_details)
11
+ device = NotificationHub::Device.find(id).update_attributes(device_details)
12
+ end
13
+
14
+ def delete_device(id)
15
+ NotificationHub::Device.find(id).destroy
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ require 'notification_hub/device_manager'
2
+
3
+ module NotificationHub
4
+ module SubscriptionManager
5
+ class << self
6
+
7
+ def create_subscription(association_model_id, event_code, channel_code, device_details=nil)
8
+ subscription = NotificationHub::Subscription.
9
+ where("#{NotificationHub.association_model}_id" => association_model_id, event_code: event_code,
10
+ channel_code: channel_code).first_or_create
11
+
12
+ if device_details
13
+ device = NotificationHub::DeviceManager.create_device(association_model_id, channel_code, device_details)
14
+
15
+ NotificationHub::SubscriptionDevice.where(notification_hub_subscription_id:
16
+ subscription.id, notification_hub_device_id: device.id).first_or_create!
17
+ end
18
+ subscription
19
+ end
20
+
21
+ def update_subscription(id, event_code, channel_code, device_details)
22
+ subscription = NotificationHub::Subscription.find(id)
23
+ subscription.update_attributes(event_code: event_code, channel_code: channel_code)
24
+ if device_details
25
+ subscription.notification_hub_subscription_devices.destroy_all
26
+ association_model_id = eval("subscription.#{NotificationHub.association_model}.id")
27
+ device = NotificationHub::DeviceManager.create_device(association_model_id, channel_code, device_details)
28
+
29
+ NotificationHub::SubscriptionDevice.where(notification_hub_subscription_id:
30
+ subscription.id, notification_hub_device_id: device.id).first_or_create!
31
+ end
32
+ subscription
33
+ end
34
+
35
+ def create_subscription_device(association_model_id, device_id, event_code, channel_code)
36
+ subscription = NotificationHub::Subscription.
37
+ where("#{NotificationHub.association_model}_id" => association_model_id, event_code: event_code,
38
+ channel_code: channel_code).first_or_create
39
+
40
+ subscription_device = NotificationHub::SubscriptionDevice.where(notification_hub_subscription_id:
41
+ subscription.id, notification_hub_device_id: device_id).first_or_create
42
+
43
+ subscription_device
44
+ end
45
+
46
+ def delete_subscription(id)
47
+ NotificationHub::Subscription.find(id.to_i).destroy
48
+ end
49
+
50
+ def delete_subscription_device(id)
51
+ NotificationHub::SubscriptionDevice.find(id.to_i).destroy
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module NotificationHub
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,88 @@
1
+ require "notification_hub/version"
2
+ require "http_logger"
3
+ require "httparty"
4
+ require 'notification_hub/channels/email'
5
+ require 'notification_hub/channels/mobile_push_notification'
6
+ require 'notification_hub/channels/browser_push_notification'
7
+ require 'notification_hub/channels/sms'
8
+ require 'notification_hub/channels/webhook'
9
+ require 'notification_hub/subscription_manager'
10
+ require 'notification_hub/device_manager'
11
+
12
+ module NotificationHub
13
+ mattr_accessor :association_model
14
+ mattr_accessor :events
15
+ mattr_accessor :logger
16
+
17
+ class << self
18
+ def configure
19
+ @@association_model ||= "user"
20
+ @@logger = Logger.new("#{Rails.root}/log/notification_hub.log")
21
+ yield self
22
+ end
23
+
24
+ def set_channel(*args)
25
+ channel = args[0]
26
+ options = args[1]
27
+ gateway = options[:gateway]
28
+
29
+ "NotificationHub::Channels::#{channel.to_s.camelize}::#{gateway.to_s.camelize}".constantize.new(options)
30
+ end
31
+
32
+ def deliver_now(association_model_id, event_code, data, options=nil)
33
+ send_message(association_model_id, event_code, data, options)
34
+ end
35
+
36
+ def deliver(association_model_id, event_code, data, options=nil)
37
+ #NotificationHubJob.perform_later(association_model_id, event_code, data, options)
38
+ NotificationHubJob.perform_async(association_model_id, event_code, data, options)
39
+ end
40
+
41
+ def send_message(association_model_id, event_code, data_wrapper, options)
42
+ # Fetch data from the database if record id is passed.
43
+ data = {}
44
+ data_wrapper.each do |key,value|
45
+ if value.is_a?(Integer)
46
+ data[key.to_sym] = key.to_s.classify.constantize.find_by_id(value)
47
+ else
48
+ data[key.to_sym] = value
49
+ end
50
+ end
51
+
52
+ # Query subsccriptions for the relevant event.
53
+ susbcriptions = NotificationHub::Subscription.where("#{association_model}_id" => association_model_id, event_code: event_code)
54
+
55
+ susbcriptions.each do |susbcription|
56
+ if susbcription.gateway_code.present?
57
+ channel_const = "NotificationHub::Channels::#{susbcription.channel_code.camelize}::#{susbcription.gateway_code.camelize}".constantize
58
+ else
59
+ channel_const = "NotificationHub::Channels::#{susbcription.channel_code.camelize}".constantize
60
+ end
61
+
62
+ susbcription.notification_hub_devices.try(:each) do |device|
63
+ device_details = device.attributes.symbolize_keys
64
+ begin
65
+ channel_const.send_message(event_code, data, device_details)
66
+ rescue => e
67
+ NotificationHub.logger.error e.message
68
+ e.backtrace.each { |line| NotificationHub.logger.error line }
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ def send_direct(event_code, data, device_details, channel_code, gateway_code=nil)
75
+ if susbcription.gateway_code.present?
76
+ channel_const = "NotificationHub::Channels::#{susbcription.channel_code.camelize}::#{susbcription.gateway_code.camelize}".constantize
77
+ else
78
+ channel_const = "NotificationHub::Channels::#{susbcription.channel_code.camelize}".constantize
79
+ end
80
+ begin
81
+ channel_const.send_message(event_code, data, device_details)
82
+ rescue => e
83
+ NotificationHub.logger.error e.message
84
+ e.backtrace.each { |line| NotificationHub.logger.error line }
85
+ end
86
+ end
87
+ end
88
+ end