Class: SoftwareBinder::CLI

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

Constant Summary collapse

@@last_category_search =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.last_category_searchObject



20
21
22
# File 'lib/software_binder/cli.rb', line 20

def self.last_category_search
  @@last_category_search
end

Instance Method Details

#callObject



5
6
7
8
9
10
11
# File 'lib/software_binder/cli.rb', line 5

def call
  puts "Welcome to Software Binder!"
  load_categories
  list_categories
  list_softwares
  reset?
end

#list_categoriesObject



24
25
26
27
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
# File 'lib/software_binder/cli.rb', line 24

def list_categories
  puts "Please enter any Keyword or Alphabet(or '#') to search software categories!"
  puts "or enter 'exit' to quit."
  input = gets.strip
  exit if input == 'exit'
  if input.length == 1
    find_by_alphabet = SoftwareBinder::Category.find_by_alphabet(input)
    if find_by_alphabet.size > 0
      find_by_alphabet.each.with_index(1) do |category, i|
        @@last_category_search << category
        puts "#{i}. #{category.name}"
      end
    else
      puts "There is no result for your search."
      self.list_categories
    end
  elsif input.to_i == 100
    result = SoftwareBinder::Category.find_by_keyword("")[0 .. 99]
    result.each.with_index(1) do |category, i|
      @@last_category_search << category
      puts "#{i}. #{category.name}"
    end
  else
    find_by_keyword = SoftwareBinder::Category.find_by_keyword(input)
    if find_by_keyword.size > 0
      find_by_keyword.each.with_index(1) do |category, i|
        @@last_category_search << category
        puts "#{i}. #{category.name}"
      end
    else
      puts "There is no result for your search."
      self.list_categories
    end
  end
end

#list_softwaresObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/software_binder/cli.rb', line 60

def list_softwares
  puts "Please enter the category index number, or type 'exit' to quit"
  input = gets.strip
  exit if input == 'exit'
  if !input.to_i.between?(1, self.class.last_category_search.size)
    puts "It is not valid input."
    self.list_softwares
  else
    selected_category = self.class.last_category_search[input.to_i - 1]
    SoftwareBinder::Scraper.scrape_softwares(selected_category)
    SoftwareBinder::Software.all.each.with_index(1) do |list, i|
      puts "#{i}. #{list.name}"
      puts "Review Rating: #{list.overall_rating}/5.0 from #{list.reviews} reviews"
      puts "Description: #{list.description}"
      puts
    end
  end
end

#load_categoriesObject



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

def load_categories
  self.class.last_category_search.clear
  SoftwareBinder::Software.reset
  SoftwareBinder::Category.reset
  SoftwareBinder::Scraper.scrape_categories
end

#reset?Boolean

Returns:

  • (Boolean)


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

def reset?
  puts "Would you like to check other software? (Y/N)"
  answer = gets.strip
  case answer.downcase
  when "y"
    self.call
  when "n"
    exit
  else
    puts "I do not understand your answer"
    puts "Please answer with Y or N"
    self.reset?
  end
end