Class: CatBreeds::CLI

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

Constant Summary collapse

BASE_PATH =
"http://www.vetstreet.com"

Instance Method Summary collapse

Instance Method Details

#goodbyeObject

upon exiting the program, the user is presented a thank you and cat artwork



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cat_breeds/cli.rb', line 166

def goodbye #upon exiting the program, the user is presented a thank you and cat artwork
  puts ""
  puts "Thanks for learning about cats!"
  puts ""
  puts "        _                        "
       puts "        \\`*-.                    "
       puts "         )  _`-.                 "
       puts "        .  : `. .                "
       puts "        : _   '  \\               "
       puts "        ; *` _.   `*-._          "
       puts "        `-.-'          `-.       "
       puts "          ;       `       `.     "
       puts "          :.       .        \\    "
       puts "          . \\  .   :   .-'   .   "
       puts "          '  `+.;  ;  '      :   "
       puts "          :  '  |    ;       ;-. "
       puts "          ; '   : :`-:     _.`* ;"
       puts " [bye] .*' /  .*' ; .*`- +'  `*' "
       puts "       `*-*   `*-*  `*-*'        "
  puts ""
  exit
end

#list_cats(breeds) ⇒ Object

indexes through array of cat breeds returned from make_cats and lists each one for user to select from



23
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
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cat_breeds/cli.rb', line 23

def list_cats(breeds) #indexes through array of cat breeds returned from make_cats and lists each one for user to select from
  puts ""
  breeds[@i..@i+@j].each.with_index(@i + 1) {|b,i|puts "[#{i}] #{b.name}"}
  puts "[all]" if @j != 49
  puts "[less]" if @j == 49
  puts "[next]" if @i == 0 && @j == 9
  puts "[back||next]" if @i >= 10 && @i+@j <49
  puts "[back]" if @i+@j >= 49 && @j == 9
  puts ""
  puts "type [exit] at any time to close"
  puts ""
  puts "Enter the cat breed or number that you would like to learn more about:"
  input = gets.strip
  if input.to_i > 0 && input.to_i <= breeds.length
    view_breed_overview(CatBreeds::Cat.all[input.to_i - 1])
  elsif CatBreeds::Cat.all.detect{|breed|breed.name.downcase == input.downcase}
    view_breed_overview(CatBreeds::Cat.all.detect{|breed| breed.name.downcase == input.downcase})
  elsif input.downcase == "all"
    @i = 0
    @j = 49
    list_cats(breeds)
  elsif input.downcase == "less"
    @i = 0
    @j = 9
    list_cats(breeds)
  elsif input.downcase == "next" && @i+@j == 49
    puts ""
    puts "That's all the cat breeds!"
    list_cats(breeds)
  elsif input.downcase == "next"
    @i += 10
    list_cats(breeds)
  elsif input.downcase == "back" && @i == 0
    puts ""
    puts "That's all the cat breeds!"
    list_cats(breeds)
  elsif input.downcase == "back"
    @i -= 10
    list_cats(breeds)
  elsif input.downcase == "exit"
    self.goodbye
  else
    puts ""
    puts "Invalid Input. Please try again."
    self.list_cats(breeds)
  end
end

#make_catsObject

Scrapes site index page to gather all of the breeds and breed urls



16
17
18
19
20
21
# File 'lib/cat_breeds/cli.rb', line 16

def make_cats #Scrapes site index page to gather all of the breeds and breed urls
  breeds_array = CatBreeds::Scraper.scrape_index(BASE_PATH + "/cats/breeds")
  breeds_array.collect do |breed|
    CatBreeds::Cat.new(breed[:name], breed[:page_url])
  end
end

#startObject



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

def start
  puts ""
  puts "----------------------------------------"
  puts "               CAT BREEDS               "
  puts "----------------------------------------"
  breeds = make_cats
  @i = 0
  @j = 9
  list_cats(breeds)
end

#view_breed_overview(breed) ⇒ Object

upon selecting a specific cat breed, this method scrapes that breed’s url page for more information



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cat_breeds/cli.rb', line 71

def view_breed_overview(breed) #upon selecting a specific cat breed, this method scrapes that breed's url page for more information
  details = CatBreeds::Scraper.scrape_profile(BASE_PATH + breed.page_url)
  breed.add_details(details)
  puts ""
  puts "----------------------------------------"
  puts "Overview of the #{breed.name}"
  puts "----------------------------------------"
  puts ""
  puts "#{breed.blurb}"
  puts ""
  puts "Fun Fact!"
  puts "#{breed.fun_fact}"
  view_more_details(breed)
end

#view_more_details(breed) ⇒ Object

after more info is scraped from view_breed_overview, more options are presented to learn more about breed



86
87
88
89
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cat_breeds/cli.rb', line 86

def view_more_details(breed) #after more info is scraped from view_breed_overview, more options are presented to learn more about breed
  puts ""
  puts "Learn more about the #{breed.name}:"
  puts "[1] Description"
  puts "[2] Characteristics"
  puts "[3] History"
  puts "[4] Personality"
  puts "[5] Grooming"
  puts "[6] Health"
  puts "[Back] to list of all cat breeds"
  input = gets.strip
  topic = nil
  info = nil
  case input.downcase
  when "1","description"
    topic = "Description"
    info = breed.description
  when "2","characteristics"
    topic = "Characteristics"
    info = Proc.new{
      i = 0
      while i < breed.characteristics.length
        puts "#{breed.characteristics[i][0]}: #{breed.characteristics[i][1]}"
        i += 1
      end
      }
  when "3","history"
    topic = "History"
    info = breed.history
  when "4","personality"
    topic = "Personality"
    info = breed.personality
  when "5","grooming"
    topic = "Grooming"
    info = breed.grooming
  when "6","health"
    topic = "Health"
    info = breed.health
  when "back"
    start
  when "exit"
    goodbye
  else
    puts ""
    puts "Invalid Input. Please try again."
    view_more_details(breed)
  end
  view_topic(breed, topic, info)

end

#view_topic(breed, topic, info) ⇒ Object

once specific topic is selected, more information on that topic is presented to the user



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cat_breeds/cli.rb', line 137

def view_topic(breed, topic, info) #once specific topic is selected, more information on that topic is presented to the user
  puts ""
  puts "----------------------------------------"
  puts "#{breed.name} - #{topic}"
  puts "----------------------------------------"
  puts ""
  if info.is_a?(String)
    puts "#{info}"
  else 
    info.call
  end
  puts ""
  puts "[1] Learn more about the #{breed.name}"
  puts "[2] Learn about a different cat breed"
  input = gets.strip
  case input.downcase
  when "1"
    view_more_details(breed)
  when "2","personality"
    start
  when "exit"
    goodbye
  else
    puts ""
    puts "Invalid Input. Please try again."
    view_topic(breed, topic, info)
  end
end