Class: SmsBroker::MessageSender

Inherits:
Object
  • Object
show all
Defined in:
lib/sms_broker/message_sender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ MessageSender

Returns a new instance of MessageSender.



6
7
8
# File 'lib/sms_broker/message_sender.rb', line 6

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/sms_broker/message_sender.rb', line 3

def client
  @client
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/sms_broker/message_sender.rb', line 3

def errors
  @errors
end

Instance Method Details

#deliverObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sms_broker/message_sender.rb', line 22

def deliver
  unless valid?
    return Client::Response::Error.new(client.name, errors, errors)
  end

  response = client.send_message(build_message)

  if should_try_with_phone_number?(response)
    return client.send_message(build_message(:phone_number))
  end

  response
end

#message(text) ⇒ Object



16
17
18
19
20
# File 'lib/sms_broker/message_sender.rb', line 16

def message(text)
  @message_text = text

  self
end

#to(number) ⇒ Object



10
11
12
13
14
# File 'lib/sms_broker/message_sender.rb', line 10

def to(number)
  @message_to = number

  self
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sms_broker/message_sender.rb', line 36

def valid?
  schema = {
    message: Compel.string.required.max_length(140),
    to: Compel.string.required
  }

  object = {
    message: @message_text,
    to: @message_to
  }

  result = Compel.hash.keys(schema).validate(object)

  @errors = result.errors

  result.valid?
end