Class: TwiteratorCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/twiterator/twiterator-cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTwiteratorCLI

Returns a new instance of TwiteratorCLI.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/twiterator/twiterator-cli.rb', line 5

def initialize
  puts " "
  puts "************ | TWITERATOR #{Twiterator::VERSION} | ************"
  puts " "
  puts "\nHi there! I'm Twiterator, a Ruby cli scraper for"
  puts "the popular social networking site, Twitter."
  puts "You can type a twitter handle (or search for one!), and I will"
  puts "display that user's basic info along with their most"
  puts "recent tweets. From there, you'll be able to dive into"
  puts "each tweet and play around until your thirst for"
  puts "knowledge of the twitterverse is quenched."
  new_user_menu
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



3
4
5
# File 'lib/twiterator/twiterator-cli.rb', line 3

def doc
  @doc
end

#tweetObject

Returns the value of attribute tweet.



3
4
5
# File 'lib/twiterator/twiterator-cli.rb', line 3

def tweet
  @tweet
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/twiterator/twiterator-cli.rb', line 3

def user
  @user
end

Instance Method Details

#display_possibleObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/twiterator/twiterator-cli.rb', line 44

def display_possible
  counter = 0
  puts " "
  puts "Hmm, that's not a twitter handle, but here are some users you might have been looking for:"
  puts "-"
  until (self.doc.css('.js-action-profile-name')[counter] == nil) || (counter == 4) do

    display_name = self.doc.css('.js-action-profile-name')[counter].text.strip
    user_name = self.doc.css('.ProfileCard-screenname')[counter].text.strip
    bio = self.doc.css('.ProfileCard-bio')[counter].text.strip

    puts "For #{display_name.upcase}, type '#{user_name}'"
    puts bio
    puts " "
    counter +=1
  end
end

#display_profileObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/twiterator/twiterator-cli.rb', line 62

def display_profile
  puts " "
  puts "#{self.user.display_name.upcase} - @#{self.user.user_name}"
  puts "#{self.user.following_count}"
  puts "#{self.user.follower_count}"
  puts " "
  if self.user.private?
    puts "Out of respect for this user's privacy, I'm not willing to display their tweets at this time."
    new_user_menu
  else
    self.user.show_five
    give_options
  end
end

#display_tweet(index) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/twiterator/twiterator-cli.rb', line 85

def display_tweet(index)
  tweet_number = index-1
  @tweet = self.user.tweets[tweet_number]
  puts "-------------"
  puts "On #{tweet.date}, at approximately #{tweet.time} #{tweet.user_name} wrote:"
  puts "'#{tweet.content}'"
  puts "-----"
  puts "#{tweet.reply_count} people replied to this tweet."
  puts "#{tweet.retweets} people retweeted."
  puts "#{tweet.likes} people liked it."
  puts "--"
  tweet_options
end

#give_optionsObject



77
78
79
80
81
82
83
# File 'lib/twiterator/twiterator-cli.rb', line 77

def give_options
  puts "-To see more of #{self.user.display_name}'s tweets, type 'more.'"
  puts "-To check out a different user's tweets, type 'new'"
  puts "-To learn more about a tweet, simply type the number of the tweet you'd like to learn about."
  #options_response
  tweet_response
end

#new_user_menuObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/twiterator/twiterator-cli.rb', line 19

def new_user_menu
  puts "\nPlease enter a twitter handle or search for the user you're thinking of."
  puts "(ex. 'kanyewest' or 'President of Mexico')"
  user = gets.chomp
  if not_verified(user)
    search_possible(user)
    display_possible
    new_user_menu
  elsif
    @user = User.new(user)
  end
  display_profile
end

#not_verified(user) ⇒ Object



33
34
35
36
# File 'lib/twiterator/twiterator-cli.rb', line 33

def not_verified(user)
  new_verify = Verification.new(user)
  new_verify.verify == false
end

#search_possible(user) ⇒ Object



38
39
40
41
42
# File 'lib/twiterator/twiterator-cli.rb', line 38

def search_possible(user)
  formatted_user = user.gsub(' ', '%20')
  html = open("https://twitter.com/search?f=users&q=#{formatted_user}&src=typd")
  @doc = Nokogiri::HTML(html)
end

#set_repliesObject



107
108
109
# File 'lib/twiterator/twiterator-cli.rb', line 107

def set_replies
  self.tweet.set_replies
end

#show_repliesObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/twiterator/twiterator-cli.rb', line 143

def show_replies
  self.tweet.replies.each_with_index do |reply, index|
    puts "#{index+1}. #{reply.content}\n"
  end
  puts "---"
  puts "To redisplay #{self.user.display_name}'s tweets, type 'back'"
  puts "OR"
  puts "To search for a new tweeter, type 'new'"
  tweet_response
end

#tweet_optionsObject



99
100
101
102
103
104
105
# File 'lib/twiterator/twiterator-cli.rb', line 99

def tweet_options
  puts "To view some of the replies to this tweet, type 'replies'"
  puts "To see more tweets from #{self.user.display_name}, type 'more'"
  puts "To check out a different user's tweets, type 'new'"
  set_replies
  tweet_response
end

#tweet_responseObject

def options_response

answer = gets.strip.upcase
if answer == "MORE"
  self.user.five_more
  give_options
elsif answer == "NEW"
  new_user_menu
elsif answer.to_i > 0
  display_tweet(answer.to_i)
end

end



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/twiterator/twiterator-cli.rb', line 123

def tweet_response
  answer = gets.strip.upcase
  if answer == "REPLIES" && self.tweet != nil
    show_replies
  elsif answer == "MORE"
    self.user.five_more
    give_options
  elsif answer == "BACK"
    self.user.redisplay
    give_options
  elsif answer == "NEW"
    new_user_menu
  elsif answer.to_i > 0
    display_tweet(answer.to_i)
  else
    puts "Uh oh!! I don't know what you're trying to say! Sorry about that."
    give_options
  end
end