Class: Porteo::Sms_protocol

Inherits:
Protocol show all
Defined in:
lib/protocols/sms_protocol.rb

Overview

Implementation of SMS protocol to be used in Porteo system. It only define specific behavior for this protocol.

Instance Attribute Summary

Attributes inherited from Protocol

#gw_config, #receiver

Instance Method Summary collapse

Methods inherited from Protocol

#initialize, #message, #send_message, #set_template, #set_template_params

Constructor Details

This class inherits a constructor from Porteo::Protocol

Instance Method Details

#check_message_sectionsnil

Check for the required fields to exists.

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if message cannot be sent.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/protocols/sms_protocol.rb', line 36

def check_message_sections
  raise ArgumentError, "Protocol Error. There are not text section" if @message_sections[:text] == nil
  # The sms must match GSM alphabet, double counting these chars
  # [ , ], <FF>, ^, \, {, }, ~, | and €
  count = 0

  @message_sections[:text].each_char do | c |
    count = count + 1 if c =~ /[\[\]\^\\\{\}\~\|€]/
   
    count = count + 1
  end

  raise ArgumentError, "Protocol Error. The message is too long" if count > 160

  raise ArgumentError, "Protocol Error. The phone number is invalid" unless @message_sections[:phone] =~ /^\d{9}$/
  
  raise ArgumentError, "Protocol Error. The country phone code is invalid" unless @message_sections[:code] =~ /^\d{2,4}$/

  raise ArgumentError, "Protocol Error. The sender is invalid" unless @message_sections[:sender] =~ /^[A-Za-z0-9]{1,11}$/

end

#override_tagsnil

Implementates the parent method to ensure that sms receptor is the one set at receiver instance variable not the :phone template tag

Returns:

  • (nil)


61
62
63
64
65
# File 'lib/protocols/sms_protocol.rb', line 61

def override_tags
  # In sms protocol, the receiver instance variable has precedence over
  # :phone tag in template
  @message_sections[:phone] = @receiver unless @receiver == nil
end