Class: Twexicon::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/twexicon/validator.rb

Constant Summary collapse

TWITTER_RESERVED_WORDS =
["settings", "i", "search", "logout", "login", "download", "signup", "tos", "privacy", "account"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator

Returns a new instance of Validator.



6
7
8
# File 'lib/twexicon/validator.rb', line 6

def initialize
  @username = ""
end

Instance Attribute Details

#usernameObject (readonly)

Returns the value of attribute username.



2
3
4
# File 'lib/twexicon/validator.rb', line 2

def username
  @username
end

Instance Method Details

#is_valid?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/twexicon/validator.rb', line 37

def is_valid?
  username.match(/^\w{1,15}$/) && !TWITTER_RESERVED_WORDS.include?(username.downcase)
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/twexicon/validator.rb', line 10

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
    rescue
      puts "Something went wrong. Please try again."
      @username = ""
      run
    end
  end
  "#{username}"
end