class TweetValidator::TweetLengthValidator
Constants
- TWEET_MAX_LENGTH
Public Class Methods
sanitize(tweet)
click to toggle source
# File lib/tweet_validator/tweet_length_validator.rb, line 20 def self.sanitize(tweet) tweet.gsub(/%<.+?>/, "").gsub(/%{.+?}/, "") end
shorten_url_length(tweet)
click to toggle source
# File lib/tweet_validator/tweet_length_validator.rb, line 24 def self.shorten_url_length(tweet) shorten_tweet = tweet.gsub(URI.regexp("http"), dummy_http_url).gsub(URI.regexp("https"), dummy_https_url) shorten_tweet.length end
valid_tweet?(tweet)
click to toggle source
# File lib/tweet_validator/tweet_length_validator.rb, line 12 def self.valid_tweet?(tweet) return false unless tweet return false if tweet.empty? return false unless shorten_url_length(sanitize(tweet)) <= TWEET_MAX_LENGTH true end
Private Class Methods
dummy_http_url()
click to toggle source
# File lib/tweet_validator/tweet_length_validator.rb, line 31 def self.dummy_http_url dummy_url("http://", TweetValidator.config.short_url_length) end
dummy_https_url()
click to toggle source
# File lib/tweet_validator/tweet_length_validator.rb, line 35 def self.dummy_https_url dummy_url("https://", TweetValidator.config.short_url_length_https) end
dummy_url(prefix, max_length)
click to toggle source
# File lib/tweet_validator/tweet_length_validator.rb, line 39 def self.dummy_url(prefix, max_length) prefix + "x" * (max_length - prefix.length) end
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
# File lib/tweet_validator/tweet_length_validator.rb, line 8 def validate_each(record, attribute, value) record.errors.add(attribute, :invalid_tweet) unless TweetLengthValidator.valid_tweet?(value) end