Class: CmSms::Message

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

Defined Under Namespace

Classes: BodyMissing, BodyToLong, DCSNotNumeric, FromMissing, ToMissing, ToUnplausible

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Message

Returns a new instance of Message.



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

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.



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

def body
  @body
end

#dcsObject

Returns the value of attribute dcs.



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

def dcs
  @dcs
end

#fromObject

Returns the value of attribute from.



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

def from
  @from
end

#referenceObject

Returns the value of attribute reference.



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

def reference
  @reference
end

#toObject

Returns the value of attribute to.



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

def to
  @to
end

Instance Method Details

#body_correct_length?Boolean

Returns:

  • (Boolean)


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

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

#body_present?Boolean

Returns:

  • (Boolean)


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

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

#dcs_numeric?Boolean

Returns:

  • (Boolean)


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

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

#deliverObject



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

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:



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

def deliver!
  raise FromMissing.new('The value for the from attribute is missing.') unless sender_present?
  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 BodyToLong.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)


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

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

#receiver_plausible?Boolean

Returns:

  • (Boolean)


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

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

#receiver_present?Boolean

Returns:

  • (Boolean)


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

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

#requestObject



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

def request
  Request.new(to_xml)
end

#sender_length?Boolean

Returns:

  • (Boolean)


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

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

#sender_present?Boolean

Returns:

  • (Boolean)


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

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

#to_xmlObject



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

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