Class: GoodreadsBooks::CLI

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

Constant Summary collapse

BASE_YEAR =

This application only works for year 2010 to latest awards year (usually, current year - 1). Goodreads Choice Awards Winner 2009 page setup differs from 2010 onwards.

2010

Instance Method Summary collapse

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/goodreads_books/cli.rb', line 7

def call
  system "clear"
  puts ""
  puts "---------- Welcome to Goodreads Choice Awards ----------"
  puts "           ----------------------------------"
  puts ""

  @latest_awards_year = GoodreadsBooks::Scraper.scrape_awards_year
  load_choice_awards_books(@latest_awards_year)

  main_menu
end

#list_booksObject

– main_menu –



66
67
68
69
70
71
72
73
74
75
# File 'lib/goodreads_books/cli.rb', line 66

def list_books
  system "clear"
  puts ""
  puts "---------- #{@awards_year} Goodreads Choice Awards Winners----------"
  puts ""

  GoodreadsBooks::Book.find_all_by_year(@awards_year).each.with_index(1) do |book, index|
    puts "#{index}. #{book.category}: #{book.title}"
  end
end

#load_choice_awards_books(awards_year) ⇒ Object

– call –



20
21
22
23
24
25
26
# File 'lib/goodreads_books/cli.rb', line 20

def load_choice_awards_books(awards_year)
  @awards_year = awards_year
  puts "Loading The Goodreads Choice Awards Winners for #{awards_year} ..."
  if GoodreadsBooks::Book.find_all_by_year(awards_year).empty?
    GoodreadsBooks::Scraper.scrape_books(awards_year)
  end
end

– load_choice_awards_books –



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/goodreads_books/cli.rb', line 28

def main_menu
  valid_input = true
  input = nil
  while input != "exit"
    book_count = GoodreadsBooks::Book.find_all_by_year(@awards_year).count
    list_books

    puts ""
    if !valid_input
      puts "Please enter the book number to see more information:".colorize(:red)
      puts "(You can also enter another Choice Awards year of 2010 or later, or type 'exit')".colorize(:red)
      valid_input = true
    else
      puts "Please enter the book number to see more information:".colorize(:green)
      puts "(You can also enter another Choice Awards year of 2010 or later, or type 'exit')".colorize(:green)
    end

    input = gets.strip

    if input.downcase == "exit"
      break
    elsif input.to_i.between?(1, book_count)
      book = GoodreadsBooks::Book.find_all_by_year(@awards_year)[input.to_i - 1]
      if !book.author
        GoodreadsBooks::Scraper.scrape_book_details(book)
      end
      view_book(book)
    elsif input.to_i.between?(BASE_YEAR, @latest_awards_year)
      load_choice_awards_books(input.to_i)
    else
      valid_input = false
    end
  end

  puts ""
  puts "Thank you for using the Goodreads Choice Awards Application."
end

#view_book(book) ⇒ Object

– display_books –



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/goodreads_books/cli.rb', line 77

def view_book(book)
  system "clear"
  puts ""
  puts "---------- #{@awards_year} BEST #{book.category.upcase} Winner ----------"
  puts ""
  puts "Title:        #{book.title}"
  puts "Author:       #{book.author}"
  puts "Votes:        #{book.vote}"
  puts "Description:  #{book.description}"

  puts ""
  puts "Would you like to open Goodreads website to view this book? Enter Y to open the website.".colorize(:green)
  input = gets.strip.downcase

  if input.downcase == "y"
    system("open #{book.url}")
  end
end