Class: CmSms::Message

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

Defined Under Namespace

Classes: BodyMissing, BodyTooLong, DCSNotNumeric, FromMissing, FromTooLong, ToMissing, ToUnplausible

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Message

Returns a new instance of Message.



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

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.



15
16
17
# File 'lib/cm_sms/message.rb', line 15

def body
  @body
end

#dcsObject

Returns the value of attribute dcs.



15
16
17
# File 'lib/cm_sms/message.rb', line 15

def dcs
  @dcs
end

#fromObject

Returns the value of attribute from.



15
16
17
# File 'lib/cm_sms/message.rb', line 15

def from
  @from
end

#referenceObject

Returns the value of attribute reference.



15
16
17
# File 'lib/cm_sms/message.rb', line 15

def reference
  @reference
end

#toObject

Returns the value of attribute to.



15
16
17
# File 'lib/cm_sms/message.rb', line 15

def to
  @to
end

Instance Method Details

#body_correct_length?Boolean

Returns:

  • (Boolean)


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

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

#body_present?Boolean

Returns:

  • (Boolean)


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

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

#dcs_numeric?Boolean

Returns:

  • (Boolean)


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

def dcs_numeric?
  true if dcs.nil? || Float(dcs) 
rescue 
  false
end

#deliverObject



65
66
67
68
69
# File 'lib/cm_sms/message.rb', line 65

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:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cm_sms/message.rb', line 71

def deliver!
  raise FromMissing.new('The value for the from attribute is missing.') unless sender_present?
  raise FromTooLong.new('The value for the sender attribute must contain 1..11 characters.') unless sender_length?
  raise ToMissing.new('The value for the to attribute is missing.') unless receiver_present?
  raise BodyMissing.new('The body of the message is missing.') unless body_present?
  raise BodyTooLong.new('The body of the message has a length greater than 160.') unless body_correct_length?
  raise ToUnplausible.new("The given value for the to attribute is not a plausible phone number.\nMaybe the country code is missing.") unless receiver_plausible?
  raise DCSNotNumeric.new("The given value for the dcs attribute is not a number.") unless dcs_numeric?
  
  deliver
end

#product_token_present?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/cm_sms/message.rb', line 57

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

#receiver_plausible?Boolean

Returns:

  • (Boolean)


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

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

#receiver_present?Boolean

Returns:

  • (Boolean)


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

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

#requestObject



61
62
63
# File 'lib/cm_sms/message.rb', line 61

def request
  Request.new(to_xml)
end

#sender_length?Boolean

Returns:

  • (Boolean)


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

def sender_length?
  sender_present? && from.length <= 11
end

#sender_present?Boolean

Returns:

  • (Boolean)


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

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

#to_xmlObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cm_sms/message.rb', line 83

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