class Twexicon::Validator attr_reader :username TWITTER_RESERVED_WORDS = ["settings", "i", "search", "logout", "login", "download", "signup", "tos", "privacy", "account"] def initialize @username = "" end def run until username.length > 0 puts "\nPlease enter a valid Twitter handle:" @username = gets.gsub(/\W/, "") until is_valid? puts "Sorry, that doesn't seem to be a valid Twitter account. Please try again." @username = gets.gsub(/\W/, "") end begin @username = Nokogiri::HTML(open("https://twitter.com/#{username}")).css("span.u-linkComplex-target").first.text rescue OpenURI::HTTPError, NoMethodError puts "Hm, that doesn't seem to be an active Twitter account." @username = "" run rescue SocketError puts "Sorry, the network isn't responding. Please try again." @username = "" run end end "#{username}" end def is_valid? username.match(/^\w{1,15}$/) && !TWITTER_RESERVED_WORDS.include?(username.downcase) end end