Last active
November 29, 2018 16:14
-
-
Save zspencer/296b2e4f64242f7226f9f28a91c472ea to your computer and use it in GitHub Desktop.
Implementation Strategies for Integrating External Services in your Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN); | |
class Person { | |
sendVerificationCode() { | |
client.messages | |
.create({ | |
body: i18n__('verification_code', { verificationCode: this.verificationCode }), | |
to: this.mobileNumber, from: process.env.TWILIO_OUTGOING_NUMBER | |
}) | |
.done(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
def send_verification_code | |
twilio = Twilio::REST::Client.new(ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']) | |
twilio.messages.create(body: I18n.t('verification_code', { verification_code: verification_code }), | |
to: mobile_number, from: ENV['TWILIO_OUTGOING_NUMBER']) | |
rescue Twilio::Rest::TwilioError => e | |
# If we can't send the message, don't explode, log a warning, and send the exception to Sentry. | |
Rails.logger.warn("Unable to deliver verification code to person #{person.id} exception: #{e.message}") | |
Raven.capture_exception(e) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sms | |
const client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN); | |
export function sendMessage({ to, body }) { | |
return client.messages | |
.create({ body, to, from: process.env.TWILIO_OUTGOING_NUMBER }) | |
.done(); | |
} | |
// Consumer: | |
// | |
// import { sendMessage } from '../lib/sms' | |
// class Person { | |
// sendVerificationCode() { | |
// sms({ to: this.mobileNumber, body: i18n__('verification_code', { verificationCode: this.verificationCode })) | |
// } | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SMS | |
class Client | |
def send(to:, body:) | |
twilio = Twilio::REST::Client.new(ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']) | |
twilio.messages.create(body: body, to: to, from: ENV['TWILIO_OUTGOING_NUMBER']) | |
rescue Twilio::Rest::TwilioError => e | |
Rails.logger.error("Delivery error sending to #{to} via Twilio: #{e.message}" | |
raise DeliveryError.new(e) | |
end | |
end | |
class DeliveryError < StandardError | |
def initialize(original) | |
super original | |
set_backtrace original.backtrace if original | |
end | |
end | |
end | |
# Consumer | |
# Class Person | |
# def send_verification_code | |
# SMS::Client.new.send(to: mobile_number, body: body: I18n.t('verification_code', { verification_code: verification_code }))= | |
# rescue SMS::DeliveryError => e | |
# # If we can't send the message, don't explode, log a warning, and send the exception to Sentry. | |
# Rails.logger.warn("Unable to deliver verification code to person #{person.id} exception: #{e.message}") | |
# Raven.capture_exception(e) | |
# end | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment