Module: Ovh::Http2sms::Validators
- Defined in:
- lib/ovh/http2sms/validators.rb
Overview
Validators for SMS parameters
Provides validation for all SMS parameters before sending to ensure they meet OVH API requirements.
Constant Summary collapse
- MAX_TAG_LENGTH =
Maximum tag length allowed by OVH API
20- VALID_SMS_CLASSES =
Valid SMS class values
(0..3).to_a.freeze
- VALID_SMS_CODINGS =
Valid smsCoding values
[1, 2].freeze
- DEFERRED_FORMAT_PATTERN =
Deferred date format: hhmmddMMYYYY
/\A\d{12}\z/
Class Method Summary collapse
- .log_length_warning(info) ⇒ Object
- .log_unicode_warning(info) ⇒ Object
- .raise_length_error(info) ⇒ Object
-
.validate!(params) ⇒ void
Validate all parameters for an SMS delivery.
-
.validate_deferred!(deferred) ⇒ Object
Validate deferred date format.
-
.validate_message!(message, no_stop: false) ⇒ Object
Validate message content and length.
- .validate_message_length!(info) ⇒ Object
-
.validate_phone_numbers!(phones) ⇒ Object
Validate phone number(s).
-
.validate_required_params!(params) ⇒ Object
Validate presence of required parameters.
-
.validate_sender_for_response!(params) ⇒ void
Validate sender_for_response compatibility.
-
.validate_sms_class!(sms_class) ⇒ Object
Validate SMS class.
-
.validate_sms_coding!(sms_coding) ⇒ Object
Validate SMS coding.
-
.validate_tag!(tag) ⇒ Object
Validate tag length.
Class Method Details
.log_length_warning(info) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/ovh/http2sms/validators.rb', line 115 def log_length_warning(info) logger = Ovh::Http2sms.configuration.logger return unless logger logger.warn( "[OVH HTTP2SMS] Message will be sent as #{info[:sms_count]} SMS segments " \ "(#{info[:characters]} characters, #{info[:encoding]} encoding). This may incur additional charges." ) end |
.log_unicode_warning(info) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ovh/http2sms/validators.rb', line 84 def log_unicode_warning(info) logger = Ovh::Http2sms.configuration.logger return unless info[:encoding] == :unicode && logger non_gsm = info[:non_gsm_chars].join(", ") logger.warn( "[OVH HTTP2SMS] Message requires Unicode encoding due to characters: #{non_gsm}. " \ "This reduces maximum SMS length from 160 to 70 characters." ) end |
.raise_length_error(info) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/ovh/http2sms/validators.rb', line 105 def raise_length_error(info) raise MessageLengthError.new( "Message is very long and will be sent as #{info[:sms_count]} SMS segments. " \ "Current length: #{info[:characters]} characters (#{info[:encoding]} encoding).", encoding: info[:encoding], length: info[:characters], max_length: info[:max_single_sms] ) end |
.validate!(params) ⇒ void
This method returns an undefined value.
Validate all parameters for an SMS delivery
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ovh/http2sms/validators.rb', line 37 def validate!(params) validate_required_params!(params) validate_phone_numbers!(params[:to]) (params[:message], no_stop: params[:no_stop]) validate_tag!(params[:tag]) if params[:tag] validate_deferred!(params[:deferred]) if params[:deferred] validate_sms_class!(params[:sms_class]) if params[:sms_class] validate_sms_coding!(params[:sms_coding]) if params[:sms_coding] validate_sender_for_response!(params) if params[:sender_for_response] end |
.validate_deferred!(deferred) ⇒ Object
Validate deferred date format
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/ovh/http2sms/validators.rb', line 140 def validate_deferred!(deferred) return if deferred.is_a?(Time) || deferred.is_a?(DateTime) deferred_str = deferred.to_s return if deferred_str.match?(DEFERRED_FORMAT_PATTERN) raise ValidationError, "Invalid deferred format: '#{deferred}'. " \ "Expected hhmmddMMYYYY format (e.g., 125025112024 for 25/11/2024 at 12:50) " \ "or a Ruby Time/DateTime object." end |
.validate_message!(message, no_stop: false) ⇒ Object
Validate message content and length
78 79 80 81 82 |
# File 'lib/ovh/http2sms/validators.rb', line 78 def (, no_stop: false) info = GsmEncoding.(, commercial: !no_stop) log_unicode_warning(info) (info) end |
.validate_message_length!(info) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/ovh/http2sms/validators.rb', line 95 def (info) return unless info[:sms_count] > 10 if Ovh::Http2sms.configuration.raise_on_length_error raise_length_error(info) else log_length_warning(info) end end |
.validate_phone_numbers!(phones) ⇒ Object
Validate phone number(s)
66 67 68 69 70 71 |
# File 'lib/ovh/http2sms/validators.rb', line 66 def validate_phone_numbers!(phones) phone_list = phones.is_a?(Array) ? phones : [phones] phone_list.each do |phone| PhoneNumber.validate!(PhoneNumber.format(phone)) end end |
.validate_required_params!(params) ⇒ Object
Validate presence of required parameters
52 53 54 55 56 57 58 59 60 |
# File 'lib/ovh/http2sms/validators.rb', line 52 def validate_required_params!(params) missing = [] missing << "to" if params[:to].nil? || params[:to].to_s.empty? missing << "message" if params[:message].nil? || params[:message].to_s.empty? return if missing.empty? raise ValidationError, "Missing required parameters: #{missing.join(", ")}" end |
.validate_sender_for_response!(params) ⇒ void
This method returns an undefined value.
Validate sender_for_response compatibility
178 179 180 181 182 183 184 185 186 |
# File 'lib/ovh/http2sms/validators.rb', line 178 def validate_sender_for_response!(params) return unless params[:sender_for_response] && params[:sender] # Log warning but don't raise error Ovh::Http2sms.configuration.logger&.warn( "[OVH HTTP2SMS] senderForResponse is enabled but a sender is also specified. " \ "The 'from' parameter will be ignored. Leave sender empty when using senderForResponse." ) end |
.validate_sms_class!(sms_class) ⇒ Object
Validate SMS class
156 157 158 159 160 161 |
# File 'lib/ovh/http2sms/validators.rb', line 156 def validate_sms_class!(sms_class) return if VALID_SMS_CLASSES.include?(sms_class.to_i) raise ValidationError, "Invalid SMS class: #{sms_class}. Must be 0, 1, 2, or 3." end |
.validate_sms_coding!(sms_coding) ⇒ Object
Validate SMS coding
167 168 169 170 171 172 |
# File 'lib/ovh/http2sms/validators.rb', line 167 def validate_sms_coding!(sms_coding) return if VALID_SMS_CODINGS.include?(sms_coding.to_i) raise ValidationError, "Invalid SMS coding: #{sms_coding}. Must be 1 (7-bit) or 2 (Unicode)." end |
.validate_tag!(tag) ⇒ Object
Validate tag length
129 130 131 132 133 134 |
# File 'lib/ovh/http2sms/validators.rb', line 129 def validate_tag!(tag) return if tag.to_s.length <= MAX_TAG_LENGTH raise ValidationError, "Tag exceeds maximum length of #{MAX_TAG_LENGTH} characters (got #{tag.length})" end |