Skip to content

Instantly share code, notes, and snippets.

@apneadiving
Last active October 11, 2018 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apneadiving/b34f87257f4179e6dc8822a02e37fd47 to your computer and use it in GitHub Desktop.
Save apneadiving/b34f87257f4179e6dc8822a02e37fd47 to your computer and use it in GitHub Desktop.
# 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'
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 }
require_relative 'common/errors'
module Waterfall
def errors
@errors ||= Services::Common::Errors.new
end
end
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
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