Class: SMSApi::Validators::MessageBodyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/client_smsapi/validators/message_body_validator.rb

Constant Summary collapse

DOUBLE_COST_CHARS =
"\"^{}[]~\\|€\n".freeze
BASIC_CHARS =
"\n\f\r !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~ ¡£¤¥§¿ÄÅÆÉÑÖØÜßàäåæçèéìñòöøùüΓΔΘΛΞΠΣΦΨΩ€".freeze
BASIC_LIMIT =
918
EXTRA_LIMIT =
402

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ MessageBodyValidator

Returns a new instance of MessageBodyValidator.



16
17
18
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 16

def initialize(body)
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



14
15
16
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 14

def body
  @body
end

Class Method Details

.valid_body?(body) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 10

def self.valid_body?(body)
  new(body).valid?
end

Instance Method Details

#basic_charset?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 45

def basic_charset?
  @basic_charset ||= body_codepoints.all? { |c| BASIC_CHARS.codepoints.include?(c) }
end

#body_codepointsObject



32
33
34
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 32

def body_codepoints
  @body_codepoints ||= body.codepoints
end

#body_sizeObject



36
37
38
39
40
41
42
43
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 36

def body_size
  if basic_charset?
    double = body_codepoints.count { |codepoint| DOUBLE_COST_CHARS.codepoints.include?(codepoint) }
    body_codepoints.count + double
  else
    body_codepoints.count
  end
end

#proper_length?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 24

def proper_length?
  if basic_charset?
    body_size <= BASIC_LIMIT
  else
    body_size <= EXTRA_LIMIT
  end
end

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/client_smsapi/validators/message_body_validator.rb', line 20

def valid?
  proper_length? && !body.empty?
end