Class: TextBelt::ResponseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/textbelt/validators/response_validator.rb

Overview

Validator for incoming http responses

Author:

  • Dean Silfen

Class Method Summary collapse

Class Method Details

.validate(phone_number, body) ⇒ Boolean

Validate the response from http service

Parameters:

  • phone_number (String)

    number to send the text to

  • body (Hash)

    The body of the server’s http response

Returns:

  • (Boolean)

    true if body is valid

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/textbelt/validators/response_validator.rb', line 23

def validate(phone_number, body)
  return true if body['message'].nil? && body['success']
  message = body['message']
  if message == 'Sorry, texts to this number are disabled.'
    raise TextBelt::Errors::BlackListedNumberError, "#{phone_number} has been blacklisted"
  end

  if message == 'Communication with SMS gateway failed.'
    raise TextBelt::Errors::GatewayFailureError, 'A gateway failure occured between the http service and the carrier'
  end

  if message == 'Could not validate phone# quota.'
    raise TextBelt::Errors::PhoneCouldNotValidateError, "#{phone_number} could not be validated in http service"
  end

  if message == 'Could not validate IP quota.'
    raise TextBelt::Errors::IPCouldNotValidateError, message
  end

  if message.include? 'Exceeded quota for this phone number.'
    raise TextBelt::Errors::PhoneQuotaExceededError, message
  end

  if message.include? 'Exceeded quota for this IP address.'
    raise TextBelt::Errors::IPQuotaExceededError, message
  end

  true
end