Class: ActionTexter::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/action_texter/message.rb

Overview

Representation of a message

TODO: Implement these fields that Nexmo can use: :status_report_req, :network_code, :vcard, :vcal, :ttl

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = {}) ⇒ Message

Returns a new instance of Message.



19
20
21
22
23
24
# File 'lib/action_texter/message.rb', line 19

def initialize(message = {})
  self.from = message[:from] || raise("A message must contain from")
  self.to = message[:to] || raise("A message must contain to")
  self.text = message[:text] || raise("A message must contain text")
  self.reference = message[:reference]
end

Instance Attribute Details

#fromString

Returns name or phone number of the author of the message.

Returns:

  • (String)

    name or phone number of the author of the message.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_texter/message.rb', line 16

class ActionTexter::Message
  attr_accessor :from, :to, :text, :reference

  def initialize(message = {})
    self.from = message[:from] || raise("A message must contain from")
    self.to = message[:to] || raise("A message must contain to")
    self.text = message[:text] || raise("A message must contain text")
    self.reference = message[:reference]
  end

  def deliver(client = nil)
    message = ActionTexter.inform_interceptors(self)
    return nil if message.blank? # Do not send if one of the interceptors cancelled

    client ||= ActionTexter::Client.default
    if client.nil?
      raise "To deliver a message you need to specify a client by parameter to deliver or by ActionTexter::Client.dafault="
    end

    response = client.deliver(message)
    ActionTexter.inform_observers(message, response)
    response
  end

  # @private
  def to_s
    "#<#{self.class.name}:#{from}:#{to}:#{text}>"
  end
end

#referenceString

Returns a reference that can be used later on to track responses. Implemented for: Nexmo.

Returns:

  • (String)

    a reference that can be used later on to track responses. Implemented for: Nexmo.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_texter/message.rb', line 16

class ActionTexter::Message
  attr_accessor :from, :to, :text, :reference

  def initialize(message = {})
    self.from = message[:from] || raise("A message must contain from")
    self.to = message[:to] || raise("A message must contain to")
    self.text = message[:text] || raise("A message must contain text")
    self.reference = message[:reference]
  end

  def deliver(client = nil)
    message = ActionTexter.inform_interceptors(self)
    return nil if message.blank? # Do not send if one of the interceptors cancelled

    client ||= ActionTexter::Client.default
    if client.nil?
      raise "To deliver a message you need to specify a client by parameter to deliver or by ActionTexter::Client.dafault="
    end

    response = client.deliver(message)
    ActionTexter.inform_observers(message, response)
    response
  end

  # @private
  def to_s
    "#<#{self.class.name}:#{from}:#{to}:#{text}>"
  end
end

#textString

Returns actual message to send.

Returns:

  • (String)

    actual message to send.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_texter/message.rb', line 16

class ActionTexter::Message
  attr_accessor :from, :to, :text, :reference

  def initialize(message = {})
    self.from = message[:from] || raise("A message must contain from")
    self.to = message[:to] || raise("A message must contain to")
    self.text = message[:text] || raise("A message must contain text")
    self.reference = message[:reference]
  end

  def deliver(client = nil)
    message = ActionTexter.inform_interceptors(self)
    return nil if message.blank? # Do not send if one of the interceptors cancelled

    client ||= ActionTexter::Client.default
    if client.nil?
      raise "To deliver a message you need to specify a client by parameter to deliver or by ActionTexter::Client.dafault="
    end

    response = client.deliver(message)
    ActionTexter.inform_observers(message, response)
    response
  end

  # @private
  def to_s
    "#<#{self.class.name}:#{from}:#{to}:#{text}>"
  end
end

#toString

Returns phone number of the author of the message.

Returns:

  • (String)

    phone number of the author of the message.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_texter/message.rb', line 16

class ActionTexter::Message
  attr_accessor :from, :to, :text, :reference

  def initialize(message = {})
    self.from = message[:from] || raise("A message must contain from")
    self.to = message[:to] || raise("A message must contain to")
    self.text = message[:text] || raise("A message must contain text")
    self.reference = message[:reference]
  end

  def deliver(client = nil)
    message = ActionTexter.inform_interceptors(self)
    return nil if message.blank? # Do not send if one of the interceptors cancelled

    client ||= ActionTexter::Client.default
    if client.nil?
      raise "To deliver a message you need to specify a client by parameter to deliver or by ActionTexter::Client.dafault="
    end

    response = client.deliver(message)
    ActionTexter.inform_observers(message, response)
    response
  end

  # @private
  def to_s
    "#<#{self.class.name}:#{from}:#{to}:#{text}>"
  end
end

Instance Method Details

#deliver(client = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/action_texter/message.rb', line 26

def deliver(client = nil)
  message = ActionTexter.inform_interceptors(self)
  return nil if message.blank? # Do not send if one of the interceptors cancelled

  client ||= ActionTexter::Client.default
  if client.nil?
    raise "To deliver a message you need to specify a client by parameter to deliver or by ActionTexter::Client.dafault="
  end

  response = client.deliver(message)
  ActionTexter.inform_observers(message, response)
  response
end

#to_sObject



41
42
43
# File 'lib/action_texter/message.rb', line 41

def to_s
  "#<#{self.class.name}:#{from}:#{to}:#{text}>"
end