Class: Net::SMTP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/net/smtp.rb

Overview

This class represents a response received by the SMTP server. Instances of this class are created by the SMTP class; they should not be directly created by the user. For more information on SMTP responses, view Section 4.2 of RFC 5321

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, string) ⇒ Response

Creates a new instance of the Response class and sets the status and string attributes



1038
1039
1040
1041
# File 'lib/net/smtp.rb', line 1038

def initialize(status, string)
  @status = status
  @string = string
end

Instance Attribute Details

#statusObject (readonly)

The three digit reply code of the SMTP response



1044
1045
1046
# File 'lib/net/smtp.rb', line 1044

def status
  @status
end

#stringObject (readonly)

The human readable reply text of the SMTP response



1047
1048
1049
# File 'lib/net/smtp.rb', line 1047

def string
  @string
end

Class Method Details

.parse(str) ⇒ Object

Parses the received response and separates the reply code and the human readable reply text



1032
1033
1034
# File 'lib/net/smtp.rb', line 1032

def self.parse(str)
  new(str[0,3], str)
end

Instance Method Details

#capabilitiesObject

Returns a hash of the human readable reply text in the response if it is multiple lines. It does not return the first line. The key of the hash is the first word the value of the hash is an array with each word thereafter being a value in the array



1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/net/smtp.rb', line 1081

def capabilities
  return {} unless @string[3, 1] == '-'
  h = {}
  @string.lines.drop(1).each do |line|
    k, *v = line[4..-1].split(' ')
    h[k] = v
  end
  h
end

#continue?Boolean

Determines whether the response received was a Positive Intermediate reply (3xx reply code)

Returns:

  • (Boolean)


1062
1063
1064
# File 'lib/net/smtp.rb', line 1062

def continue?
  status_type_char() == '3'
end

#cram_md5_challengeObject

Creates a CRAM-MD5 challenge. You can view more information on CRAM-MD5 on Wikipedia: en.wikipedia.org/wiki/CRAM-MD5



1073
1074
1075
# File 'lib/net/smtp.rb', line 1073

def cram_md5_challenge
  @string.split(/ /)[1].unpack1('m')
end

#exception_classObject

Determines whether there was an error and raises the appropriate error based on the reply code of the response



1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'lib/net/smtp.rb', line 1093

def exception_class
  case @status
  when /\A4/  then SMTPServerBusy
  when /\A50/ then SMTPSyntaxError
  when /\A53/ then SMTPAuthenticationError
  when /\A5/  then SMTPFatalError
  else             SMTPUnknownError
  end
end

#messageObject

The first line of the human readable reply text



1067
1068
1069
# File 'lib/net/smtp.rb', line 1067

def message
  @string.lines.first
end

#status_type_charObject

Takes the first digit of the reply code to determine the status type



1050
1051
1052
# File 'lib/net/smtp.rb', line 1050

def status_type_char
  @status[0, 1]
end

#success?Boolean

Determines whether the response received was a Positive Completion reply (2xx reply code)

Returns:

  • (Boolean)


1056
1057
1058
# File 'lib/net/smtp.rb', line 1056

def success?
  status_type_char() == '2'
end