Class: PawesomeParks::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



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

def call
puts "\n\n
██████╗  █████╗ ██╗    ██╗███████╗███████╗ ██████╗ ███╗   ███╗███████╗    ██████╗  █████╗ ██████╗ ██╗  ██╗███████╗
██╔══██╗██╔══██╗██║    ██║██╔════╝██╔════╝██╔═══██╗████╗ ████║██╔════╝    ██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝██╔════╝
██████╔╝███████║██║ █╗ ██║█████╗  ███████╗██║   ██║██╔████╔██║█████╗      ██████╔╝███████║██████╔╝█████╔╝ ███████╗
██╔═══╝ ██╔══██║██║███╗██║██╔══╝  ╚════██║██║   ██║██║╚██╔╝██║██╔══╝      ██╔═══╝ ██╔══██║██╔══██╗██╔═██╗ ╚════██║
██║     ██║  ██║╚███╔███╔╝███████╗███████║╚██████╔╝██║ ╚═╝ ██║███████╗    ██║     ██║  ██║██║  ██║██║  ██╗███████║
╚═╝     ╚═╝  ╚═╝ ╚══╝╚══╝ ╚══════╝╚══════╝ ╚═════╝ ╚═╝     ╚═╝╚══════╝    ╚═╝     ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝
\n".green
puts "\nWelcome to Pawesome Parks!\n\n"
puts "There are currently 51 dog-friendly parks within the City of Sydney council region in NSW, Australia. With a range of off-leash hours and other restrictions, this app will let you explore the most suitable park to take your furry friend!"
retrieve_dog_parks
end

#display_optionsObject



52
53
54
55
56
57
58
59
60
# File 'lib/pawesome_parks/cli.rb', line 52

def display_options
    puts "\nMenu:"
    puts "1 - List all dog parks"
    puts "2 - Search for dog parks by suburb or postcode"
    puts "3 - View parks with unrestricted off-leash hours"
    puts "/help - Provides a list of available commands\n"
    puts "\nPlease select an option from the menu above by entering '1', '2', or '3' below. You can also enter '/exit' to leave the application or '/menu' to return to this menu at any time:".green
    gets.strip.downcase
end

#exit_appObject



166
167
168
169
# File 'lib/pawesome_parks/cli.rb', line 166

def exit_app
    puts "\nThank you for using Sydney's Pawesome Parks! Goodbye!\n\n"
    exit
end

#get_park_details_by_nameObject



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

def get_park_details_by_name
    loop do 
        choice = gets.strip.downcase
        if PawesomeParks::Park.park_names.find { |name| name.downcase == choice }
            puts ""
            chosen_park_instance = PawesomeParks::Park.find_by_name choice
            print_park chosen_park_instance
            puts "\nPlease enter another park name for more details or enter '/menu' to return to the main menu:".green
        elsif choice == "/exit"
            exit_app
        elsif choice == "/menu"
            menu_selection
        elsif choice == "/help"
            help
        else
            puts "\nInvalid park name. Please enter a park name included in the list above or enter '/menu to return to the main menu".green
        end
    end
end

#helpObject



143
144
145
146
147
148
149
150
# File 'lib/pawesome_parks/cli.rb', line 143

def help
    puts "\nAvailable Commands:"
    puts "/menu - Return to the main menu (use at any time)".yellow
    puts "/help - Brings up this list of available commands (use at any time)".yellow
    puts "/print - Use when prompted to list information".yellow
    puts "/exit - Terminates the program (use at any time)".yellow
    menu_selection
end

#list_dog_parksObject



62
63
64
65
66
67
# File 'lib/pawesome_parks/cli.rb', line 62

def list_dog_parks
    puts "\nAll Dog Parks:\n"
    PawesomeParks::Park.park_names.each { |park| puts park }
    puts "\nEnter one of the above park names to see more details, or enter '/menu' to return to the main menu:".green
    get_park_details_by_name
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pawesome_parks/cli.rb', line 33

def menu_selection
    loop do
        choice = display_options
        if choice == "1"
            list_dog_parks
        elsif choice == "2"
            search_for_park
        elsif choice == "3"
            off_leash_hours
        elsif choice.downcase == "/exit"
            exit_app
        elsif choice.downcase == "/help"
            help
        else
            puts "\nSorry, that is an invalid selection, please enter another option:".green
        end
    end
end

#off_leash_hoursObject



135
136
137
138
139
140
141
# File 'lib/pawesome_parks/cli.rb', line 135

def off_leash_hours 
    puts "\nParks with unrestricted off-leash hours include:\n\n"
    parks = PawesomeParks::Park.unrestricted_off_leash_hours
    parks.each { |park| puts park.name }
    puts "\nEnter one of the above parks name's to see more details or enter '/menu' to return to the main menu:".green
    get_park_details_by_name
end


152
153
154
155
156
157
158
# File 'lib/pawesome_parks/cli.rb', line 152

def print_locations
    puts "\nSuburbs with dog parks include:"
    PawesomeParks::Park.suburbs.each { |suburb| puts suburb.capitalize }
    puts "\nPostcodes with dog parks include:"
    PawesomeParks::Park.postcodes.each { |postcode| puts postcode }
    search_for_park
end


160
161
162
163
164
# File 'lib/pawesome_parks/cli.rb', line 160

def print_park park
    puts "#{park.name}:
    #{park.off_leash_description}
    Address: #{park.street_address}, #{park.suburb}, #{park.postcode}\n\n"
end

#retrieve_dog_parksObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pawesome_parks/cli.rb', line 17

def retrieve_dog_parks
    num_of_retries = 0
    until num_of_retries == 3 do 
        parks = PawesomeParks::API.new
        if parks.get_parks == "error"
            puts "\nSorry, there has been an error accessing City of Sydney's dog park information. Reattempting for you now... (reattempt #{num_of_retries + 1} / 3)".red
        else
            parks.make_parks
            menu_selection
        end
        num_of_retries += 1
    end
    puts "\nUnfortunately we have been unable to access the information needed for this program to run and it will now be terminated.\n\n".red
    exit
end

#search_by_postcode(location) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pawesome_parks/cli.rb', line 111

def search_by_postcode location 
    if PawesomeParks::Park.postcodes.include? location.to_i
        parks = PawesomeParks::Park.find_by_postcode location.to_i
        puts "\nDog parks at postcode #{location} include:\n\n"
        parks.each { |park| print_park park }
    else
        puts "\nUnable to locate a dog park at that postcode. Postcodes with a dog park include:\n".green
        puts PawesomeParks::Park.postcodes.each { |postcode| postcode }
        puts ""
    end
end

#search_by_suburb(location) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pawesome_parks/cli.rb', line 123

def search_by_suburb location 
    if PawesomeParks::Park.suburbs.include? location
        parks = PawesomeParks::Park.find_by_suburb location
        puts "\nDog parks in #{location.capitalize} include:\n\n"
        parks.each { |park| print_park park }
    else
        puts "\nUnable to locate a dog park at that suburb name. Suburbs with a dog park include:\n".green
        PawesomeParks::Park.suburbs.each { |suburb| puts suburb.capitalize }
        puts ""
    end
end

#search_for_parkObject



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

def search_for_park 
    puts "\nPlease enter a suburb name or postcode within the City of Sydney. Enter '/print' to see a list of possible suburbs and postcodes to choose from:".green
    loop do 
        location = gets.strip.downcase
        if location.to_i != 0
            search_by_postcode location
            puts "\nPlease enter another postcode or suburb name for park details, enter '/menu' to return to the main menu:".green
        elsif location == "/print"
            print_locations
        elsif location == "/exit"
            exit_app
        elsif location == "/menu"
            menu_selection
        elsif location == "/help"
            help
        else
            search_by_suburb location
            puts "\nPlease enter another suburb name or postcode for park details, enter '/menu' to return to the main menu:".green
        end
    end
end