Class: ActionTexter::Validator::Message

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

Defined Under Namespace

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Message

Returns a new instance of Message.



13
14
15
16
17
18
19
20
21
22
# File 'lib/action_texter/validator/message.rb', line 13

def initialize(message)
  @message = message

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

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



11
12
13
# File 'lib/action_texter/validator/message.rb', line 11

def message
  @message
end

Instance Method Details

#body_correct_length?Boolean

Returns:

  • (Boolean)


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

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

#body_present?Boolean

Returns:

  • (Boolean)


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

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

#product_token_present?Boolean

Returns:

  • (Boolean)


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

def product_token_present?
  !ActionTexter.config.product_token.nil? && !ActionTexter.config.product_token.empty?
end

#receiver_plausible?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/action_texter/validator/message.rb', line 24

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

#receiver_present?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/action_texter/validator/message.rb', line 28

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

#sender_length?Boolean

Returns:

  • (Boolean)


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

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

#sender_present?Boolean

Returns:

  • (Boolean)


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

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