Skip to content

Commit 8e2dc61

Browse files
committed
Merge branch 'main' of github.com:excid3/noticed
2 parents 68c406f + 7cb10d1 commit 8e2dc61

File tree

10 files changed

+67
-8
lines changed

10 files changed

+67
-8
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
push:
88
branches:
99
- main
10+
workflow_call:
1011

1112
jobs:
1213
sqlite:

.github/workflows/publish_gem.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ on:
88
type: string
99

1010
jobs:
11+
test:
12+
uses: ./.github/workflows/ci.yml
13+
1114
push:
15+
needs: test
1216
runs-on: ubuntu-latest
1317

1418
permissions:
@@ -27,7 +31,7 @@ jobs:
2731
- name: Update version
2832
run: |
2933
sed -i 's/".*"/"${{ inputs.version }}"/' lib/noticed/version.rb
30-
bundle config set frozen false
34+
bundle config set --local deployment 'false'
3135
bundle
3236
bundle exec appraisal
3337
git config user.name 'GitHub Actions'

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ class DeliveryMethods::WhatsApp < Noticed::DeliveryMethod
645645
def deliver
646646
# ...
647647
config.day #=> #<Proc:0x000f7c8 (lambda)>
648-
evaluate_option(config.day) #=> "Tuesday"
648+
evaluate_option(:day) #=> "Tuesday"
649649
end
650650
end
651651
```

app/models/concerns/noticed/deliverable.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ def param(*names)
7070
end
7171

7272
def with(params)
73-
record = params.delete(:record)
74-
new(params: params, record: record)
73+
if self < Ephemeral
74+
new(params: params)
75+
else
76+
record = params.delete(:record)
77+
new(params: params, record: record)
78+
end
7579
end
7680

7781
def deliver(recipients = nil, **options)

docs/delivery_methods/fcm.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Customize the Firebase Cloud Messaging notification object. This can be a Lambda
5050

5151
The callable object will be given the device token as an argument.
5252

53-
There are lots of options of now to structure a FCM notification message. See https://firebase.google.com/docs/cloud-messaging/concept-options for more details.
53+
There are lots of options of how to structure a FCM notification message. See https://firebase.google.com/docs/cloud-messaging/concept-options for more details.
5454

5555
### `credentials`
5656
The location of your Firebase Cloud Messaging credentials.

docs/delivery_methods/ios.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Here, the recipient `has_many :notification_tokens` with columns `platform` and
7474

7575
```ruby
7676
deliver_by :ios do |config|
77-
config.device_tokens = ->(recipient) { recipient.notification_tokens.where(platform: :iOS).pluck(:token) }
77+
config.device_tokens = -> { recipient.notification_tokens.where(platform: :iOS).pluck(:token) }
7878
end
7979
```
8080

docs/delivery_methods/twilio_messaging.md

+19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ deliver_by :twilio_messaging do |config|
3838
end
3939
```
4040

41+
## Error Handling
42+
43+
Twilio provides a full list of error codes that can be handled as needed. See https://www.twilio.com/docs/api/errors
44+
45+
```ruby
46+
deliver_by :twilio_messaging do |config|
47+
config.error_handler = lambda do |twilio_error_response|
48+
error_hash = JSON.parse(twilio_error_response.body)
49+
case error_hash["code"]
50+
when 21211
51+
# The 'To' number is not a valid phone number.
52+
# Write your error handling code
53+
else
54+
raise "Unhandled Twilio error: #{error_hash}"
55+
end
56+
end
57+
end
58+
```
59+
4160
## Options
4261

4362
* `json` - *Optional*

lib/noticed/delivery_methods/twilio_messaging.rb

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ module DeliveryMethods
33
class TwilioMessaging < DeliveryMethod
44
def deliver
55
post_request url, basic_auth: {user: account_sid, pass: auth_token}, form: json.stringify_keys
6+
rescue Noticed::ResponseUnsuccessful => exception
7+
if exception.response.code.start_with?("4") && config[:error_handler]
8+
notification.instance_exec(exception.response, &config[:error_handler])
9+
else
10+
raise
11+
end
612
end
713

814
def json

test/delivery_methods/twilio_messaging_test.rb

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class TwilioMessagingTest < ActiveSupport::TestCase
44
setup do
55
@delivery_method = Noticed::DeliveryMethods::TwilioMessaging.new
6-
set_config(
6+
@config = {
77
account_sid: "acct_1234",
88
auth_token: "token",
99
json: -> {
@@ -13,7 +13,8 @@ class TwilioMessagingTest < ActiveSupport::TestCase
1313
Body: "Hello world"
1414
}
1515
}
16-
)
16+
}
17+
set_config(@config)
1718
end
1819

1920
test "sends sms" do
@@ -38,6 +39,26 @@ class TwilioMessagingTest < ActiveSupport::TestCase
3839
end
3940
end
4041

42+
test "passes error to notification instance if error_handler is configured" do
43+
@delivery_method = Noticed::DeliveryMethods::TwilioMessaging.new(
44+
"delivery_method_name",
45+
noticed_notifications(:one)
46+
)
47+
48+
error_handler_called = false
49+
@config[:error_handler] = lambda do |twilio_error_message|
50+
error_handler_called = true
51+
end
52+
set_config(@config)
53+
54+
stub_request(:post, "https://api.twilio.com/2010-04-01/Accounts/acct_1234/Messages.json").to_return(status: 422)
55+
assert_nothing_raised do
56+
@delivery_method.deliver
57+
end
58+
59+
assert(error_handler_called, "Handler is called if status is 4xx")
60+
end
61+
4162
private
4263

4364
def set_config(config)

test/ephemeral_notifier_test.rb

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ class EphemeralNotifierTest < ActiveSupport::TestCase
1313
perform_enqueued_jobs
1414
end
1515
end
16+
17+
test "ephemeral has record shortcut" do
18+
assert_equal :foo, EphemeralNotifier.with(record: :foo).record
19+
end
1620
end

0 commit comments

Comments
 (0)