Class: CmSms::Message

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

Defined Under Namespace

Classes: BodyMissing, BodyToLong, FromMissing, ToMissing, ToUnplausible

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Message

Returns a new instance of Message.



15
16
17
18
19
20
21
22
23
# File 'lib/cm_sms/message.rb', line 15

def initialize(attributes = {})
  @from          = attributes[:from]
  @to            = attributes[:to]
  @dcs           = attributes[:dcs]
  @body          = attributes[:body]
  @reference     = attributes[:reference]
  
  @product_token = CmSms.config.product_token
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



13
14
15
# File 'lib/cm_sms/message.rb', line 13

def body
  @body
end

#dcsObject

Returns the value of attribute dcs.



13
14
15
# File 'lib/cm_sms/message.rb', line 13

def dcs
  @dcs
end

#fromObject

Returns the value of attribute from.



13
14
15
# File 'lib/cm_sms/message.rb', line 13

def from
  @from
end

#referenceObject

Returns the value of attribute reference.



13
14
15
# File 'lib/cm_sms/message.rb', line 13

def reference
  @reference
end

#toObject

Returns the value of attribute to.



13
14
15
# File 'lib/cm_sms/message.rb', line 13

def to
  @to
end

Instance Method Details

#body_correct_length?Boolean

Returns:

  • (Boolean)


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

def body_correct_length?
  body_present? && body.length <= 160
end

#body_present?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/cm_sms/message.rb', line 37

def body_present?
  !body.nil? && !body.empty?
end

#deliverObject



53
54
55
56
57
# File 'lib/cm_sms/message.rb', line 53

def deliver
  raise CmSms::Configuration::ProductTokenMissing.new("Please provide an valid product key.\nAfter signup at https://www.cmtelecom.de/, you will find one in your settings.") unless product_token_present?
  
  request.perform
end

#deliver!Object

Raises:



59
60
61
62
63
64
65
66
67
# File 'lib/cm_sms/message.rb', line 59

def deliver!
  raise FromMissing.new('The from attribute is missing.') unless sender_present?
  raise ToMissing.new('The to attribute is missing.') unless receiver_present?
  raise BodyMissing.new('The body of the message is missing.') unless body_present?
  raise BodyToLong.new('The body of the message has a length greater than 160.') unless body_correct_length?
  raise ToUnplausible.new("THe given to attribute is not a plausible phone number.\nMaybe the country code is missing.") unless receiver_plausible?
  
  deliver
end

#product_token_present?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/cm_sms/message.rb', line 45

def product_token_present?
  !@product_token.nil? && !@product_token.empty?
end

#receiver_plausible?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cm_sms/message.rb', line 25

def receiver_plausible?      
  receiver_present? && Phony.plausible?(to)
end

#receiver_present?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/cm_sms/message.rb', line 29

def receiver_present?
  !to.nil? && !to.empty?
end

#requestObject



49
50
51
# File 'lib/cm_sms/message.rb', line 49

def request
  Request.new(to_xml)
end

#sender_present?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/cm_sms/message.rb', line 33

def sender_present?
  !from.nil? && !from.empty?
end

#to_xmlObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cm_sms/message.rb', line 69

def to_xml
  builder = Builder::XmlMarkup.new
  builder.instruct! :xml, version: '1.0'

  xml = builder.MESSAGES do |m|
    m.AUTHENTICATION do |authentication|
      authentication.PRODUCTTOKEN @product_token
    end
    m.MSG do |msg|
      msg.FROM from
      msg.TO to
      msg.DCS dcs if dcs
      msg.BODY body
      msg.REFERENCE reference if reference
    end
  end
end