X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fcommon%2Fuser.rb;fp=lib%2Fcommon%2Fuser.rb;h=bb239d13400473b30e15408c63149cc2a8b103e8;hp=0000000000000000000000000000000000000000;hb=f819b178c5c1cb8adda0182c610e5c52fad8bea7;hpb=8a3e804a4c5f33bee1d80243be6b139e45f07a48 diff --git a/lib/common/user.rb b/lib/common/user.rb new file mode 100644 index 0000000..bb239d1 --- /dev/null +++ b/lib/common/user.rb @@ -0,0 +1,63 @@ +require 'common/domain' +require 'common/errors' + +class User + # A class representing a syntactically valid user; that is, an email + # address. Once constructed, you can be sure that it's valid. + + @localpart = nil + @domain = nil + + def domain() + return @domain + end + + def domainpart() + return @domain.to_s() + end + + def initialize(username) + # Initialize this user object, but throw an error if either the + # localpart or domainpart are invalid. The one argument is an + # email address string. + if not username.is_a?(String) + msg = 'username must be a String ' + msg += "but a #{username.class.to_s()} was given" + raise InvalidUserError.new(msg) + end + + parts = username.split('@') + + if parts.length() < 2 then + msg = "the username #{username} does not contain an '@' symbol" + raise InvalidUserError.new(msg) + end + + localpart = parts[0] + + if localpart.length() > 64 then + msg = "the local part of #{username} cannot have more than 64 characters" + raise InvalidUserError(msg) + end + + if localpart.empty? then + msg = "the local part of #{username} cannot be empty" + raise InvalidUserError.new(msg) + end + + @localpart = localpart + @domain = Domain.new(parts[1]) + end + + def localpart() + return @localpart + end + + def to_s() + return @localpart + '@' + @domain.to_s() + end + + def ==(other) + return self.to_s() == other.to_s() + end +end