Class: Truemail::Validate::Smtp::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/truemail/validate/smtp/request.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

SMTP_PORT =
25
CONNECTION_TIMEOUT_ERROR =
'connection timed out'
RESPONSE_TIMEOUT_ERROR =
'server response timeout'
CONNECTION_DROPPED =
'server dropped connection after response'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, host:, email:, attempts: nil) ⇒ Request

Returns a new instance of Request.


16
17
18
19
20
21
22
# File 'lib/truemail/validate/smtp/request.rb', line 16

def initialize(configuration:, host:, email:, attempts: nil)
  @configuration = Truemail::Validate::Smtp::Request::Configuration.new(configuration)
  @response = Truemail::Validate::Smtp::Response.new
  @host = host
  @email = email
  @attempts = attempts
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration


14
15
16
# File 'lib/truemail/validate/smtp/request.rb', line 14

def configuration
  @configuration
end

#emailObject (readonly)

Returns the value of attribute email


14
15
16
# File 'lib/truemail/validate/smtp/request.rb', line 14

def email
  @email
end

#hostObject (readonly)

Returns the value of attribute host


14
15
16
# File 'lib/truemail/validate/smtp/request.rb', line 14

def host
  @host
end

#responseObject (readonly)

Returns the value of attribute response


14
15
16
# File 'lib/truemail/validate/smtp/request.rb', line 14

def response
  @response
end

Instance Method Details

#check_portObject


24
25
26
27
28
29
30
31
# File 'lib/truemail/validate/smtp/request.rb', line 24

def check_port
  Timeout.timeout(configuration.connection_timeout) do
    return response.port_opened = !TCPSocket.new(host, Truemail::Validate::Smtp::Request::SMTP_PORT).close
  end
rescue => error
  retry if attempts_exist? && error.is_a?(Timeout::Error)
  response.port_opened = false
end

#runObject


33
34
35
36
37
38
39
40
41
# File 'lib/truemail/validate/smtp/request.rb', line 33

def run
  session.start do |smtp_request|
    response.connection = true
    smtp_handshakes(smtp_request, response)
  end
rescue => error
  retry if attempts_exist?
  assign_error(attribute: :connection, message: compose_from(error))
end