Last active
October 11, 2018 22:18
-
-
Save apneadiving/b34f87257f4179e6dc8822a02e37fd47 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
source "https://rubygems.org" | |
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | |
gem 'twilio-ruby' | |
gem 'dotenv' | |
gem 'waterfall' |
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
require 'dotenv/load' | |
require 'waterfall' | |
require_relative 'waterfall_custom' | |
require_relative 'services/send_sms' | |
puts "Enter Phone Number" | |
number = gets.chomp! | |
puts "Enter Message" | |
message = gets.chomp! | |
Flow.new | |
.chain(twilio_result: :result) do | |
Services::SendSms.new(number, message) | |
end | |
.chain { puts 'Message sent successfully' } | |
.chain {|outflow| puts outflow.twilio_result } | |
.on_dam {|errors| puts errors } |
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
require_relative 'common/errors' | |
module Waterfall | |
def errors | |
@errors ||= Services::Common::Errors.new | |
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
require_relative 'clients/twilio' | |
module Services | |
class SendSms | |
include Waterfall | |
def initialize(recipient, message) | |
@recipient = recipient | |
@message = message | |
end | |
def call | |
chain do | |
errors.add :validation, "Missing recipient's number." if @recipient.empty? | |
errors.add :validation, "Missing message to recipient." if @message.empty? | |
dam(errors) if errors.any? | |
end | |
chain(result: :client) do | |
Services::Clients::Twilio.new(@recipient, @message) | |
end | |
end | |
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
require 'twilio-ruby' | |
module Services | |
module Clients | |
class Twilio | |
include Waterfall | |
PHONE_NUMBER = '+18124322807' | |
def initialize(recipient, message) | |
@recipient = recipient | |
@message = message | |
end | |
def call | |
chain(:client) { send_message } | |
end | |
private | |
def send_message | |
account_id = ENV['TWILIO_ACCOUNT_ID'] | |
auth_token = ENV['TWILIO_AUTH_TOKEN'] | |
client = ::Twilio::REST::Client.new account_id, auth_token | |
client.api.account.messages.create( | |
from: PHONE_NUMBER, | |
to: @recipient, | |
body: @message | |
) | |
client | |
rescue ::Twilio::REST::RestError => error | |
errors.add :sms, error.message | |
dam(errors) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment