Class: BbcNewsCliGem::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



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

def initialize
  front_page_scraper = BbcNewsCliGem::Scraper.new(BbcNewsCliGem::RootAddress+"/news")
  @web_page = front_page_scraper.scrape_front_page
end

Instance Attribute Details

#web_pageObject (readonly)

Returns the value of attribute web_page.



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

def web_page
  @web_page
end

Instance Method Details

#callObject



10
11
12
13
# File 'lib/bbc_news_cli_gem/cli.rb', line 10

def call
  welcome
  main_loop
end

#check_for_open(article) ⇒ Object



36
37
38
39
40
# File 'lib/bbc_news_cli_gem/cli.rb', line 36

def check_for_open(article)
  puts "Do you want to open the article in the browser? (y/n)"
  input = gets.chomp
  article.open if input == "y"
end

#get_article(input) ⇒ Object



31
32
33
34
# File 'lib/bbc_news_cli_gem/cli.rb', line 31

def get_article(input)
  article_index = input.split(" ").last.to_i - 1
  web_page.articles[article_index]
end

#helpObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/bbc_news_cli_gem/cli.rb', line 15

def help
  seperator = "------------------------------------------"
  puts seperator
  puts "You can use any of the following commands:"
  puts "'help' - get options"
  puts "'articles' - shows a list of all articles"
  puts "'article <some number>' - opens the article of the number specified"
  puts "'exit' - quits this gem"
  puts seperator
end

#main_loopObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bbc_news_cli_gem/cli.rb', line 42

def main_loop
  puts "What would you like to do?"
  input = gets.chomp
  case input
  when "help"
    help
    main_loop
  when "articles"
    web_page.display_articles
    main_loop
  when "exit"
    puts "Whatevery, I didn't want you to read the news anyway. "
  when /^article/
    article = get_article(input)
    article.display
    check_for_open(article)
    main_loop
  else
    puts "I'm sorry, I don't know what you're saying"
    main_loop
  end
end

#welcomeObject



26
27
28
29
# File 'lib/bbc_news_cli_gem/cli.rb', line 26

def welcome
  puts "Hello! Welcome to the BBC News CLI Gem"
  help
end