]>
gitweb.michael.orlitzky.com - mailshears.git/blob - common/user.rb
1 require 'common/domain'
2 require 'common/errors'
5 # A class representing a syntactically valid user; that is, an email
6 # address. Once constructed, you can be sure that it's valid.
19 def initialize(username
)
20 # Initialize this user object, but throw an error if either the
21 # localpart or domainpart are invalid. The one argument is an
22 # email address string.
23 if not username
.is_a
?(String
)
24 msg
= 'username must be a String '
25 msg +
= "but a #{username.class.to_s()} was given"
26 raise InvalidUserError
.new(msg
)
29 parts
= username
.split('@')
31 if parts
.length() < 2 then
32 msg
= "the username #{username} does not contain an '@' symbol"
33 raise InvalidUserError
.new(msg
)
38 if localpart
.length() > 64 then
39 msg
= "the local part of #{username} cannot have more than 64 characters"
40 raise InvalidUserError(msg
)
43 if localpart
.empty
? then
44 msg
= "the local part of #{username} cannot be empty"
45 raise InvalidUserError
.new(msg
)
48 @localpart = localpart
49 @domain = Domain
.new(parts
[1])
57 return @localpart +
'@' +
@domain.to_s()
61 return self.to_s() == other
.to_s()