Sha256: 13e338b16140a80d0c32184b42f4d9ae6b04967b2aa9aad9a5fac6d8726679a3

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twexicon-0.1.8 lib/twexicon/validator.rb
twexicon-0.1.7.1 lib/twexicon/validator.rb