Class: CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#generated_urlObject (readonly)

Returns the value of attribute generated_url.



3
4
5
# File 'lib/pup_finder/cli.rb', line 3

def generated_url
  @generated_url
end

#listObject (readonly)

Returns the value of attribute list.



3
4
5
# File 'lib/pup_finder/cli.rb', line 3

def list
  @list
end

#selected_sizeObject (readonly)

Returns the value of attribute selected_size.



3
4
5
# File 'lib/pup_finder/cli.rb', line 3

def selected_size
  @selected_size
end

Instance Method Details

#callObject



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

def call
    what_size
        if get_size != "exit"
            please_wait
            scrape_pages
            get_info
        end
end

#continue_getsObject



149
150
151
# File 'lib/pup_finder/cli.rb', line 149

def continue_gets
    gets.strip.downcase
end

#continue_messageObject



143
144
145
146
147
# File 'lib/pup_finder/cli.rb', line 143

def continue_message
    puts "\nWould you like to see another pup?"
    puts "You can enter yes, no, or menu to go back to the main menu"
    puts "Enter exit to end this program\n\n"
end

#enter_a_numberObject



106
107
108
# File 'lib/pup_finder/cli.rb', line 106

def enter_a_number
    puts "\n\nEnter a number to see more information about that pup \n\n"
end

#find_all_breeds_by_size(size) ⇒ Object

Returns an array of all breed objects (with just name, size and link attributes) that match size



93
94
95
# File 'lib/pup_finder/cli.rb', line 93

def find_all_breeds_by_size(size)
    Breed.all.select {|breed_object| breed_object.breed_size == size}
end

#generate_url(input) ⇒ Object



78
79
80
# File 'lib/pup_finder/cli.rb', line 78

def generate_url(input)
    url_to_scrape = URLGenerator.new_url(input)  
end

#get_infoObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pup_finder/cli.rb', line 28

def get_info
    @list = list_breeds
    enter_a_number
    number_to_see = get_number(list)
    
    if number_to_see == "exit"
        puts Rainbow("\n\nGoodbye!").cyan
    else
        breed_to_scrape = list[number_to_see - 1]
        exists = Breed.all.select {|breed_object| breed_object.breed_name == breed_to_scrape}
        
        if !exists[0].weight
            AKCScraper.make_breed(number_to_see, list)
        end

    more_info(number_to_see)
    continue_message
    options
    end
end

#get_number(list) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pup_finder/cli.rb', line 110

def get_number(list)
    range_to_select_from = (1..list.length).to_a
    number_entered = gets.strip
        if number_entered != "exit"
            number_entered = number_entered.to_i
            loop do
                if !range_to_select_from.include?(number_entered)
                    puts "Please enter a number between #{range_to_select_from[0]} and #{range_to_select_from[-1]}"
                    return get_number(list)
                else
                    return number_entered
                end
            end
        else
            return "exit"
        end
end

#get_sizeObject



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

def get_size
    sizes_to_select_from = ["tiny", "small", "medium", "large", "huge"]
    input = gets.strip.downcase

    if input == "tiny"
        input = "xsmall"
    elsif input == "huge"
        input = "xlarge"
    elsif input == "exit"
        puts Rainbow("Goodbye!").cyan
        return "exit"
    elsif !sizes_to_select_from.include?(input)
        puts "Please try again"
        input = get_size
    end
    
    @selected_size = input
    return input
end

#list_breedsObject

Returns a numbered list of all breeds by the selected size



99
100
101
102
103
104
# File 'lib/pup_finder/cli.rb', line 99

def list_breeds
    find_all_breeds_by_size(selected_size).map.with_index do |breed, index|
        puts Rainbow("#{index + 1}. #{breed.breed_name}").cyan
        breed.breed_name
    end
end

#more_info(number_entered) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pup_finder/cli.rb', line 128

def more_info(number_entered)
    breed_to_see = @list[number_entered - 1]
    Breed.all.each do |breed|
        if breed.breed_name == breed_to_see
            puts Rainbow("\nThe #{breed.breed_name}\n").bold
            puts Rainbow("      __\n(___()'`;\n/,    /`\n\\\\'--\\\\\n\n").cyan.bright
            puts Rainbow("Temperament: ").bold + Rainbow("#{breed.temperament}\n").cyan
            puts Rainbow("Weight: ").bold + Rainbow("#{breed.weight}\n").cyan
            puts Rainbow("Life Expectancy: ").bold + Rainbow("#{breed.life_expectancy}\n").cyan
            puts Rainbow("Description: ").bold + Rainbow("#{breed.description}\n").cyan
        end
    end
end

#optionsObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/pup_finder/cli.rb', line 153

def options
    case continue_gets
        when "yes"
            puts "\n"
            get_info
        when "menu"
            puts "\n"
            call
        when "no"
            puts Rainbow("\nGoodbye!").cyan
        when "exit"
            puts Rainbow("\nGoodbye!").cyan
        else
            puts "Please try again"
            options
    end
end

#please_waitObject



74
75
76
# File 'lib/pup_finder/cli.rb', line 74

def please_wait
    puts "\nPlease wait while we load the pups!\n\n"
end

#scrape_pagesObject



82
83
84
85
86
87
88
89
90
# File 'lib/pup_finder/cli.rb', line 82

def scrape_pages
    #this prevents the program from instantiating new breeds if they already exist
    if @generated_url == generate_url(selected_size)
    
    else
        @generated_url = generate_url(selected_size)
        AKCScraper.get_all_pages_of_breeds(generated_url)
    end
end

#welcomeObject



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

def welcome
    puts Rainbow(" _____               ______ _           _").cyan            
    puts Rainbow("|  __ \\\             |  ____(_)         | |").cyan     
    puts Rainbow("| |__) |   _ _ __   | |__   _ _ __   __| | ___ _ __").cyan
    puts Rainbow("|  ___/ | | | '_ \\\  |  __| | | '_ \\\ / _` |/ _ \\\ '__|").cyan
    puts Rainbow("| |   | |_| | |_) | | |    | | | | | (_| |  __/ |").cyan    
    puts Rainbow("|_|    \\\__,_| .__/  |_|    |_|_| |_|\\\__,_|\\\___|_|").cyan  
    puts Rainbow("            | |                                  ").cyan    
    puts Rainbow("            |_|                                  ").cyan
    puts Rainbow("\nWelcome to PupFinder!").bright
    
    call
end

#what_sizeObject



49
50
51
52
# File 'lib/pup_finder/cli.rb', line 49

def what_size
    puts "What size pupper are you interested in?"
    puts "You can say tiny, small, medium, large, or huge\n\n"
end