Class: TopHeadlines::CLI

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

Instance Method Summary collapse

Instance Method Details

#all_headlines_open_in_browserObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/top-headlines/cli.rb', line 90

def all_headlines_open_in_browser
  puts "Select source and headline number to open the full article in the browser. \n(e.g. 'cnn 3') \n\nAlternatively, #{request_input_full_menu.downcase}"
  print "\nYOUR SELECTION: "
  @num = gets.strip.upcase

  while @num[-1].to_i.between?(1,5) && !num_included_in_menu_if_statement
    source = @num[0...-1].strip.upcase
    headline = @num.strip[-1].to_i

    open_url(source, headline)

    sleep(1)
    puts "\nSelect another source and headline number to open full article in the browser. \n\nAlternatively, #{request_input_full_menu.downcase}"
    print "\nYOUR SELECTION: "
    @num = gets.strip.upcase
  end

  num_included_in_menu_if_statement ? @input = @num : invalid_entry
  menu_if_statement
end

#callObject



3
4
5
6
7
8
# File 'lib/top-headlines/cli.rb', line 3

def call
  welcome_banner
  news_sources
  menu
  goodbye
end

#goodbyeObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/top-headlines/cli.rb', line 153

def goodbye
  system "clear"
  puts "\nThanks for visiting – see you next time!"

  puts <<-DOC
            

        ```-.                                   
      `.-:::-           ````````                
    `.-:::::---.    `..----------..`            
     -:::::::-`   `.-----------------.          
     `-::::::`  `.---------------------`        
      `-:::--  `------/o:------:o/------`       
         ..    -------+o/------:o+-------`      
         `..` `--------------------------.      
           `..---------------------------.      
              `--------------------------.      
               .-----:yo:------:oy:------       
               `.------+yyooooyyo:------`       
                 .--------::/:--------.         
                  `.----------------.`          
                     ``..------...`                                                                    


Image: VectorStock (https://www.vectorstock.com/royalty-free-vector/bye-goodbye-vector-6122218)
ASCII Image Conversion: Patrik Roos (http://www.text-image.com/convert/ascii.html)

TopHeadlines Gem made by Zach Newburgh (http://www.zachnewburgh.com/)
Copyright © 2016 Zach Newburgh
  
  DOC

end

#headlines_from_source_open_in_browserObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/top-headlines/cli.rb', line 111

def headlines_from_source_open_in_browser
  puts "Select headline number to open the full article in the browser. \n\nAlternatively, #{request_input_full_menu.downcase}"
  print "\nYOUR SELECTION: "
  @num = gets.strip.upcase

  while @num.to_i.between?(1,5) && !num_included_in_menu_if_statement
    open_url(@input, @num)
    
    sleep(1)
    puts "\nSelect another headline number to open full article in the browser. \n\nAlternatively, #{request_input_full_menu.downcase}"
    print "\nYOUR SELECTION: "
    @num = gets.strip.upcase
    
    @input = @num if num_included_in_menu_if_statement
  end
    
  num_included_in_menu_if_statement ? @input = @num : invalid_entry
  menu_if_statement
end

#invalid_entryObject



145
146
147
148
149
150
151
# File 'lib/top-headlines/cli.rb', line 145

def invalid_entry
  puts "\n=> INVALID SELECTION: #{request_input_full_menu}"
  sleep(1)
  news_sources_banner
  news_sources
  print "YOUR SELECTION: "
end

#list_all_headlinesObject



64
65
66
# File 'lib/top-headlines/cli.rb', line 64

def list_all_headlines
  TopHeadlines::Source.list_all_headlines
end

#list_all_headlines_bannerObject



56
57
58
59
60
61
62
# File 'lib/top-headlines/cli.rb', line 56

def list_all_headlines_banner
  system "clear"
  puts " -------------------------------"
  puts "| TOP HEADLINES & BREAKING NEWS |"
  puts " -------------------------------"
  time
end

#list_headlines_from_sourceObject



82
83
84
85
86
87
88
# File 'lib/top-headlines/cli.rb', line 82

def list_headlines_from_source
  system "clear"
  puts "\n*** #{@input} ***"
  time
  TopHeadlines::Source.scrape_headlines(@input)[0,5].each_with_index {|headline, index| puts "#{index + 1}. #{headline}"}
  puts "\n"
end


10
11
12
13
14
15
16
17
# File 'lib/top-headlines/cli.rb', line 10

def menu
  puts "Which source do you want to view? Alternatively, type 'all' to view all headlines or 'exit' to exit!"
  print "YOUR SELECTION: "
  while @input != "EXIT" && @num != "EXIT"
    @input = gets.strip.upcase 
    menu_if_statement
  end
end


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/top-headlines/cli.rb', line 19

def menu_if_statement
  if @input == "ALL"
    list_all_headlines_banner
    list_all_headlines
    all_headlines_open_in_browser
  elsif @input == "SOURCES"
    news_sources_banner
    news_sources
    puts request_input_full_menu
    print "YOUR SELECTION: "
  elsif source_includes?(@input)
    list_headlines_from_source
    headlines_from_source_open_in_browser
  elsif @input != "EXIT"
    invalid_entry
  else
  end
end

#news_sourcesObject



51
52
53
54
# File 'lib/top-headlines/cli.rb', line 51

def news_sources
  TopHeadlines::Source.all.keys.sort.each {|source| puts "*** #{source} ***"}
  puts "\n"
end

#news_sources_bannerObject



76
77
78
79
80
# File 'lib/top-headlines/cli.rb', line 76

def news_sources_banner
  puts "\n"
  puts "NEWS SOURCES"
  puts "------------"
end

#num_included_in_menu_if_statementObject



141
142
143
# File 'lib/top-headlines/cli.rb', line 141

def num_included_in_menu_if_statement
  @num == "EXIT" || @num == "ALL" || @num == "SOURCES" || source_includes?(@num)
end

#open_url(source, number) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/top-headlines/cli.rb', line 131

def open_url(source, number)
  headline = TopHeadlines::Source.scrape_headlines(source)[number.to_i-1]
  puts "\n=> You selected the #{number.to_i.ordinalize} headline: '#{headline}'."
  puts "=> Opening..."

  sleep(2)
  url = TopHeadlines::Source.scrape_urls(source)[number.to_i-1]
  url != nil ? system("open", url) : invalid_entry
end

#request_input_full_menuObject



68
69
70
# File 'lib/top-headlines/cli.rb', line 68

def request_input_full_menu
  "Select a source, type 'sources' to view sources, type 'all' to view all headlines, or type 'exit' to exit."
end

#source_includes?(input) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/top-headlines/cli.rb', line 72

def source_includes?(input)
  TopHeadlines::Source.all.keys.include?(input)
end

#timeObject



46
47
48
49
# File 'lib/top-headlines/cli.rb', line 46

def time
  puts "Reporting live as of #{Time.now.strftime("%l:%M %p %Z on %a, %b #{Time.now.strftime("%e").to_i.ordinalize}, %Y")}:"
  puts "\n"
end

#welcome_bannerObject



38
39
40
41
42
43
44
# File 'lib/top-headlines/cli.rb', line 38

def welcome_banner
  system "clear"
  puts " --------------------------- "
  puts "| WELCOME TO TOP HEADLINES! |"
  puts " --------------------------- "
  time
end