communify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 88e94e418d817416c98acd0a9f61a075b9cdf22aefcbb6fddf68b38ae7e3fa75
4
+ data.tar.gz: 7377a77794d509b110ffebd0985b311bc4618afdf686ec522558465f9cd50454
5
+ SHA512:
6
+ metadata.gz: 8487ee00369708cc1f4a323567b76d7e132dba70481c9e2c4ee23c6e09d12fb2edcaac5f0d310e66e3e0d25dab4f096d1bffd20462e1a801f95784da8772d886
7
+ data.tar.gz: 8b30467377a80ed47782ee9598f3f990e91f74a3c952dffa4d50ffef551503d99330aec98a5c2c09a1a0ed4596a592b53463f9a045936de0ebe96a5eebe45313
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.1
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at 66819332+kishorerr@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in communify.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ communify (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.1)
10
+ public_suffix (>= 2.0.2, < 6.0)
11
+ connection_pool (2.2.5)
12
+ diff-lcs (1.5.0)
13
+ faraday (2.3.0)
14
+ faraday-net_http (~> 2.0)
15
+ ruby2_keywords (>= 0.0.4)
16
+ faraday-net_http (2.0.3)
17
+ fcm (1.0.8)
18
+ faraday (>= 1.0.0, < 3.0)
19
+ googleauth (~> 1)
20
+ googleauth (1.2.0)
21
+ faraday (>= 0.17.3, < 3.a)
22
+ jwt (>= 1.4, < 3.0)
23
+ memoist (~> 0.16)
24
+ multi_json (~> 1.11)
25
+ os (>= 0.9, < 2.0)
26
+ signet (>= 0.16, < 2.a)
27
+ jwt (2.4.1)
28
+ memoist (0.16.2)
29
+ multi_json (1.15.0)
30
+ nokogiri (1.13.7-x86_64-linux)
31
+ racc (~> 1.4)
32
+ os (1.1.4)
33
+ public_suffix (5.0.0)
34
+ racc (1.6.0)
35
+ rack (2.2.4)
36
+ rack-protection (2.2.1)
37
+ rack
38
+ rake (10.5.0)
39
+ redis (4.7.1)
40
+ rspec (3.11.0)
41
+ rspec-core (~> 3.11.0)
42
+ rspec-expectations (~> 3.11.0)
43
+ rspec-mocks (~> 3.11.0)
44
+ rspec-core (3.11.0)
45
+ rspec-support (~> 3.11.0)
46
+ rspec-expectations (3.11.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.11.0)
49
+ rspec-mocks (3.11.1)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.11.0)
52
+ rspec-support (3.11.0)
53
+ ruby2_keywords (0.0.5)
54
+ sidekiq (6.0.7)
55
+ connection_pool (>= 2.2.2)
56
+ rack (~> 2.0)
57
+ rack-protection (>= 2.0.0)
58
+ redis (>= 4.1.0)
59
+ signet (0.17.0)
60
+ addressable (~> 2.8)
61
+ faraday (>= 0.17.5, < 3.a)
62
+ jwt (>= 1.5, < 3.0)
63
+ multi_json (~> 1.10)
64
+ twilio-ruby (5.69.0)
65
+ faraday (>= 0.9, < 3.0)
66
+ jwt (>= 1.5, <= 2.5)
67
+ nokogiri (>= 1.6, < 2.0)
68
+
69
+ PLATFORMS
70
+ x86_64-linux
71
+
72
+ DEPENDENCIES
73
+ bundler (~> 2.3)
74
+ communify!
75
+ fcm (~> 1.0.8)
76
+ rake (~> 10.0)
77
+ redis (~> 4.7.0)
78
+ rspec (~> 3.0)
79
+ sidekiq (~> 6.0.0)
80
+ twilio-ruby (~> 5.69.0)
81
+
82
+ BUNDLED WITH
83
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Kishore RR
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # Communify
2
+
3
+ The Communify gem lets your ruby backend send SMS through twilio and push notifications through Firebase cloud messaging while maintaining the log of the same. Also it logs all the emails that are sent through ActionMailer.
4
+
5
+ ## Why communify?
6
+
7
+ Companies can communicate or notify their customers with information about the app, its features, its updates, and other details with their customers using email, push notifications and through SMS.
8
+
9
+ This stays to be a common mode of communication irrespective of the purpose of the app. So in order to ease the coding part and bring in all the 3 modes of communication together, we have come up with a Rails gem that helps developers include SMS, push notification and email logging with minimal lines of code.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'communify'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install communify
26
+
27
+ Installing communify in your code also includes `twilio-ruby` with version equal to or higher to 5.69.0 and `fcm` with version equal to or higher than 1.0.8 i.e Communify abstracts the working of these gems
28
+
29
+ ## Requirements
30
+
31
+ For successful functioning of this gem include sidekiq, redis and dotenv-rails gems in your application's gemfile
32
+
33
+ And keep your sidekiq running, to run locally use
34
+
35
+ $ bundle exec sidekiq
36
+
37
+ ## Usage
38
+
39
+ For maintainig logs, run the following command in your command line
40
+
41
+ $ rails g communify:install
42
+
43
+ This command generates a migration file and a model named CommunifyLog
44
+
45
+ And then execute:
46
+
47
+ $ rails db:migrate
48
+
49
+ This creates a table with fields
50
+ - title -> title of a push notification goes here
51
+
52
+ - content -> message/notification text to be sent goes here
53
+
54
+ - recipient -> recipient mobile number incase of sms or registration id (generated when an app is installed) incase of push notification goes here
55
+
56
+ - priority -> is an enum with options urgent(0 minutes), high(5 minutes), medium(30 minutes), low(60 minutes). When a message is with priority - urgent, the sidekiq job is triggered immediately, in case of high priority - the sidekiq job is triggered in 5 minutes etc., default value: urgent
57
+
58
+ - content_type -> this field stores if the log is for sms(0) / push notification(1)
59
+
60
+ - status -> stores the status of the message/notification sent if it is successful sent or failured if so then with what error
61
+
62
+ - third_party_id -> id generated when a message or notification is successfully sent
63
+
64
+ - attempt_count -> is generated automatically and tells the number of attempts taken to send the text. The text is attempted to send utmost 3 times
65
+
66
+
67
+ ### For SMS
68
+
69
+ To store Twilio keys in your app, run the following command in your command line
70
+
71
+ $ rails g communify:sms
72
+
73
+ This command generates communify_sms.rb file in config/initializers folder of your app
74
+
75
+ Uncomment the lines and set the account_sid, auth_token and sender_no with the keys provided in your twilio account.
76
+
77
+ Then add the following lines in your controller method to send sms
78
+
79
+ >Communify::Controllers::Sms.send_message(content, recipient, priority) # returns the resource_id if successful
80
+
81
+ Note:
82
+ Content -> text to be sent through SMS
83
+
84
+ Recipient -> recipient number with country code
85
+ Example: “+91-1234567890”
86
+
87
+ Priority -> “urgent” / “high” / “medium” / “low”
88
+
89
+ Sample Log:
90
+
91
+ <CommunifyLog id: 1, title: "", content: "OTP from abc app: 123456", recipient: "+1-123456789", attempt_count: 1, status: "Message Delivered at 2022-12-12T15:20:45+00:00", third_party_id: "SMaf7c76052f9c5180c03abc5c73d67d9", created_at: "2022-12-12 15:20:45", updated_at: "2022-12-12 15:20:45", priority: "urgent", content_type: "sms">
92
+
93
+ ### For Push notification
94
+
95
+ To store device information, run the following command in your command line
96
+
97
+ $ rails g communify:push_notification
98
+
99
+ This command generates a migration file and a model in your project folder
100
+
101
+ And then execute:
102
+
103
+ $ rails db:migrate
104
+
105
+ This creates a table named Devices with fields
106
+
107
+ - device_id -> registration id goes here
108
+
109
+ - device_name -> name of the devices goes here (android/ios/web)
110
+
111
+ - person_id, person_type -> user id and class name goes here
112
+
113
+ Get registration_id/device_id (An ID generated by the FCM SDK for each client app instance) from the frontend and link it with a user and store it in Device table using
114
+
115
+ >Device.create(device_id: "registration_id goes here", device_name: "Name of the device", person: user)
116
+
117
+ To send and maintain logs of the notification
118
+
119
+ First store the FCM SERVER KEY in your .env file with variable name FCM_SERVER_KEY
120
+
121
+ Then to send notification add this line of code in your controller
122
+
123
+ >Communify::Controllers::PushNotification.send_push_notification(title, content, recipient, notification_type, other_details, priority)
124
+
125
+ Note:
126
+
127
+ Title -> Title of the notification goes here
128
+
129
+ Content -> Text to be sent through SMS
130
+
131
+ Recipient -> Array of registration ids
132
+ Example: [“reg_id1”,”reg_id2”]
133
+
134
+ Notification Type -> Type of notification i.e keyword that helps Frontend to know where to redirect the user when the notification is clicked
135
+
136
+ Other details -> Can be a string or a json converted to a string such that these details are sent to frontend/mobile through the
137
+ notification but are not visible to the user
138
+
139
+ Priority -> “urgent” / “high” / “medium” / “low”
140
+
141
+ - user refers to the user record
142
+
143
+ - registration_id and device_id refers to the same and is used interchangebly
144
+
145
+ Sample log:
146
+
147
+ <CommunifyLog id: 1, title: "Notification title", content: "Notification content", recipient: "[\"e2x7BuarRx2tHqv7p80LBJ:APA91bEsh1_ouyuwLvtt06UCX...", attempt_count: 1, status: "Notification Delivered at 2022-11-08T07:17:57+00:0...", third_party_id: "0:1667891877225583%d8a4a6c1d8a4a6c1", created_at: "2022-11-04 14:28:27", updated_at: "2022-11-04 14:28:27", priority: "urgent", content_type: "push_notification">
148
+
149
+ ### For Maintaining Email logs
150
+
151
+ To allow storing logs of all emails that are sent, execute the following command,
152
+
153
+ $ rails g communify:email
154
+
155
+ This creates a mail_observer.rb file in config/initializers directory. Observers give you access to the email message after it has been sent.
156
+
157
+ So this helps maintaing the logs in CommunifyLog table where the content, receiver email address and sent timings are stored.
158
+
159
+ Sample log:
160
+
161
+ <CommunifyLog id: 1, title: "", content: "Subject: Verify your email address, Body: <...", recipient: "ccc@gmail.com", attempt_count: 1, status: "Mail delivered at 2022-12-12T14:47:44+00:00", third_party_id: "63973f107deb7_d32c53f9f6e105da41323d@ip-172-31-10-...", created_at: "2022-12-12 14:47:45", updated_at: "2022-12-12 14:47:45", priority: "urgent", content_type: "email">
162
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "communify"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/communify.gemspec ADDED
@@ -0,0 +1,47 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "communify/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "communify"
8
+ spec.version = Communify::VERSION
9
+ spec.authors = ["Kishore RR, Shree Varshini"]
10
+ spec.email = ["66819332+kishorerr@users.noreply.github.com, rpshreevarshini@gmail.com"]
11
+
12
+ spec.summary = "Communication Mediator Gem"
13
+ spec.description = "Communication mediator"
14
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 2.6.0"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ # if spec.respond_to?(:metadata)
21
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
22
+
23
+ # spec.metadata["homepage_uri"] = spec.homepage
24
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
25
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
26
+ # else
27
+ # raise "RubyGems 2.0 or newer is required to protect against " \
28
+ # "public gem pushes."
29
+ # end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = "exe"
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = ["lib"]
39
+
40
+ spec.add_development_dependency "bundler", "~> 2.3"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ spec.add_development_dependency "redis", "~>4.7.0"
44
+ spec.add_development_dependency "sidekiq", "~>6.0.0"
45
+ spec.add_dependency "twilio-ruby", "~> 5.69.0"
46
+ spec.add_dependency "fcm", "~>1.0.8"
47
+ end
@@ -0,0 +1,25 @@
1
+ module Communify
2
+ module Controllers
3
+ class Email
4
+ def self.create_log(mail)
5
+ content = "Subject: #{mail.subject}, Body: #{mail.body}"
6
+ recipient = self.comma_separated(mail.to) + self.comma_separated(mail.cc) + self.comma_separated(mail.bcc)
7
+ resource = CommunifyLog.new(content: content, recipient: recipient, status: "Mail delivered at #{mail.date}", third_party_id: mail.message_id, content_type: 2, attempt_count: 1)
8
+ if resource.save
9
+ return resource.id
10
+ else
11
+ raise "Error => Resource has not been saved!!"
12
+ end
13
+ end
14
+
15
+ private
16
+ def self.comma_separated(arr)
17
+ if !arr.nil? && !arr.empty?
18
+ arr.join(",")
19
+ else
20
+ ""
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ require 'communify/workers/push_notification_worker'
2
+ require 'sidekiq/api'
3
+ module Communify
4
+ module Controllers
5
+ class PushNotification
6
+ def self.send_push_notification(title, content, recipient, notification_type, other_details, priority = 0)
7
+ resource = CommunifyLog.new(title: title, content: content, recipient: recipient.to_s, content_type: 1, priority: priority)
8
+ if resource.save
9
+ resource.update_column(:status, "Notification Queued at #{DateTime.now}")
10
+ time = resource.read_attribute_before_type_cast(:priority)
11
+ attempt = 1
12
+ result = Communify::Workers::PushNotificationWorker.perform_in(time.minutes.from_now, resource.id, notification_type, other_details, time, attempt)
13
+ return resource.id
14
+ else
15
+ raise "Error => Resource has not been saved!!"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'communify/workers/sms_worker'
2
+ require 'sidekiq/api'
3
+ module Communify
4
+ module Controllers
5
+ class Sms
6
+ def self.send_message(content, recipient, priority = 0)
7
+ resource = CommunifyLog.new(content: content, recipient: recipient, priority: priority, content_type: 0)
8
+ if resource.save
9
+ resource.update_column(:status, "Message Queued at #{DateTime.now}")
10
+ time = resource.read_attribute_before_type_cast(:priority)
11
+ attempt = 1
12
+ result = Communify::Workers::SmsWorker.perform_in(time.minutes.from_now, resource.id, time, attempt)
13
+ return resource.id
14
+ else
15
+ raise "Error => Resource has not been saved!!"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/generators'
2
+
3
+ module Communify
4
+ module Generators
5
+ class EmailGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def copy_config
9
+ template "mail_observer.erb", "config/initializers/mail_observer.rb"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ class MailObserver
2
+ def self.delivered_email(message)
3
+ Communify::Controllers::Email.create_log(message)
4
+ end
5
+ end
6
+
7
+ ActionMailer::Base.register_observer(MailObserver)
8
+
9
+
@@ -0,0 +1,34 @@
1
+ require 'rails/generators'
2
+
3
+
4
+ module Communify
5
+ module Generators
6
+
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../templates", __FILE__)
9
+
10
+ desc "Creates a Communify log"
11
+
12
+ def copy_migration
13
+ template "communify_log_migration.erb", "db/migrate/#{DateTime.now.to_formatted_s(:number)}_create_communify_logs.rb", migration_version: migration_version
14
+ end
15
+
16
+ def copy_communify_log_model
17
+ template "communify_log_model.erb", "app/models/communify_log.rb"
18
+ end
19
+
20
+ private
21
+
22
+ def rails5_and_up?
23
+ Rails::VERSION::MAJOR >= 5
24
+ end
25
+
26
+ def migration_version
27
+ if rails5_and_up?
28
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateCommunifyLogs < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :communify_logs do |t|
6
+ t.string :title, null: false, default: ""
7
+ t.string :content, null: false, default: ""
8
+ t.string :recipient, null: false, default: ""
9
+ t.integer :attempt_count, null: false,default: 1
10
+ t.string :status
11
+ t.string :third_party_id, null: false,default: ""
12
+
13
+ t.timestamps null: false
14
+ end
15
+ add_column :communify_logs, :priority, :integer, default: 0
16
+ add_column :communify_logs, :content_type, :integer
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ class CommunifyLog < ActiveRecord::Base
2
+ #default values have been set for priority in minutes
3
+ enum priority: {urgent: 0, high: 5, medium: 30 , low: 60}
4
+ enum content_type: {sms: 0, push_notification: 1, email: 2}
5
+
6
+ end
@@ -0,0 +1,31 @@
1
+ require 'rails/generators'
2
+ module Communify
3
+ module Generators
4
+ class PushNotificationGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ desc "Creates a Device table"
8
+
9
+ def copy_migration
10
+ template "device_migration.erb", "db/migrate/#{DateTime.now.to_formatted_s(:number)}_create_devices.rb", migration_version: migration_version
11
+ end
12
+
13
+ def copy_communify_log_model
14
+ template "device_model.erb", "app/models/device.rb"
15
+ end
16
+
17
+ private
18
+
19
+ def rails5_and_up?
20
+ Rails::VERSION::MAJOR >= 5
21
+ end
22
+
23
+ def migration_version
24
+ if rails5_and_up?
25
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateDevices < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :devices do |t|
6
+ t.string :device_id, null: false
7
+ t.string :device_name, null: false
8
+ t.references :person, polymorphic: true, index: true
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ class Device < ApplicationRecord
2
+ belongs_to :person, polymorphic: true
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+
4
+ module Communify
5
+ module Generators
6
+
7
+ class SmsGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def copy_config
11
+ template "communify_sms.erb", "config/initializers/communify_sms.rb"
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'communify'
2
+ Communify.config do |c|
3
+
4
+ # These are mandatory options that you must set for sms communication:
5
+
6
+ # Credentials for Twilio
7
+ # c.account_sid = 'Your Twilio Account SID goes here'
8
+ # c.auth_token = 'Your Twilio Auth Token goes here'
9
+ # c.sender_no = 'Your Sender Number goes here'
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module Communify
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'fcm'
2
+ require 'sidekiq'
3
+ require 'communify'
4
+ require 'communify/controllers/push_notification_controller'
5
+ module Communify
6
+ module Workers
7
+ class PushNotificationWorker
8
+ include Sidekiq::Worker
9
+ sidekiq_options dead: false, retry: false
10
+
11
+ def perform(resource_id, notification_type, other_details, time, attempt)
12
+ fcm_client = FCM.new(ENV['FCM_SERVER_KEY'])
13
+ @current_resource = CommunifyLog.find(resource_id)
14
+ begin
15
+ options = {
16
+ priority: 'high',
17
+ data: {
18
+ message: @current_resource.content,
19
+ notification_type: notification_type,
20
+ other_details: other_details
21
+ },
22
+ notification: {
23
+ title: @current_resource.title,
24
+ body: @current_resource.content,
25
+ sound: 'default'
26
+ }
27
+ }
28
+ response = fcm_client.send(JSON.parse(@current_resource.recipient), options)
29
+ raise if response[:status_code] != 200 || JSON.parse(response[:body])["success"] == 0
30
+ rescue
31
+ @current_resource.update_column(:attempt_count, attempt)
32
+ attempt = attempt +1
33
+ if response[:status_code] != 200
34
+ @current_resource.update_column(:status, "Notification failed at #{DateTime.now} due to error => #{response[:response]}")
35
+ else
36
+ @current_resource.update_column(:status, "Notification failed at #{DateTime.now} due to error => #{JSON.parse(response[:body])["results"]}")
37
+ end
38
+ if attempt < 4
39
+ sleep(time.minutes)
40
+ retry
41
+ end
42
+ else
43
+ message_id = JSON.parse(response[:body])["results"]
44
+ @current_resource.update_column(:third_party_id, message_id.to_s)
45
+ @current_resource.update_column(:attempt_count, attempt)
46
+ @current_resource.update_column(:status, "Notification Delivered at #{DateTime.now}")
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,40 @@
1
+ require 'twilio-ruby'
2
+ require 'sidekiq'
3
+ require 'communify'
4
+ require 'communify/controllers/sms_controller'
5
+ module Communify
6
+ module Workers
7
+ class SmsWorker
8
+ include Sidekiq::Worker
9
+ sidekiq_options dead: false, retry: false
10
+
11
+ def perform(resource_id, time, attempt)
12
+ account_sid = Communify.account_sid
13
+ auth_token = Communify.auth_token
14
+ @client = Twilio::REST::Client.new account_sid, auth_token
15
+ @current_resource = CommunifyLog.find(resource_id)
16
+ begin
17
+ twilio_message = @client.messages.create(from: Communify.sender_no,to: @current_resource.recipient, body: @current_resource.content) # status_callback
18
+ rescue Exception => e
19
+ @current_resource.update_column(:attempt_count, attempt)
20
+ attempt = attempt +1
21
+ @current_resource.update_column(:status, "Message failed at #{DateTime.now} due to error => #{e}")
22
+ if attempt < 4
23
+ sleep(time.minutes)
24
+ retry
25
+ end
26
+ #end
27
+ else
28
+ @current_resource.update_column(:third_party_id, twilio_message.sid)
29
+ @current_resource.update_column(:attempt_count, attempt)
30
+ @current_resource.update_column(:status, "Message Delivered at #{DateTime.now}") #comment out
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ # In communify.erb file get status_callback link
38
+ #: generate a controller action
39
+ # and then update message status after checking smssid with sid
40
+ #refer communify -test
data/lib/communify.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "communify/version"
2
+ require 'communify/generators/install/install_generator'
3
+ require 'communify/generators/sms/sms_generator'
4
+ require 'communify/generators/push_notification/push_notification_generator'
5
+ require 'communify/generators/email/email_generator'
6
+ require 'communify/controllers/sms_controller'
7
+ require 'communify/controllers/push_notification_controller'
8
+ require 'communify/controllers/email_controller'
9
+ require 'communify/workers/sms_worker'
10
+ require 'communify/workers/push_notification_worker'
11
+
12
+ module Communify
13
+ class Error < StandardError; end
14
+
15
+ class << self
16
+ attr_accessor :account_sid, :auth_token, :sender_no
17
+
18
+ def config
19
+ yield self
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: communify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kishore RR, Shree Varshini
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.7.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.7.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: sidekiq
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 6.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 6.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: twilio-ruby
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 5.69.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 5.69.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: fcm
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.8
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.8
111
+ description: Communication mediator
112
+ email:
113
+ - 66819332+kishorerr@users.noreply.github.com, rpshreevarshini@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - communify.gemspec
130
+ - lib/communify.rb
131
+ - lib/communify/controllers/email_controller.rb
132
+ - lib/communify/controllers/push_notification_controller.rb
133
+ - lib/communify/controllers/sms_controller.rb
134
+ - lib/communify/generators/email/email_generator.rb
135
+ - lib/communify/generators/email/templates/mail_observer.erb
136
+ - lib/communify/generators/install/install_generator.rb
137
+ - lib/communify/generators/install/templates/communify_log_migration.erb
138
+ - lib/communify/generators/install/templates/communify_log_model.erb
139
+ - lib/communify/generators/push_notification/push_notification_generator.rb
140
+ - lib/communify/generators/push_notification/templates/device_migration.erb
141
+ - lib/communify/generators/push_notification/templates/device_model.erb
142
+ - lib/communify/generators/sms/sms_generator.rb
143
+ - lib/communify/generators/sms/templates/communify_sms.erb
144
+ - lib/communify/version.rb
145
+ - lib/communify/workers/push_notification_worker.rb
146
+ - lib/communify/workers/sms_worker.rb
147
+ homepage:
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: 2.6.0
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubygems_version: 3.2.33
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Communication Mediator Gem
170
+ test_files: []