module Reredos

Constants

DOMAIN_LABEL_MAX_LENGTH
DOMAIN_MAX_LENGTH
EMAIL_MAX_LENGTH
USERNAME_MAX_LENGTH
VERSION

Public Class Methods

valid_email?(str) click to toggle source
# File lib/reredos.rb, line 11
def valid_email?(str)
  return false if str.length > EMAIL_MAX_LENGTH
  return false if str.count('@') != 1 # @は一度だけ

  username, domain = str.split('@')
  valid_username?(username) && valid_domain?(domain)
end

Private Class Methods

alphanumeric?(char) click to toggle source
# File lib/reredos.rb, line 53
def alphanumeric?(char)
  letter?(char) || digit?(char)
end
digit?(char) click to toggle source
# File lib/reredos.rb, line 49
def digit?(char)
  ('0'..'9').include?(char)
end
hyphen_or_alphanumeric?(char) click to toggle source
# File lib/reredos.rb, line 57
def hyphen_or_alphanumeric?(char)
  char == '-' || alphanumeric?(char)
end
letter?(char) click to toggle source
# File lib/reredos.rb, line 45
def letter?(char)
  ('a'..'z').include?(char) || ('A'..'Z').include?(char)
end
valid_domain?(str) click to toggle source
# File lib/reredos.rb, line 21
def valid_domain?(str)
  return false if str.length > DOMAIN_MAX_LENGTH
  return false unless str.split('.', -1).all? { |l| valid_label?(l) }

  true
end
valid_label?(str) click to toggle source

ラベルはアルファベットで始まり、アルファベットか数字かハイフンが続き、アルファベットか数字で終わる

# File lib/reredos.rb, line 35
def valid_label?(str)
  return false if str.empty?
  return false if str.length > DOMAIN_LABEL_MAX_LENGTH
  return false unless letter?(str[0])
  return false unless alphanumeric?(str[-1])
  return true if str[1...-1].empty?

  str[1...-1].chars.all? { |c| hyphen_or_alphanumeric?(c) }
end
valid_username?(str) click to toggle source
# File lib/reredos.rb, line 28
def valid_username?(str)
  return false if str.length > USERNAME_MAX_LENGTH
  return false if str.split('.', -1).any?(&:empty?)
  /\A[0-9a-zA-Z\.\+\-\_]+\z/ === str # 既定の文字のみで構成されている
end