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
# 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)
end

#display_articles(from_number = 1) ⇒ Object



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

def display_articles(from_number = 1)
  Article.all[from_number-1, 10].each.with_index(from_number) do |article, index| 
  puts "#{index}. #{article.title}\n".colorize(:blue)
  puts "#{article.description}"
  puts "----------------------".colorize(:green)
  end
  menu
end

#display_instructions(page_num) ⇒ Object



85
86
87
88
# File 'lib/cli.rb', line 85

def display_instructions(page_num)
  puts "You are on page #{page_num}. Type 'next' or 'back' to navigate between pages. Type the number of an article to view its details."
  puts ""
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")
  articles_array = Scraper.scrape_index_page(page_url)
  Article.create_from_collection(articles_array)
  articles_array.uniq!
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cli.rb', line 47

def menu
  display_instructions(@page_num)
  input = gets.chomp
  if input.downcase == "next" 
    @page_num += 1
    make_articles(page_url(@page_num))
    display_articles((@page_num*10)-9)
    display_instructions(@page_num)
  elsif input.downcase == "back" && @page_num != 1
    @page_num -= 1
    display_articles((@page_num*10)-9)
    display_instructions(@page_num)
  elsif input.downcase == "back" && @page_num == 1
    puts "Sorry! You're already on the first page of articles.".colorize(:red)
    puts ""
    menu
  elsif (1..1000).include?(input.to_i)
    display_article_details(input.to_i - 1)
    puts "Would you like to view another article? Enter Y or N."
    input = gets.chomp.downcase
    if input == "y"
      run
    else
      quit
    end
  elsif input == "exit"
    quit
  else
    menu
  end
end

#page_url(page_num) ⇒ Object



90
91
92
# File 'lib/cli.rb', line 90

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

#quitObject



79
80
81
82
83
# File 'lib/cli.rb', line 79

def quit
  puts ""
  puts "Thank you for reading today!"
  exit
end

#runObject



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

def run
  @page_num = 1
  make_articles
  display_articles
  menu
end