Sha256: ac038f8d3317ac77d6d45ded64538219949352598c2b109fa0f8da43d0db1133

Contents?: true

Size: 780 Bytes

Versions: 3

Compression:

Stored size: 780 Bytes

Contents

module OSU
  # Validates an osu email. Can also strip a name.# from an osu email.
  class Email
    VALID_EMAIL =
      /\A(?<name_n>[a-z]([a-z-]*[a-z])?\.[1-9]\d*)@([a-z]+\.|)osu.edu\z/i

    def self.valid?(email)
      case email
      when VALID_EMAIL then true
      else false
      end
    end

    attr_reader :email

    def initialize(email)
      raise InvalidEmailError, email unless self.class.valid?(email)

      @email = email.downcase
    end

    def name_n
      VALID_EMAIL.match(email)['name_n']
    end

    # Raised when a string is an invalid email
    class InvalidEmailError < ArgumentError
      def initialize(email)
        super(
          "#{email} is not a valid OSU email. It must resemble name\.\#\@osu.edu"
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
osu_person-0.2.2 lib/osu/email.rb
osu_person-0.2.1 lib/osu/email.rb
osu_person-0.2.0 lib/osu/email.rb