Class: SmsBroker::VoiceMessageSender

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ VoiceMessageSender

Returns a new instance of VoiceMessageSender.



7
8
9
# File 'lib/sms_broker/voice_message_sender.rb', line 7

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#voice_message_textObject (readonly)

Returns the value of attribute voice_message_text.



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

def voice_message_text
  @voice_message_text
end

Instance Method Details

#build_message(from = :sender_id) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/sms_broker/voice_message_sender.rb', line 69

def build_message(from = :sender_id)
  client.is_a?(SmsBroker::Client::Nexmo) && tag_message

  {
    text: @voice_message_text,
    from: get_sender(from),
    to:   @voice_message_to
  }.merge!(@voice_message_options || {})
end

#deliverObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sms_broker/voice_message_sender.rb', line 29

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

  response = client.send_voice_message(build_message)

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

  response
end

#message(text) ⇒ Object



17
18
19
20
21
# File 'lib/sms_broker/voice_message_sender.rb', line 17

def message(text)
  @voice_message_text = text

  self
end

#to(number) ⇒ Object



11
12
13
14
15
# File 'lib/sms_broker/voice_message_sender.rb', line 11

def to(number)
  @voice_message_to = number

  self
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sms_broker/voice_message_sender.rb', line 43

def valid?
  options = @voice_message_options || {}

  schema = {
    message: Compel.string.required.max_length(140),
    to: Compel.string.required,
    lang: Compel.string,
    rate: Compel.integer,
    repeat: Compel.integer
  }

  object = {
    message: @voice_message_text,
    to: @voice_message_to,
    lang: options[:lang],
    rate: options[:rate],
    repeat: options[:repeat]
  }

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

  @errors = result.errors

  result.valid?
end

#with(options) ⇒ Object



23
24
25
26
27
# File 'lib/sms_broker/voice_message_sender.rb', line 23

def with(options)
  @voice_message_options = options

  self
end