magicbell 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76d91ee7dd27c175853dff9ff3dcf5a296dbf58c5151f1a228841a23beb57cc3
4
- data.tar.gz: dd40c6308d20d6219ef0d5a7962d4a70f5e0f381bf8911c76eec1d9d3dbcd439
3
+ metadata.gz: 8a2806ab42cdd91d4eaada59a02ef686432dec09656fd369d244e9a9111a5f0f
4
+ data.tar.gz: ad0d1da2faf2bbdc2c51da677bf4c91c90a4fbb759a70e74da19d525e47de7a2
5
5
  SHA512:
6
- metadata.gz: 2eb10d824cc3228716ef4e1f0c4f392d44528e6b961352e382bb8dc8790ec12cafccdb9a9210a4211dc459a436930a5ec1901bdf8fa58ee17bc63cc041c779ab
7
- data.tar.gz: 1895cbe977bcd395598963545f057fb2e5bb614faa5b6d57eed185d1892dbef0a90e9aa736a59ac601c6b26478718f1b52a289bb23c3566bd0f0e2126c77652b
6
+ metadata.gz: 6849a6a65cb71d23f3f96b7cfccf47bdc2d808c76004bbfafb4321f9b670b852cb7b2770f0fa3fc0a553ca3c6e5d2ba85a52820c63e00350c61f1a69cffee730
7
+ data.tar.gz: e0a8e457403e9b67210d0ada15d8aa3adbbbfea7a7c50810e40ad04dcf0b6d3ca74c193f191a34e127a9b5080aab834e9fa6ed66cca7c2ab95e71484c007d4ee
data/README.md CHANGED
@@ -1,261 +1,250 @@
1
- # magicbell-ruby
1
+ # MagicBell Ruby Library
2
2
 
3
- [MagicBell](https://magicbell.io) is an embeddable Notification Inbox for web & mobile applications.
3
+ This library provides convenient access to the [MagicBell REST API](https://magicbell.com/docs/rest-api/overview) from applications written in Ruby. It includes helpers for creating notifications, fetching them, and calculating the HMAC for a user.
4
4
 
5
- Please familiarlize yourself with the [core concepts of MagicBell](https://developer.magicbell.io/docs/core-concepts) before using this gem.
5
+ [MagicBell](https://magicbell.com) is the notification inbox for your web and mobile applications. You may find it helpful to familiarize yourself with the [core concepts of MagicBell](https://magicbell.com/docs/core-concepts).
6
6
 
7
- This gem
7
+ <img width="415" alt="MagicBell Notification Inbox" src="https://files.readme.io/c09b21a-image1.png">
8
8
 
9
- 1. Makes it easy to interact with [MagicBell's REST API](https://developer.magicbell.io/reference) from Ruby.
9
+ ## Installation
10
10
 
11
- You can use it to create a notification in your MagicBell project etc.
11
+ First, [sign up for a MagicBell account](https://magicbell.com) and grab your MagicBell project's API key and secret from the "Settings" section of your MagicBell dashboard.
12
12
 
13
- 2. Can BCC your ActionMailer email notifications to MagicBell if you rather not use MagicBell's API in your Rails application.
13
+ If you just want to use the package, run:
14
14
 
15
- MagicBell will create an in-app notification from any email notification that's blind copied to it.
15
+ ```
16
+ gem install magicbell
17
+ ```
16
18
 
17
- 3. Helps you calculate the HMAC for a user's email or external_id when you turn on [HMAC Authentication](https://developer.magicbell.io/docs/turn-on-hmac-authentication) for your MagicBell project
19
+ ### Bundler
18
20
 
19
- <img width="415" alt="MagicBell Notification Inbox" src="https://files.readme.io/c09b21a-image1.png">
21
+ If you are installing via bundler, add the gem to your app's Gemfile:
20
22
 
21
- ## Installation
23
+ ```ruby
24
+ # Gemfile
25
+ source 'https://rubygems.org'
22
26
 
23
- Sign up at magicbell.io and obtain your MagicBell project's API Key and API Secret from the "Settings" section in your MagicBell dashboard.
27
+ gem 'magicbell'
28
+ ```
24
29
 
25
- Add the gem to your app's Gemfile
30
+ and run `bundle install` a usual.
26
31
 
27
- ```ruby
28
- gem "magicbell"
29
- ```
32
+ ## Configuration
30
33
 
31
- and install the gem
34
+ The library needs to be configured with your MagicBell project's API key and secret.
32
35
 
33
- ```
34
- bundle install
35
- ```
36
+ ### Global configuration
36
37
 
37
- The gem will automatically pick your MagicBell project's API Key and API Secret from the `MAGICBELL_API_KEY` and `MAGICBELL_API_SECRET` environment variables. Alternatively, provide the API Key and API Secret in an initializer
38
+ By default, this library will automatically pick your MagicBell project's API key and secret from the `MAGICBELL_API_KEY` and `MAGICBELL_API_SECRET` environment variables, respectively.
38
39
 
39
- ```
40
- vim config/initializers/magicbell.rb
41
- ```
40
+ Alternatively, you can configure your MagicBell manually. For example, for a rails project, create an initializer file for MagicBell and set your project's keys:
42
41
 
43
42
  ```ruby
44
43
  # config/initializers/magicbell.rb
45
44
 
46
45
  MagicBell.configure do |config|
47
- config.api_key = "your_magicbell_api_key"
48
- config.api_secret = "your_magicbell_api_secret"
46
+ config.api_key = 'MAGICBELL_API_KEY'
47
+ config.api_secret = 'MAGICBELL_API_SECRET'
49
48
  end
50
49
  ```
51
50
 
52
- ## API Wrapper
51
+ ### Per-request configuration
52
+
53
+ For apps that need to use multiple keys during the lifetime of a process, provide the specific keys when you create instances of `MagicBell::Client`:
54
+
55
+ ```ruby
56
+ require 'magicbell'
57
+
58
+ magicbell = MagicBell::Client.new(
59
+ api_key: 'MAGICBELL_PROJECT_API_KEY',
60
+ api_secret: 'MAGICBELL_PROJECT_API_SECRET'
61
+ )
62
+ ```
63
+
64
+ Please keep in mind that any instance of `MagicBell::Client` will default to the global configuration unless an API key and secret are provided.
53
65
 
54
- This gem makes it easy to interact with MagicBell's REST API https://developer.magicbell.io/reference from Ruby
66
+ ## Usage
55
67
 
56
68
  ### Create a notification
57
69
 
58
- Send a notification to one or many users by identifying them with their email address
70
+ You can send a notification to one or many users by identifying them by their email address:
59
71
 
60
72
  ```ruby
73
+ require 'magicbell'
74
+
61
75
  magicbell = MagicBell::Client.new
62
76
  magicbell.create_notification(
63
- "title" => "Rob assigned a task to you",
64
- "recipients" => [
65
- {
66
- email: "joe@example.com"
67
- },
68
- ]
77
+ title: 'Rob assigned a task to you',
78
+ recipients: [{
79
+ email: 'joe@example.com'
80
+ }, {
81
+ email: 'mary@example.com'
82
+ }]
69
83
  )
70
84
  ```
71
85
 
72
- You can also identify users with their `external_id`, which is their ID in your database. That way, if their email address changes, they'd still have access to their notifications. You'll need to make sure you identify them with their `externalID` [in your frontend](https://developer.magicbell.io/docs/browser-js#identifying-users).
86
+ Or you can identify users by an `external_id` (their ID in your database, for example):
73
87
 
74
88
  ```ruby
75
- magicbell.create_notification(
76
- title: "Welcome to Muziboo",
77
- recipients: [{
78
- external_id: "id_in_your_database"
79
- }]
80
- )
89
+ require 'magicbell'
90
+
91
+ magicbell = MagicBell::Client.new
92
+ magicbell.create_notification(
93
+ title: 'Rob assigned a task to you',
94
+ recipients: [{
95
+ external_id: 'DATABASE_ID'
96
+ }]
97
+ )
81
98
  ```
82
99
 
83
- You can even provide content for the notification and a URL to redirect the user to when they click on the notification the MagicBell Notification Inbox
100
+ This method has the benefit of allowing users to access their notifications when their email address changes. Make sure you identify users by their `externalID` when you [initialize the notification inbox](https://magicbell.com/docs/react/identifying-users), too.
101
+
102
+ You can also provide other data accepted by [our API](https://magicbell.com/docs/rest-api/reference):
84
103
 
85
104
  ```ruby
105
+ require 'magicbell'
106
+
86
107
  magicbell = MagicBell::Client.new
87
108
  magicbell.create_notification(
88
- "title" => "Rob assigned to a task to you",
89
- "recipients" => [
90
- {
91
- email: "joe@example.com"
92
- },
93
- ],
94
- "content": "Hey Joe, can give this customer a demo of our app?",
95
- "action_url" => "https://yourwebsite.com/task_path"
109
+ title: 'Rob assigned to a task to you',
110
+ content: 'Hey Joe, can give this customer a demo of our app?',
111
+ action_url: 'https://example.com/task_path',
112
+ custom_attributes: {
113
+ recipient: {
114
+ first_name: 'Joe',
115
+ last_name: 'Smith'
116
+ }
117
+ },
118
+ overrides: {
119
+ channels: {
120
+ web_push: {
121
+ title: 'New task assigned'
122
+ }
123
+ }
124
+ },
125
+ recipients: [{
126
+ email: 'joe@example.com'
127
+ }],
96
128
  )
97
129
  ```
98
130
 
99
131
  ### Fetch a user's notifications
100
132
 
101
- Fetch a user's notifications
133
+ To fetch a user's notifications you can do this:
102
134
 
103
135
  ```ruby
136
+ require 'magicbell'
137
+
104
138
  magicbell = MagicBell::Client.new
105
- user = magicbell.user_with_email("joe@example.com")
106
- user_notifications = user.notifications
107
- user_notifications.each { |user_notification| puts user_notification.attribute("title") }
139
+
140
+ user = magicbell.user_with_email('joe@example.com')
141
+ user.notifications.each { |notification| puts notification.attribute('title') }
108
142
  ```
109
143
 
110
- Please note that the example above fetches the user's 15 most recent notification (the default number of notifications per page) If you'd like to fetch subsequent pages, use
144
+ Please note that the example above fetches the user's 15 most recent notifications (the default number per page). If you'd like to fetch subsequent pages, use the `each_page` method instead:
111
145
 
112
146
  ```ruby
147
+ require 'magicbell'
148
+
113
149
  magicbell = MagicBell::Client.new
114
- user = magicbell.user_with_email("joe@example.com")
115
- user_notifications = user.notifications
116
- user_notifications.each_page do |page|
117
- page.each do |user_notification|
118
- puts user_notification.attribute("title")
150
+
151
+ user = magicbell.user_with_email('joe@example.com')
152
+ user.notifications.each_page do |page|
153
+ page.each do |notification|
154
+ puts notification.attribute('title')
119
155
  end
120
156
  end
121
157
  ```
122
158
 
123
- ### Mark a user notification as read/unread
159
+ ### Mark a user's notification as read/unread
124
160
 
125
161
  ```ruby
162
+ require 'magicbell'
163
+
126
164
  magicbell = MagicBell::Client.new
127
- user = magicbell.user_with_email("joe@example.com")
128
- user_notification = user.notifications.first
129
- user_notification.mark_as_read
130
- user_notification.mark_as_unread
165
+
166
+ user = magicbell.user_with_email('joe@example.com')
167
+
168
+ notification = user.notifications.first
169
+ notification.mark_as_read
170
+ notification.mark_as_unread
131
171
  ```
132
172
 
133
- ### Mark all notifications of a user as read/seen
173
+ ### Mark all notifications of a user as read
134
174
 
135
175
  ```ruby
176
+ require 'magicbell'
177
+
136
178
  magicbell = MagicBell::Client.new
137
- user = magicbell.user_with_email("joe@example.com")
179
+
180
+ user = magicbell.user_with_email('joe@example.com')
138
181
  user.mark_all_notifications_as_read
182
+ ```
183
+
184
+ ### Mark all notifications of a user as seen
185
+
186
+ ```ruby
187
+ require 'magicbell'
188
+
189
+ magicbell = MagicBell::Client.new
190
+
191
+ user = magicbell.user_with_email('joe@example.com')
139
192
  user.mark_all_notifications_as_seen
140
193
  ```
141
194
 
142
195
  ### Error handling
143
196
 
144
- Please note that the gem raises a `MagicBell::Client::HTTPError` if an API returns a non 2xx response
197
+ This gem raises a `MagicBell::Client::HTTPError` if the MagicBell API returns a non-2xx response.
145
198
 
146
199
  ```ruby
200
+ require 'magicbell'
201
+
147
202
  begin
148
203
  magicbell = MagicBell::Client.new
149
204
  magicbell.create_notification(
150
- "title" => "Rob assigned to a task to you"
205
+ title: 'Rob assigned to a task to you'
151
206
  )
152
207
  rescue MagicBell::Client::HTTPError => e
153
- # Report the error to your error tracker
208
+ # Report the error to your error tracker, for example
154
209
  error_context = {
155
210
  response_status: e.response_status,
156
211
  response_headers: e.response_headers,
157
212
  response_body: e.response_body
158
213
  }
159
- ErrorReporter.report(e, context: error_context)
160
- end
161
- ```
162
214
 
163
- ## Rails integration
164
-
165
- If you've existing ActionMailer email notifications in your Rails app and prefer to not spend time migrating to MagicBell's API, you can blind copy your ActionMailer email notifications to MagicBell. MagicBell will create in-app notifications from email notifications blind copied to it.
166
-
167
- Call the `ring_the_magicbell` method in your action mailers like in the example below
168
-
169
- ```ruby
170
- class NotificationMailer < ActionMailer::Base
171
- # The ring_the_magicbell method will bcc your email notifications to your MagicBell project's BCC email address
172
- #
173
- # Upon receiving a blind copied email notification, magicbell.io will automatically create an in-app notification for the user
174
- ring_the_magicbell
175
-
176
- # This is an email notification in your app
177
- def new_comment
178
- # ...
179
- end
180
-
181
- # This is another email notification in your app
182
- def mentioned
183
- # ...
184
- end
185
- end
186
- ```
187
-
188
- The gem will automatically pick your MagicBell project's BCC email from the `MAGICBELL_BCC_EMAIL` environment variable. Alternatively, provide your MagicBell project's BCC email address in an initializer
189
-
190
- ```
191
- vim config/initializers/magicbell.rb
192
- ```
193
-
194
- ```ruby
195
- # config/initializers/magicbell.rb
196
-
197
- MagicBell.configure do |config|
198
- config.api_key = "your_magicbell_api_key"
199
- config.api_secret = "your_magicbell_api_secret"
200
- config.bcc_email = "your_magicbell_bcc_email@ring.magicbell.io"
215
+ ErrorReporter.report(e, context: error_context)
201
216
  end
202
217
  ```
203
218
 
204
- The BCC email address is available in the "Settings" section in your MagicBell dashboard.
205
-
206
- ### Customize Action URL
219
+ ### Calculate the HMAC secret for a user
207
220
 
208
- When a user clicks on a notification in MagicBell's Notification Inbox, the Notification Inbox redirects the user to the first URL the body of the email notification. This URL is called the `action_url`.
209
-
210
- If you wish to redirect users to a different URL instead, provide an `action_url` in your mailers
221
+ You can use the `MagicBell.hmac` method. Note that in this case, the API secret, which is used to generate the HMAC, will be fetched from the global configuration.
211
222
 
212
223
  ```ruby
213
- class NotificationMailer < ActionMailer::Base
214
- ring_the_magicbell
224
+ require 'magicbell'
215
225
 
216
- def new_comment(comment)
217
- # ...
218
- magicbell_notification_action_url("https://myapp.com/comments/#{comment.id}")
219
- # ...
220
- end
221
- end
226
+ hmac = MagicBell.hmac('joe@example.com')
222
227
  ```
223
228
 
224
- ### Customize Notification Title
225
-
226
- The Notification inbox will use the subject of the email notification as a notification's title. If this behaviour isn't sutiable for your app, provide a title in your mailers
229
+ You can also use the API secret of a specific client instance to calculate the HMAC:
227
230
 
228
231
  ```ruby
229
- class NotificationMailer < ActionMailer::Base
230
- ring_the_magicbell
231
-
232
- def new_comment(comment)
233
- # ...
234
- magicbell_notification_title("Richard posted a new comment")
235
- # ...
236
- end
237
- end
238
- ```
232
+ require 'magicbell'
239
233
 
240
- ## HMAC Authentication
241
-
242
- ### Calculate HMAC
234
+ magicbell = MagicBell::Client.new(
235
+ api_key: 'MAGICBELL_API_KEY',
236
+ api_secret: 'MAGICBELL_API_SECRET'
237
+ )
243
238
 
244
- ```
245
- user_email = "joe@example.com"
246
- hmac = MagicBell.hmac(user_email)
239
+ hmac = magicbell.hmac('joe@example.com')
247
240
  ```
248
241
 
249
- See https://developer.magicbell.io/docs/turn-on-hmac-authentication for more information on turning on HMAC Authentication for your MagicBell Project
242
+ Please refer to our docs to know [how to turn on HMAC authentication](https://magicbell.com/docs/turn-on-hmac-authentication) for your MagicBell project.
250
243
 
251
244
  ## API docs
252
245
 
253
- Please visit our website https://magicbell.io and our API docs https://developer.magicbell.io for more information MagicBell's embeddable notification inbox and MagicBell's REST API
246
+ Please visit [our website](https://magicbell.com) and [our docs](https://magicbell.com/docs) for more information.
254
247
 
255
248
  ## Contact Us
256
249
 
257
- Have a query or hit upon a problem? Create a post in our Developer Community https://community.magicbell.io or contact us at hello@magicbell.io
258
-
259
- ```
260
-
261
- ```
250
+ Have a query or hit upon a problem? Feel free to contact us at [hello@magicbell.io](mailto:hello@magicbell.io).
data/lib/magicbell.rb CHANGED
@@ -32,7 +32,6 @@ module MagicBell
32
32
 
33
33
  def_delegators :config, :api_key,
34
34
  :api_secret,
35
- :bcc_email,
36
35
  :api_host
37
36
 
38
37
  def configure
@@ -47,25 +46,16 @@ module MagicBell
47
46
  @config = Config.new
48
47
  end
49
48
 
50
- def authentication_headers
49
+ def authentication_headers(client_api_key: nil, client_api_secret: nil)
51
50
  {
52
- "X-MAGICBELL-API-KEY" => api_key,
53
- "X-MAGICBELL-API-SECRET" => api_secret
51
+ "X-MAGICBELL-API-KEY" => client_api_key || api_key,
52
+ "X-MAGICBELL-API-SECRET" => client_api_secret || api_secret
54
53
  }
55
54
  end
56
55
 
57
56
  # Calculate HMAC for user's email
58
57
  def hmac(message)
59
- digest = sha256_digest
60
- secret = api_secret
61
-
62
- Base64.encode64(OpenSSL::HMAC.digest(digest, secret, message)).strip
63
- end
64
-
65
- private
66
-
67
- def sha256_digest
68
- OpenSSL::Digest::Digest.new('sha256')
58
+ MagicBell::Client.new(api_key: api_key, api_secret: api_secret).hmac(message)
69
59
  end
70
60
  end
71
61
  end
@@ -2,16 +2,6 @@ require "json"
2
2
 
3
3
  module MagicBell
4
4
  module ActionMailerExtension
5
- def self.included(mailer_class)
6
- mailer_class.send :extend, ClassMethods
7
- end
8
-
9
- module ClassMethods
10
- def ring_the_magicbell
11
- default bcc: MagicBell.bcc_email
12
- end
13
- end
14
-
15
5
  def magicbell_notification_action_url(action_url)
16
6
  headers["X-MagicBell-Notification-ActionUrl"] = action_url
17
7
  end
@@ -9,6 +9,11 @@ module MagicBell
9
9
 
10
10
  include ApiOperations
11
11
 
12
+ def initialize(api_key: nil, api_secret: nil)
13
+ @api_key = api_key
14
+ @api_secret = api_secret
15
+ end
16
+
12
17
  def create_notification(notification_attributes)
13
18
  MagicBell::Notification.create(self, notification_attributes)
14
19
  end
@@ -23,7 +28,19 @@ module MagicBell
23
28
  end
24
29
 
25
30
  def authentication_headers
26
- MagicBell.authentication_headers
31
+ MagicBell.authentication_headers(client_api_key: @api_key, client_api_secret: @api_secret)
32
+ end
33
+
34
+ def hmac(message)
35
+ secret = @api_secret || MagicBell.api_secret
36
+
37
+ Base64.encode64(OpenSSL::HMAC::digest(sha256_digest, secret, message)).strip
38
+ end
39
+
40
+ private
41
+
42
+ def sha256_digest
43
+ OpenSSL::Digest::Digest.new('sha256')
27
44
  end
28
45
  end
29
46
  end
@@ -2,7 +2,6 @@ module MagicBell
2
2
  class Config
3
3
  attr_writer :api_key
4
4
  attr_writer :api_secret
5
- attr_writer :bcc_email
6
5
  attr_writer :api_host
7
6
 
8
7
  def initialize
@@ -17,10 +16,6 @@ module MagicBell
17
16
  @api_secret || ENV["MAGICBELL_API_SECRET"]
18
17
  end
19
18
 
20
- def bcc_email
21
- @bcc_email || ENV["MAGICBELL_BCC_EMAIL"]
22
- end
23
-
24
19
  def api_host
25
20
  @api_host || ENV["MAGICBELL_API_HOST"] || "https://api.magicbell.io"
26
21
  end
@@ -1,3 +1,3 @@
1
1
  module MagicBell
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
metadata CHANGED
@@ -1,15 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magicbell
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hana Mohan
8
8
  - Nisanth Chunduru
9
+ - Rahmane Ousmane
10
+ - Josue Montano
9
11
  autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
12
- date: 2021-06-23 00:00:00.000000000 Z
14
+ date: 2021-07-06 00:00:00.000000000 Z
13
15
  dependencies:
14
16
  - !ruby/object:Gem::Dependency
15
17
  name: httparty
@@ -123,10 +125,12 @@ dependencies:
123
125
  - - ">="
124
126
  - !ruby/object:Gem::Version
125
127
  version: '0'
126
- description: Notifications like never before!
128
+ description: The notification inbox for your product
127
129
  email:
128
130
  - hana@magicbell.io
129
131
  - nisanth@supportbee.com
132
+ - rahmane@magicbell.io
133
+ - josue@magicbell.io
130
134
  executables: []
131
135
  extensions: []
132
136
  extra_rdoc_files: []
@@ -153,11 +157,13 @@ files:
153
157
  - lib/magicbell/railtie.rb
154
158
  - lib/magicbell/singleton_api_resource.rb
155
159
  - lib/magicbell/version.rb
156
- homepage: https://magicbell.io
160
+ homepage: https://magicbell.com
157
161
  licenses:
158
162
  - MIT
159
163
  metadata: {}
160
- post_install_message:
164
+ post_install_message: "\n *** Breaking Change:: The 2.0.0 release removes the BCC
165
+ functionality. ***\n Please update your MagicBell integration to use the API
166
+ for creating notifications or downgrade to 1.0.4\n "
161
167
  rdoc_options: []
162
168
  require_paths:
163
169
  - lib
@@ -172,8 +178,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
178
  - !ruby/object:Gem::Version
173
179
  version: '0'
174
180
  requirements: []
175
- rubygems_version: 3.1.4
181
+ rubygems_version: 3.1.6
176
182
  signing_key:
177
183
  specification_version: 4
178
- summary: Ruby wrapper for MagicBell.io
184
+ summary: Ruby Library for MagicBell
179
185
  test_files: []