Class: Kaizoku::CommandLineInterface

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

Overview

Your code goes here…

Instance Method Summary collapse

Instance Method Details

#exit_screenObject



7
8
9
10
11
# File 'lib/kaizoku.rb', line 7

def exit_screen
  puts "Thank you for using Kaizoku!"
  separator
  exit
end

#gem_description(subcategory) ⇒ Object



118
119
120
121
122
# File 'lib/kaizoku.rb', line 118

def gem_description(subcategory)
  url = "https://www.ruby-toolbox.com" + subcategory[0].css("a").attribute("href").value
  doc = Nokogiri::HTML(open(url))
  doc
end

#gem_description_output(doc) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/kaizoku.rb', line 124

def gem_description_output(doc)
  puts "Here's the gem for the job!"
  puts "  "
  puts "#{pastel.bright_yellow(doc.css(".is-size-4").first.children.text)}"
  puts "  "
  puts "#{pastel.bright_yellow('score: ')}" + doc.css(".score").first.css("span").text
  puts "#{pastel.bright_yellow('description: ')}" + doc.css(".description.column").first.text
  puts "#{pastel.bright_yellow('github: ')}" + doc.css(".links.column").first.css(".button.is-white")[1].attributes["href"].text
  separator
  exit_screen
end

#get_categoryObject



45
46
47
48
49
50
51
# File 'lib/kaizoku.rb', line 45

def get_category
  doc = Nokogiri::HTML(open("https://www.ruby-toolbox.com"))
  doc.css(".category-group").each do |category|
    puts category.css("h3").text
  end
  get_subcategory_screen
end

#get_category_screenObject



29
30
31
# File 'lib/kaizoku.rb', line 29

def get_category_screen
  get_category_validation
end

#get_category_validationObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kaizoku.rb', line 33

def get_category_validation
  input = gets.chomp
  separator
  if input == "list"
    get_category
  elsif input == "exit"
    exit_screen
  else
    unrecognized_input
  end
end

#get_gem_screenObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kaizoku.rb', line 91

def get_gem_screen
  separator
  puts "Type the subcategory to see the best gem for that category."
  puts "#{pastel.bright_yellow('back')} - go back to the main screen."
  puts "#{pastel.bright_yellow('exit')} - go quit the app."
  separator
  input = gets.chomp
  separator
  if input == "exit"
    exit_screen
  elsif input == "back"
    greeting_screen
  else
    match_input_with_subcategory(input)
    subcategory = match_input_with_subcategory(input)
    doc = gem_description(subcategory)
    gem_description_output(doc)
  end

end

#get_subcategory(input) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kaizoku.rb', line 72

def get_subcategory(input)
  separator
  doc = Nokogiri::HTML(open("https://www.ruby-toolbox.com"))
  @subcategories = []
  doc.css(".category-group").each do |category|
    if category.css("h3").text == input
      @category = category
      puts "Here are the #{pastel.bright_yellow('subcategories')}, if any:"
      puts "  "
      category.css(".column.is-half-desktop").each do |subcategory|
        @subcategories << subcategory
        puts subcategory.css("a span").text
      end
      get_gem_screen
    end
  end
end

#get_subcategory_screenObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kaizoku.rb', line 54

def get_subcategory_screen
  separator
  puts "Please make a selection:"
  puts "  "
  puts "#{pastel.bright_yellow('[category]')} - enter one of the categories see gem subcategories."
  puts "#{pastel.bright_yellow('back')} - go back to the main screen."
  puts "#{pastel.bright_yellow('exit')} - go quit the app."
  separator
  input = gets.chomp
  if input == "exit"
    exit_screen
  elsif input == "back"
    greeting_screen
  else
    get_subcategory(input)
  end
end

#greeting_screenObject



19
20
21
22
23
24
25
26
27
# File 'lib/kaizoku.rb', line 19

def greeting_screen
  separator
  puts "Welcome to #{pastel.bright_yellow('Kaizoku')}!"
  puts "                                                      "
  puts "Easily find the best gem for the task at hand."
  puts "Type #{pastel.bright_yellow('list')} to see a list of gem categories."
  separator
  get_category_screen
end

#match_input_with_subcategory(input) ⇒ Object



112
113
114
115
116
# File 'lib/kaizoku.rb', line 112

def match_input_with_subcategory(input)
  @subcategories.select do |subcategory|
    input == subcategory.css("a span").text
  end
end

#pastelObject



136
137
138
139
# File 'lib/kaizoku.rb', line 136

def pastel
  pastel = Pastel.new
  pastel
end

#runObject



148
149
150
# File 'lib/kaizoku.rb', line 148

def run
  greeting_screen
end

#separatorObject



141
142
143
144
145
146
# File 'lib/kaizoku.rb', line 141

def separator
  pastel
  puts "                                                      "
  puts pastel.bright_magenta("======================================================")
  puts "                                                      "
end

#unrecognized_inputObject



13
14
15
16
# File 'lib/kaizoku.rb', line 13

def unrecognized_input
  puts "Input not recognized."
  exit_screen
end