]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/common/user.rb
Overhaul everything to get consistent error reports.
[mailshears.git] / lib / common / user.rb
diff --git a/lib/common/user.rb b/lib/common/user.rb
new file mode 100644 (file)
index 0000000..bb239d1
--- /dev/null
@@ -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