Class: SmsTraffic::Sms

Inherits:
Object
  • Object
show all
Defined in:
lib/sms_traffic/sms.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone, message, originator: nil) ⇒ Sms

Returns a new instance of Sms.



6
7
8
9
10
11
12
13
# File 'lib/sms_traffic/sms.rb', line 6

def initialize(phone, message, originator: nil)
  @phone      = phone
  @message    = message
  @originator = originator || SmsTraffic.configuration.originator
  @status     = 'not-sent'
  @errors     = []
  validate!
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/sms_traffic/sms.rb', line 4

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/sms_traffic/sms.rb', line 4

def id
  @id
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/sms_traffic/sms.rb', line 3

def message
  @message
end

#originatorObject

Returns the value of attribute originator.



3
4
5
# File 'lib/sms_traffic/sms.rb', line 3

def originator
  @originator
end

#phoneObject

Returns the value of attribute phone.



3
4
5
# File 'lib/sms_traffic/sms.rb', line 3

def phone
  @phone
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/sms_traffic/sms.rb', line 4

def status
  @status
end

Instance Method Details

#deliverObject

rubocop:disable Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sms_traffic/sms.rb', line 15

def deliver # rubocop:disable Metrics/MethodLength
  # @type [Client::Response]
  response = Client.deliver(phone, message, originator)

  unless response.success?
    @errors << response.error_description
    return false
  end

  # @type [Client::Response::Reply]
  reply = response.reply

  if reply.ok?
    @status = 'sent'
    @id     = reply.sms_id || reply.sms_ids.values.first
    true
  else
    @errors << (reply.error_description || 'Sms has been not enqueued')
    false
  end
end

#update_statusObject



37
38
39
40
41
42
43
44
# File 'lib/sms_traffic/sms.rb', line 37

def update_status
  return status if id.nil?

  response = Client.status(id)
  return response.code unless response.success?

  @status = response.reply.status.downcase
end