Sha256: 926174000f779c9772cbf9c8716193ee09a22a9fdd2bfc98548d93e2c1501fdc
Contents?: true
Size: 1.13 KB
Versions: 9
Compression:
Stored size: 1.13 KB
Contents
module ActiveModel module Validations class TwitterValidator < EachValidator # Basic username regexp TWITTER_USERNAME_REGEXP = /([A-Za-z0-9_]{1,15})/i # Regexp used to detect twitter username within the URL. TWITTER_URL_REGEXP = %r{\Ahttps?://(?:www\.)?twitter.com/#{TWITTER_USERNAME_REGEXP}\z}i # Regexp to test using twitter username as @sign. TWITTER_ATSIGN_REGEXP = /\A@#{TWITTER_USERNAME_REGEXP}\z/i # Regexp to test against usernames without the @sign TWITTER_NOATSIGN_REGEXP = /\A#{TWITTER_USERNAME_REGEXP}\z/i def validate_each(record, attribute, value) format = options[:format].to_sym if options[:format] if value.nil? record.errors.add(attribute, :blank) elsif format == :url match = value.match(TWITTER_URL_REGEXP) record.errors.add(attribute) unless match && !match[1].nil? elsif format == :username_with_at record.errors.add(attribute) unless value =~ TWITTER_ATSIGN_REGEXP else record.errors.add(attribute) unless value =~ TWITTER_NOATSIGN_REGEXP end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems