Class: Cli

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



4
5
6
7
# File 'lib/cli.rb', line 4

def initialize
  @scraper = Scraper.new
  Article.reset_all
end

Instance Attribute Details

#scraperObject

Returns the value of attribute scraper.



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

def scraper
  @scraper
end

Instance Method Details

#ask_for_another_articleObject



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cli.rb', line 105

def ask_for_another_article
  puts "Would you like to read another article? (y/n)".colorize(:blue)
  continue = gets.chomp.downcase

  if continue == 'y'
    Article.reset_all
    call
  elsif continue == 'n'
    puts "Goodbye!".colorize(:blue)
  else
    ask_for_another_article
  end
end

#ask_to_continueObject



96
97
98
99
100
101
102
103
# File 'lib/cli.rb', line 96

def ask_to_continue
  user_choice = nil
  until ['','exit'].include?(user_choice)
    puts '(press enter to continue, or type "exit" to exit)'.colorize(:blue)
    user_choice = gets.chomp
  end
  user_choice
end

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cli.rb', line 9

def call
  puts "Welcome to The Bump CLI!".colorize(:blue)
  family_stage = set_stage - 1
  scraper.get_articles(family_stage)

  puts "Please select an article:".colorize(:blue)
  show_article_titles
  article_choice = set_article - 1

  show_article_header(article_choice)
  show_article_content(article_choice, 0)

  ask_for_another_article
end

#set_articleObject



39
40
41
42
43
44
45
46
# File 'lib/cli.rb', line 39

def set_article
  article_choice = gets.chomp.to_i
  until article_choice.to_i.between?(1,5)
    puts "Please select an article by its number".colorize(:blue)
    article_choice = gets.chomp.to_i
  end
  article_choice
end

#set_stageObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cli.rb', line 24

def set_stage
  stage = ""
  until stage.to_i.between?(1,5)
    puts "What stage of pregnancy is your family in?".colorize(:blue)
    puts "Please select the number from the following options:".colorize(:blue)
    puts "1 - Getting Pregnant"
    puts "2 - First Trimester"
    puts "3 - Second Trimester"
    puts "4 - Third Trimester"
    puts "5 - Parenting"
    stage = gets.chomp.to_i
  end
  stage
end

#show_article_content(choice, paragraph) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cli.rb', line 64

def show_article_content(choice, paragraph)
  article = Article.all[choice]

  #puts the next paragraph of the article. If what is next is a header, it places that AND the next paragraph
  #if the next paragraph is an ul, then it puts the whole list
  #it also cleans up the text as it puts it out
  if ["h1","h2","h3","h4"].include?(article.content[paragraph].name)
    puts article.content[paragraph].text.gsub(/â.*¢/,"-").gsub("â","\'").colorize(:yellow)
    paragraph += 1
    puts article.content[paragraph].text.gsub(/â.*¢/,"-").gsub("â","\'")
  elsif article.content[paragraph].name == "ul"
    article.content[paragraph].children.each do |li|
      if li.name == "li"
        puts "- " + li.text.gsub(/â.*¢/,"-").gsub("â","\'")
      end
    end
  else
    puts article.content[paragraph].text.gsub(/â.*¢/,"-").gsub("â","\'")
  end

  #for continuing, this checks if we are at the end of the article, and offers the next paragraph if it is not
  if paragraph + 1 < article.content.length
    response = ask_to_continue

    if response == ''
      show_article_content(choice, paragraph+1)
    end
  else
    puts "(End of article)"
  end
end

#show_article_header(choice) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/cli.rb', line 54

def show_article_header(choice)
  article = Article.all[choice]

  puts ""
  puts article.title.colorize(:light_yellow).underline
  puts article.subtitle.colorize(:yellow)
  puts "by #{article.author}"
  puts ""
end

#show_article_titlesObject



48
49
50
51
52
# File 'lib/cli.rb', line 48

def show_article_titles
  Article.all.each.with_index(1) do |article, i|
    puts "#{i} - #{article.title}"
  end
end