Class: WWFinder::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#inputObject

Returns the value of attribute input.



2
3
4
# File 'lib/ww_finder/cli.rb', line 2

def input
  @input
end

#selected_cityObject

Returns the value of attribute selected_city.



2
3
4
# File 'lib/ww_finder/cli.rb', line 2

def selected_city
  @selected_city
end

#selected_continentObject

Returns the value of attribute selected_continent.



2
3
4
# File 'lib/ww_finder/cli.rb', line 2

def selected_continent
  @selected_continent
end

#selected_countryObject

Returns the value of attribute selected_country.



2
3
4
# File 'lib/ww_finder/cli.rb', line 2

def selected_country
  @selected_country
end

Instance Method Details

#app_loopObject



10
11
12
13
14
# File 'lib/ww_finder/cli.rb', line 10

def app_loop 
    while @input != "exit"
        print_countries_list
    end
end

#error(entry_point) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ww_finder/cli.rb', line 105

def error(entry_point)
    exit if input == "exit"
    puts "\nOops, that's not a valid #{entry_point} option".light_white.on_red.bold
    case entry_point 
    when "continent"
        get_country_selection
    when "country"
        get_country_selection
    when "city"
        get_city_selection 
    when "building"
        get_building_selection
    end
end

#get_building_selectionObject



86
87
88
89
90
# File 'lib/ww_finder/cli.rb', line 86

def get_building_selection
    instructions
    get_user_selection
    valid_input(selected_city.buildings) ? show_building : error("building")
end

#get_city_selectionObject



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

def get_city_selection
    instructions
    get_user_selection
    valid_input(selected_country.cities) ? set_city : error("city")
end

#get_country_selectionObject



37
38
39
40
41
42
43
44
45
# File 'lib/ww_finder/cli.rb', line 37

def get_country_selection
    puts "Enter a continent number and country number eg '3, 2'".light_white.on_green
    get_user_selection
    cc_input = input.split(',')
    @input = cc_input[0]
    valid_input(WWFinder::Continent.all) ? set_continent : error("continent")
    @input = cc_input[1]
    valid_input(selected_continent.countries) ? set_country : error("country")
end

#get_user_selectionObject



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ww_finder/cli.rb', line 125

def get_user_selection
    @input = gets.strip
    case @input
    when "cities"
        print_cities_list
    when "back"
        print_buildings_list
    when "exit"
        goodbye
        exit
    end
end

#goodbyeObject



142
143
144
145
# File 'lib/ww_finder/cli.rb', line 142

def goodbye 
    puts "\nHappy working! Don't forget to take a break!\n".light_white.on_magenta.bold
    exit
end

#instructionsObject



82
83
84
# File 'lib/ww_finder/cli.rb', line 82

def instructions 
    puts "\nEnter the option number to see more details".green
end


74
75
76
77
78
79
80
# File 'lib/ww_finder/cli.rb', line 74

def print_buildings_list
    @selected_city.get_buildings
    @selected_city.buildings.each.with_index(1) do | building, idx |
        puts "#{idx}: #{building.address}"
    end
    get_building_selection
end


56
57
58
59
60
61
# File 'lib/ww_finder/cli.rb', line 56

def print_cities_list
    selected_country.cities.each.with_index(1) do | city, idx |
        puts "#{idx}: #{city.name}"
    end
    get_city_selection
end


26
27
28
29
30
31
32
33
34
35
# File 'lib/ww_finder/cli.rb', line 26

def print_countries_list
    resize_screen
    table_data = []
    WWFinder::Continent.all.each.with_index(1) do | cont, i | 
        table_data << {"#{i.to_s.light_white.bold}. #{cont.name.light_white.on_magenta}": WWFinder::Continent.all.map{| cont | cont.countries[i - 1] ? "#{i.to_s.magenta}: #{cont.countries[i - 1].name}" : nil }}
    end  
    table = TTY::Table.new(table_data)
    puts table.render(:ascii)
    get_country_selection
end

#resize_screenObject



16
17
18
# File 'lib/ww_finder/cli.rb', line 16

def resize_screen
    print "\e[8;50;150t"
end

#runObject



4
5
6
7
8
# File 'lib/ww_finder/cli.rb', line 4

def run 
    welcome
    app_loop
    start
end

#set_cityObject



69
70
71
72
# File 'lib/ww_finder/cli.rb', line 69

def set_city 
    @selected_city = selected_country.find_city(user_num_input)
    print_buildings_list
end

#set_continentObject



47
48
49
# File 'lib/ww_finder/cli.rb', line 47

def set_continent
    @selected_continent = WWFinder::Continent.find(user_num_input)
end

#set_countryObject



51
52
53
54
# File 'lib/ww_finder/cli.rb', line 51

def set_country
    @selected_country = selected_continent.find_country(user_num_input)
    print_cities_list
end

#show_buildingObject



92
93
94
95
96
97
98
99
# File 'lib/ww_finder/cli.rb', line 92

def show_building
    building = @selected_city.buildings[user_num_input]
    building.prepare_details
    puts "\n#{building.name}".light_white.on_magenta.bold
    puts building.info
    puts building.address.light_white.on_light_magenta
    what_next
end

#user_num_inputObject



138
139
140
# File 'lib/ww_finder/cli.rb', line 138

def user_num_input
    input.to_i - 1 
end

#valid_input(data) ⇒ Object



101
102
103
# File 'lib/ww_finder/cli.rb', line 101

def valid_input(data)
    user_num_input >= 0 && user_num_input <= data.length - 1
end

#welcomeObject



20
21
22
23
24
# File 'lib/ww_finder/cli.rb', line 20

def welcome 
    puts "\nHai there! Are you looking for a place to work today?".light_white.on_magenta.bold
    puts "Hit enter to continue".green
    get_user_selection
end

#what_nextObject



120
121
122
123
# File 'lib/ww_finder/cli.rb', line 120

def what_next
    puts "\nIf you are done, type 'exit', otherwise type 'back' to see other buildings in this city, 'cities' to see other cities in this country, or hit enter to see more countries".green
    get_user_selection
end