class FakeTwilio::Message

Minimal implementation to mimic sending of SMS messages.

Attributes

body[R]
sent[R]
success[R]
to[R]

Public Class Methods

new(params) click to toggle source

Mimic processing a Message.

# File lib/fake_twilio/message.rb, line 7
def initialize(params)
  @success = verify_params(params)
  @to      = params['To']
  @body    = params['Body']
  @sent    = Time.now
end

Public Instance Methods

date() click to toggle source

Represent the date sent. If the attempt to send an SMS failed, do not list the date.

# File lib/fake_twilio/message.rb, line 23
def date
  success ? { 'datesent' => sent } : {}
end
to_json() click to toggle source

Produce output representing whether the SMS succeeded or failed.

# File lib/fake_twilio/message.rb, line 15
def to_json
  [status, price, date]
    .reduce(&:merge)
    .to_json
end

Private Instance Methods

price() click to toggle source

Represent the cost to send the message. Possibilities are constrained to 3 cents and nothing, predicated on success.

# File lib/fake_twilio/message.rb, line 37
def price
  { 'price' => success ? '0.03' : '0.00' }
end
status() click to toggle source

Represent the state of the message. Since no actual process exists, constrain possible statuses to success or failure.

# File lib/fake_twilio/message.rb, line 31
def status
  { 'status' => success ? 'sent' : 'failed' }
end
verify_params(params) click to toggle source

Implement minimal checking for the parameters. The body must be between 1 and 1600 bytes and a recipient must be specified.

# File lib/fake_twilio/message.rb, line 43
def verify_params(params)
  return false if params['Body'].to_s.empty?
  return false if params['Body'].to_s.bytes.size > 1600
  return false unless /\+\d+$/.match(params['To'])
  true
end