Class: StellingBanjos::Cli

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

Constant Summary collapse

"https://www.elderly.com/collections/stelling"

Instance Method Summary collapse

Instance Method Details

#add_newlines(string, max_length) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/stelling_banjos/cli.rb', line 141

def add_newlines(string, max_length)
  words = string.split(' ')
  lines = []
  current_line = ''
  until words.empty?
    current_line += " #{words.shift}"
    if current_line.length >= max_length
      lines << current_line
      current_line = ''
    end
  end
  lines.push(current_line).join("\n       ").lstrip
end

#create_banjosObject



42
43
44
45
# File 'lib/stelling_banjos/cli.rb', line 42

def create_banjos
  StellingBanjos::Scraper.scrape_catalog_page(LINK)
  StellingBanjos::Banjos.create_from_catalog(StellingBanjos::Scraper.all)
end

#display_banjosObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/stelling_banjos/cli.rb', line 47

def display_banjos
  puts ""
  puts "    ██████╗ █████╗ ████████╗ █████╗ ██╗      ██████╗  ██████╗  ".colorize(:green)
  puts "    ██╔════╝██╔══██╗╚══██╔══╝██╔══██╗██║     ██╔═══██╗██╔════╝ ".colorize(:green)
  puts "    ██║     ███████║   ██║   ███████║██║     ██║   ██║██║  ███╗".colorize(:green)
  puts "    ██║     ██╔══██║   ██║   ██╔══██║██║     ██║   ██║██║   ██║".colorize(:green)
  puts "    ╚██████╗██║  ██║   ██║   ██║  ██║███████╗╚██████╔╝╚██████╔╝".colorize(:green)
  puts "    ╚═════╝╚═╝  ╚═╝   ╚═╝   ╚═╝  ╚═╝╚══════╝ ╚═════╝  ╚═════╝  ".colorize(:green)
  puts "    ███████████████████████████████████████████████████████████".colorize(:green)
  puts "                   ~ Supplied by www.elderly.com ~".colorize(:red)
  puts ""

  StellingBanjos::Banjos.all.each.with_index do |banjo, index|
    if banjo.sold_out?
      puts "    #{index + 1}. #{banjo.name} - #{banjo.price}" + " - SOLD OUT".colorize(:red)
    else
      puts "    #{index + 1}. #{banjo.name} - #{banjo.price}"
    end
  end
  second_menu
end

#exit_outObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/stelling_banjos/cli.rb', line 155

def exit_out
  puts ""
  puts "    ██████╗  ██████╗  ██████╗ ██████╗ ██████╗ ██╗   ██╗███████╗██╗ ".colorize(:red)
  puts "    ██╔════╝ ██╔═══██╗██╔═══██╗██╔══██╗██╔══██╗╚██╗ ██╔╝██╔════╝██║".colorize(:red)
  puts "    ██║  ███╗██║   ██║██║   ██║██║  ██║██████╔╝ ╚████╔╝ █████╗  ██║".colorize(:red)
  puts "    ██║   ██║██║   ██║██║   ██║██║  ██║██╔══██╗  ╚██╔╝  ██╔══╝  ╚═╝".colorize(:red)
  puts "    ╚██████╔╝╚██████╔╝╚██████╔╝██████╔╝██████╔╝   ██║   ███████╗██╗".colorize(:red)
  puts "    ╚═════╝  ╚═════╝  ╚═════╝ ╚═════╝ ╚═════╝    ╚═╝   ╚══════╝╚═╝ ".colorize(:red)
  puts "    ███████████████████████████████████████████████████████████████".colorize(:red)
  puts ""
end

#first_menuObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stelling_banjos/cli.rb', line 19

def first_menu
  puts ""
  puts "    MENU"
  puts "    ══════════════════════════════════"
  puts "    -Press 'Enter' to view the catalog".colorize(:green)
  puts "    -Type 'Exit' to leave".colorize(:red)
  puts "    ══════════════════════════════════"

  input = gets.strip.downcase
  case input
  when ""
    puts "    Loading...".colorize(:red)
    create_banjos
    display_banjos
  when "exit"
    exit_out
  else
    puts "    Please make a valid entry".colorize(:red)
    puts ""
    first_menu
  end
end

#info_page_display(input) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stelling_banjos/cli.rb', line 90

def info_page_display(input)
  input = input.to_i - 1

  link = StellingBanjos::Banjos.all[input].link
  description = StellingBanjos::Scraper.scrape_info_page(link)
  formatted_description = add_newlines(description, 68)
  name = StellingBanjos::Banjos.all[input].name
  price = StellingBanjos::Banjos.all[input].price

  puts ""
  puts ""
  puts "        █ #{name.upcase} - #{price}".colorize(:green)
  puts ""
  puts "        █ #{formatted_description}"
  puts ""
  if StellingBanjos::Banjos.all[input].sold_out?
    puts "        █ SORRY! This banjo is SOLD OUT from elderly.com! Let's check Google instead...".colorize(:red)
    puts "        ==> https://www.google.com/search?q=#{name.downcase.gsub(" ", "-")}".colorize(:blue)
  else
    puts "        █ Interested in buying? Go here:"
    puts "        ==> #{link}".colorize(:blue)
  end
  puts ""
  third_menu
end

#second_menuObject



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

def second_menu
  puts ""
  puts "    MENU"
  puts "    ══════════════════════════════════════════"
  puts "    -Enter a banjo number for more information".colorize(:green)
  puts "    -Type 'Exit' to exit".colorize(:red)
  puts "    ══════════════════════════════════════════"

  input = gets.strip
  if input.downcase == "exit"
    exit_out
  elsif input.to_i.between?(1, StellingBanjos::Banjos.all.length)
    puts "    Loading...".colorize(:red)
    info_page_display(input)
  else
    puts ""
    puts "    Please make a valid entry".colorize(:red)
    second_menu
  end
end

#startObject



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

def start
  puts ""
  puts "    ██████╗  █████╗ ███╗   ██╗     ██╗ ██████╗ ███████╗██╗".colorize(:green)
  puts "    ██╔══██╗██╔══██╗████╗  ██║     ██║██╔═══██╗██╔════╝██║".colorize(:green)
  puts "    ██████╔╝███████║██╔██╗ ██║     ██║██║   ██║███████╗██║".colorize(:green)
  puts "    ██╔══██╗██╔══██║██║╚██╗██║██   ██║██║   ██║╚════██║╚═╝".colorize(:green)
  puts "    ██████╔╝██║  ██║██║ ╚████║╚█████╔╝╚██████╔╝███████║██╗".colorize(:green)
  puts "    ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═══╝ ╚════╝  ╚═════╝ ╚══════╝╚═╝".colorize(:green)
  puts "    ██████████████████████████████████████████████████████".colorize(:green)
  puts ""
  puts "    Hello! Welcome to the Stelling Banjos catalog! The best banjos in the world!"
  first_menu
end

#third_menuObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/stelling_banjos/cli.rb', line 116

def third_menu
  puts ""
  puts "    MENU"
  puts "    ══════════════════════════════════════════"
  puts "    -Enter another banjo number".colorize(:green)
  puts "    -Type 'Catalog' to view the catalog again".colorize(:green)
  puts "    -Type 'Exit' to exit".colorize(:red)
  puts "    ══════════════════════════════════════════"
  puts ""

  input = gets.strip
  if input.downcase == "exit"
    exit_out
  elsif input.to_i.between?(1, StellingBanjos::Banjos.all.length)
    puts "    Loading...".colorize(:red)
    info_page_display(input)
  elsif input.downcase == "catalog"
    display_banjos
  else
    puts ""
    puts "    Please make a valid entry".colorize(:red)
    third_menu
  end
end