Skip to content

Commit

Permalink
Merge branch 'main' of github.com:excid3/noticed
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed May 23, 2024
2 parents 68c406f + 7cb10d1 commit 8e2dc61
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- main
workflow_call:

jobs:
sqlite:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/publish_gem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ on:
type: string

jobs:
test:
uses: ./.github/workflows/ci.yml

push:
needs: test
runs-on: ubuntu-latest

permissions:
Expand All @@ -27,7 +31,7 @@ jobs:
- name: Update version
run: |
sed -i 's/".*"/"${{ inputs.version }}"/' lib/noticed/version.rb
bundle config set frozen false
bundle config set --local deployment 'false'
bundle
bundle exec appraisal
git config user.name 'GitHub Actions'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ class DeliveryMethods::WhatsApp < Noticed::DeliveryMethod
def deliver
# ...
config.day #=> #<Proc:0x000f7c8 (lambda)>
evaluate_option(config.day) #=> "Tuesday"
evaluate_option(:day) #=> "Tuesday"
end
end
```
Expand Down
8 changes: 6 additions & 2 deletions app/models/concerns/noticed/deliverable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ def param(*names)
end

def with(params)
record = params.delete(:record)
new(params: params, record: record)
if self < Ephemeral
new(params: params)
else
record = params.delete(:record)
new(params: params, record: record)
end
end

def deliver(recipients = nil, **options)
Expand Down
2 changes: 1 addition & 1 deletion docs/delivery_methods/fcm.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Customize the Firebase Cloud Messaging notification object. This can be a Lambda

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

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

### `credentials`
The location of your Firebase Cloud Messaging credentials.
Expand Down
2 changes: 1 addition & 1 deletion docs/delivery_methods/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Here, the recipient `has_many :notification_tokens` with columns `platform` and

```ruby
deliver_by :ios do |config|
config.device_tokens = ->(recipient) { recipient.notification_tokens.where(platform: :iOS).pluck(:token) }
config.device_tokens = -> { recipient.notification_tokens.where(platform: :iOS).pluck(:token) }
end
```

Expand Down
19 changes: 19 additions & 0 deletions docs/delivery_methods/twilio_messaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ deliver_by :twilio_messaging do |config|
end
```

## Error Handling

Twilio provides a full list of error codes that can be handled as needed. See https://www.twilio.com/docs/api/errors

```ruby
deliver_by :twilio_messaging do |config|
config.error_handler = lambda do |twilio_error_response|
error_hash = JSON.parse(twilio_error_response.body)
case error_hash["code"]
when 21211
# The 'To' number is not a valid phone number.
# Write your error handling code
else
raise "Unhandled Twilio error: #{error_hash}"
end
end
end
```

## Options

* `json` - *Optional*
Expand Down
6 changes: 6 additions & 0 deletions lib/noticed/delivery_methods/twilio_messaging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ module DeliveryMethods
class TwilioMessaging < DeliveryMethod
def deliver
post_request url, basic_auth: {user: account_sid, pass: auth_token}, form: json.stringify_keys
rescue Noticed::ResponseUnsuccessful => exception
if exception.response.code.start_with?("4") && config[:error_handler]
notification.instance_exec(exception.response, &config[:error_handler])
else
raise
end
end

def json
Expand Down
25 changes: 23 additions & 2 deletions test/delivery_methods/twilio_messaging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class TwilioMessagingTest < ActiveSupport::TestCase
setup do
@delivery_method = Noticed::DeliveryMethods::TwilioMessaging.new
set_config(
@config = {
account_sid: "acct_1234",
auth_token: "token",
json: -> {
Expand All @@ -13,7 +13,8 @@ class TwilioMessagingTest < ActiveSupport::TestCase
Body: "Hello world"
}
}
)
}
set_config(@config)
end

test "sends sms" do
Expand All @@ -38,6 +39,26 @@ class TwilioMessagingTest < ActiveSupport::TestCase
end
end

test "passes error to notification instance if error_handler is configured" do
@delivery_method = Noticed::DeliveryMethods::TwilioMessaging.new(
"delivery_method_name",
noticed_notifications(:one)
)

error_handler_called = false
@config[:error_handler] = lambda do |twilio_error_message|
error_handler_called = true
end
set_config(@config)

stub_request(:post, "https://api.twilio.com/2010-04-01/Accounts/acct_1234/Messages.json").to_return(status: 422)
assert_nothing_raised do
@delivery_method.deliver
end

assert(error_handler_called, "Handler is called if status is 4xx")
end

private

def set_config(config)
Expand Down
4 changes: 4 additions & 0 deletions test/ephemeral_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class EphemeralNotifierTest < ActiveSupport::TestCase
perform_enqueued_jobs
end
end

test "ephemeral has record shortcut" do
assert_equal :foo, EphemeralNotifier.with(record: :foo).record
end
end

0 comments on commit 8e2dc61

Please sign in to comment.