Skip to content

Instantly share code, notes, and snippets.

@unixsuperhero
Created September 29, 2015 23:30
Show Gist options
  • Save unixsuperhero/be49ec39917dd7306dfe to your computer and use it in GitHub Desktop.
Save unixsuperhero/be49ec39917dd7306dfe to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'twilio-ruby'
args = ARGV.clone
person = args.shift
text = args.join ' '
class BaseSms
def client
@client ||= Twilio::REST::Client.new(account_sid, auth_token)
end
def account
@client && @client.account
end
def messages
account && account.messages
end
private
def account_sid
'AC820c6e45190f3ed928574057ca7b333e'
end
def auth_token
'b3560f0a2a16f109b8d8833ded97b268'
end
def twilio_number
'+15005550006'
end
end
class Sms1 < BaseSms
class << self
def from_args(args=ARGV.clone)
send(*args)
end
def send(to, *msg)
new([to, msg].flatten).tap(&:send)
end
end
attr_accessor :args
attr_accessor :receiver, :message
attr_accessor :return_value
def initialize(args=ARGV.clone)
raise Exception, '2 Arguments Expected, Only %d Received' % args.count if args.count < 2
@args = args.clone
@receiver = args.shift
@message = args.join(' ')
end
def send
return @return_value = {
to: to,
from: from,
body: message,
}
# returned early so i don't actually use someone else's twilio creds
messages.create(to: to,
from: from,
body: message)
end
def to
{
'josh' => '+9541234444',
'kirino' => '+7541234444',
'shiro' => '+8501234444',
}.fetch(receiver, receiver)
end
def from
twilio_number
end
end
puts
puts 'Sms1'
puts
sms = Sms1.from_args
puts return_value: sms.return_value
sms = Sms1.from_args(ARGV)
puts return_value: sms.return_value
sms = Sms1.send(:josh, 'this', 'is', 'the', 'message')
puts return_value: sms.return_value
sms = Sms1.send('+7543214444', 'another message')
puts return_value: sms.return_value
class Sms2 < BaseSms
class << self
def to(person)
new.tap{|sms| sms.to(person) }
end
def message(msg)
new.tap{|sms| sms.message(msg) }
end
end
attr_accessor :return_value
def initialize
@from = twilio_number
end
def to(person)
@to = people(person) || person
self
end
def message(msg)
@message = msg
self
end
def send
@return_value = {
to: @to,
from: @from,
body: @message,
}
return self
# returned early so i don't actually use someone else's twilio creds
messages.create(to: @to,
from: @from,
body: @message)
end
def people(person)
{
'josh' => '+9541234444',
'kirino' => '+7541234444',
'shiro' => '+8501234444',
}.fetch(person, person)
end
end
puts
puts 'Sms2'
puts
sms = Sms2.to(person).message(text).send
puts return_value: sms.return_value
sms = Sms2.new.to(person).message(text).send
puts return_value: sms.return_value
sms = Sms2.message(text).to(person).send
puts return_value: sms.return_value
class Sms3 < BaseSms
class << self
def send(opts={})
new(opts).tap(&:send)
end
end
attr_accessor :opts
attr_accessor :person, :text
attr_accessor :return_value
def initialize(opts={})
@person = opts[:to]
@text = opts[:message]
end
def send
@return_value = {
to: to,
from: twilio_number,
body: text,
}
end
def to
{
'josh' => '+9541234444',
}.fetch(person, person)
end
end
puts
puts 'Sms3'
puts
sms = Sms3.send(to: 'josh', message: 'helloooooooo')
puts return_value: sms.return_value
sms = Sms3.new(to: 'josh', message: 'yayayaay')
sms.send
puts return_value: sms.return_value
class Sms4 < BaseSms
class << self
def send(opts={})
new(opts).send
end
end
attr_accessor :opts
attr_accessor :person, :text
attr_accessor :response
def initialize(opts={})
@opts = opts
@person = opts[:to]
@text = opts[:message]
end
def send
successful = rand(2) == 0
if not successful
@response = Sms4::Response.unsuccessful(Exception.new('failed for some reason'))
else
@response = Sms4::Response.successful(to: to, from: from, body: text)
end
end
def to
{
'josh' => '+9541234444',
}.fetch(person, person)
end
def from
twilio_number
end
end
class Sms4
class Response
class << self
def successful(opts)
new(true).tap do |resp|
resp.opts = opts
end
end
def unsuccessful(e)
new(false).tap do |resp|
resp.err = e
end
end
end
attr_accessor :successful, :opts, :err
def initialize(successful)
puts wtf: successful
@successful = !!successful
end
def successful?
@successful == true
end
def to
opts[:to]
end
def from
opts[:from]
end
def body
opts[:body]
end
def reason
err.message
end
def backtrace
err.backtrace
end
end
end
puts
puts 'Sms4'
puts
response = Sms4.send(to: 'josh', message: 'ohayou')
puts successful?: response.successful?, opts: response.opts, error: response.err
response = Sms4.new(to: 'josh', message: 'ohayou').send
puts successful?: response.successful?, opts: response.opts, error: response.err
response = Sms4.send(to: 'josh', message: 'ohayou')
puts successful?: response.successful?, opts: response.opts, error: response.err
response = Sms4.send(to: 'josh', message: 'ohayou')
puts successful?: response.successful?, opts: response.opts, error: response.err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment