Class: BookDeals::Launcher

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

Overview

manages user interaction with the scraper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = CLI.new, scraper = Scraper.new) ⇒ Launcher

Returns a new instance of Launcher.



47
48
49
50
51
52
53
# File 'lib/book_deals/launcher.rb', line 47

def initialize(io = CLI.new, scraper = Scraper.new)
  self.input_output = io
  self.scraper = scraper
  @categories = []

  @selected_category = nil
end

Instance Attribute Details

#input_outputObject

Returns the value of attribute input_output.



7
8
9
# File 'lib/book_deals/launcher.rb', line 7

def input_output
  @input_output
end

#scraperObject

Returns the value of attribute scraper.



7
8
9
# File 'lib/book_deals/launcher.rb', line 7

def scraper
  @scraper
end

Instance Method Details

#display_dealsObject



23
24
25
26
27
28
29
30
31
# File 'lib/book_deals/launcher.rb', line 23

def display_deals
  self.input_output.say "Deals for Category - #{@selected_category.name}".colorize(:green)
  self.input_output.say "--------------------------------------".colorize(:green)
  @selected_category.books.each do |book|
    self.input_output.say book.to_s
  end

  self.display_total_book_deals_in_category
end

#display_menuObject



15
16
17
18
19
20
21
# File 'lib/book_deals/launcher.rb', line 15

def display_menu
  self.input_output.say "Select a Category to see the deals:".colorize(:blue)
  @categories = self.scraper.scrape_categories_from_home_page
  @categories.each.with_index(1) do |category, category_number|
    self.input_output.say "#{category_number}. #{category.name} (Press #{category_number} to see #{category.name})"
  end
end

#display_total_book_deals_in_categoryObject



33
34
35
36
# File 'lib/book_deals/launcher.rb', line 33

def display_total_book_deals_in_category
  self.input_output.say "Total #{@selected_category.books.count} deal/deals found for category #{@selected_category.name}.".colorize(:green)
  self.input_output.say "=============================================================================="
end

#does_user_wants_to_quit?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/book_deals/launcher.rb', line 9

def does_user_wants_to_quit?
  self.input_output.say "Do you want to continue viewing deals? (y/n)".colorize(:blue)
  answer = self.input_output.ask
  %w(n no exit).include?(answer.downcase)
end

#good_byeObject



43
44
45
# File 'lib/book_deals/launcher.rb', line 43

def good_bye
  self.input_output.say "Thank you for visiting BookDeals. Hope to see you soon. Goodbye!".colorize(:green)
end

#greet_userObject



38
39
40
41
# File 'lib/book_deals/launcher.rb', line 38

def greet_user
  self.input_output.say "Welcome to BookDeals!!".colorize(:green)
  self.input_output.say "=======================".colorize(:green)
end

#select_categoryObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/book_deals/launcher.rb', line 65

def select_category
  category_choice = self.input_output.ask
  number_of_categories = @categories.count
  if category_choice.to_i.between?(1, number_of_categories)
    category = @categories[category_choice.to_i - 1]

    @selected_category = self.scraper.scrape_deals_from_category_page(category)
    self.display_deals
  else
   self.wrong_choice_selection(number_of_categories)
  end
end

#startObject



55
56
57
58
59
60
61
62
63
# File 'lib/book_deals/launcher.rb', line 55

def start
  self.greet_user
  loop do
    self.display_menu
    self.select_category
    break if self.does_user_wants_to_quit?
  end
  self.good_bye
end

#wrong_choice_selection(number_of_categories) ⇒ Object



78
79
80
81
82
83
# File 'lib/book_deals/launcher.rb', line 78

def wrong_choice_selection(number_of_categories)
  self.input_output.say "The category selected is not present!".colorize(:red)
  self.input_output.say "Please select from options 1 to #{number_of_categories}:".colorize(:red)
  self.display_menu
  self.select_category
end