Class: CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



6
7
8
9
10
# File 'lib/cli.rb', line 6

def call
  puts "Welcome to the longform browsing Ruby Gem CLI!"
  puts ""
  run
end

#display_article_details(index) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cli.rb', line 34

def display_article_details(index)
  puts "----------------------".colorize(:green)
  puts "#{Article.all[index].title}".colorize(:blue)
  puts "  Description:".colorize(:light_blue) + " #{Article.all[index].description}"
  puts "  Author(s):".colorize(:light_blue) + " #{Article.all[index].author}"
  puts "  Source:".colorize(:light_blue) + " #{Article.all[index].source}"
  puts "  Published:".colorize(:light_blue) + " #{Article.all[index].date}"
  puts "  Length:".colorize(:light_blue) + " #{Article.all[index].length}"
  puts ""
  puts "  Link:".colorize(:light_blue) + " #{Article.all[index].url}"
  puts "----------------------".colorize(:green)
  menu
end

#display_articlesObject



25
26
27
28
29
30
31
32
# File 'lib/cli.rb', line 25

def display_articles
  Article.all.each_with_index do |article, index|
  puts "#{index + 1}. #{article.title}\n".colorize(:blue)
  puts "#{article.description}"
  puts "----------------------".colorize(:green)
  end
  menu
end

#make_articles(page_url = "https://longreads.com/picks") ⇒ Object



19
20
21
22
23
# File 'lib/cli.rb', line 19

def make_articles(page_url = "https://longreads.com/picks")
  Article.all.clear
  articles_array = Scraper.scrape_index_page(page_url)
  Article.create_from_collection(articles_array)
end


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cli.rb', line 48

def menu
  puts "Type \"Search\" to select a page of articles, the number of an article to view its details, or \"exit\" to leave the CLI."
  input = gets.chomp
  if input.downcase == "search"
    puts "Please enter the page number you would like to view."
    page_num = gets.chomp.to_i
    make_articles(page_url(page_num))
    display_articles
  elsif (1..10).include?(input.to_i)
    display_article_details(input.to_i - 1)
  elsif input == "exit"
    puts "Thank you for reading today!"
    @active = false
  else
    menu
  end
end

#page_url(page_num) ⇒ Object



66
67
68
# File 'lib/cli.rb', line 66

def page_url(page_num)
  "https://longreads.com/picks" + "/?page=#{page_num}"
end

#runObject



12
13
14
15
16
17
# File 'lib/cli.rb', line 12

def run
  @active = true
  while @active
    menu 
  end
end