Skip to content

Commit

Permalink
154
Browse files Browse the repository at this point in the history
  • Loading branch information
kobaltz committed Oct 8, 2018
0 parents commit 3177d5f
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
8 changes: 8 additions & 0 deletions Gemfile
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'
25 changes: 25 additions & 0 deletions Gemfile.lock
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
21 changes: 21 additions & 0 deletions main.rb
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

33 changes: 33 additions & 0 deletions services/clients/twilio.rb
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
23 changes: 23 additions & 0 deletions services/common/errors.rb
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
33 changes: 33 additions & 0 deletions services/object.rb
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
25 changes: 25 additions & 0 deletions services/send_sms.rb
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

0 comments on commit 3177d5f

Please sign in to comment.