-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3177d5f
Showing
8 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | ||
|
||
gem 'twilio-ruby' | ||
gem 'dotenv' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
dotenv (2.5.0) | ||
faraday (0.15.3) | ||
multipart-post (>= 1.2, < 3) | ||
jwt (2.1.0) | ||
mini_portile2 (2.3.0) | ||
multipart-post (2.0.0) | ||
nokogiri (1.8.5) | ||
mini_portile2 (~> 2.3.0) | ||
twilio-ruby (5.14.1) | ||
faraday (~> 0.9) | ||
jwt (>= 1.5, <= 2.5) | ||
nokogiri (>= 1.6, < 2.0) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
dotenv | ||
twilio-ruby | ||
|
||
BUNDLED WITH | ||
1.16.6 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'dotenv/load' | ||
require_relative 'services/send_sms' | ||
|
||
puts "Enter Phone Number" | ||
number = gets.chomp! | ||
|
||
puts "Enter Message" | ||
message = gets.chomp! | ||
|
||
request = Services::SendSms.call(number, message) | ||
|
||
if request.success? | ||
puts 'Message sent successfully' | ||
elsif request.failure? | ||
puts request.errors | ||
end | ||
|
||
puts request.success? | ||
puts request.result | ||
puts request.result.result.class | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'twilio-ruby' | ||
module Services | ||
module Clients | ||
class Twilio < Services::Object | ||
PHONE_NUMBER = '+18124322807' | ||
|
||
def initialize(recipient, message) | ||
@recipient = recipient | ||
@message = message | ||
end | ||
|
||
def call | ||
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 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Services | ||
module Common | ||
class Errors < Hash | ||
def add(key, value, _opts = {}) | ||
self[key] ||= [] | ||
self[key] << value | ||
self[key].uniq! | ||
end | ||
|
||
def add_multiple_errors(errors_hash) | ||
errors_hash.each do |key, values| | ||
errors_hash[key].each { |value| add key, value } | ||
end | ||
end | ||
|
||
def each | ||
each_key do |field| | ||
self[field].each { |message| yield field, message } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require_relative 'common/errors' | ||
|
||
module Services | ||
class Object | ||
class << self | ||
def call(*arg) | ||
new(*arg).constructor | ||
end | ||
end | ||
|
||
attr_reader :result | ||
def constructor | ||
@result = call | ||
self | ||
end | ||
|
||
def success? | ||
!failure? | ||
end | ||
|
||
def failure? | ||
errors.any? | ||
end | ||
|
||
def errors | ||
@errors ||= Services::Common::Errors.new | ||
end | ||
|
||
def call | ||
fail NotImplementedError unless defined?(super) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require_relative 'object' | ||
require_relative 'clients/twilio' | ||
|
||
module Services | ||
class SendSms < Services::Object | ||
def initialize(recipient, message) | ||
@recipient = recipient | ||
@message = message | ||
end | ||
|
||
def call | ||
errors.add :validation, "Missing recipient's number." if @recipient.empty? | ||
errors.add :validation, "Missing message to recipient." if @message.empty? | ||
send_message unless errors.any? | ||
end | ||
|
||
private | ||
|
||
def send_message | ||
request = Services::Clients::Twilio.call(@recipient, @message) | ||
errors.add_multiple_errors(request.errors) if request.failure? | ||
request | ||
end | ||
end | ||
end |