Class: CountriesCli::CLI

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

Overview

CLI Controller

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
14
15
# File 'lib/countries_cli/cli.rb', line 7

def initialize()
   # Get all countries from API in JSON format
    page = open("https://restcountries.eu/rest/v2/all")
    countries = JSON.parse(page.read)
    # Parse JSON to objects
    countries.each do |country|
        CountriesCli::COUNTRY.new(country)
    end 
end

Instance Method Details

#callObject



17
18
19
20
21
22
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
# File 'lib/countries_cli/cli.rb', line 17

def call
    puts "Information about countries around the world!"
    display_options
    user_input = gets.chomp.to_i
    while(user_input != 12)
        case user_input
        when 1
            list_all_country_names
        when 2
            search_by_name
        when 3
            search_by_suffix
        when 4
            search_by_currency_name
        when 5
            search_by_currency_symbol
        when 6
            search_by_language
        when 7
            search_by_certain_population
        when 8
            list_all_countries_sorted_by_population
        when 9
            search_by_capital
        when 10
            search_by_region
        when 11
            search_by_subregion
        when 12
        else
            puts "Option does not exist. Please enter a correct option."
        end 
        display_options
        user_input = gets.chomp.to_i
    end
    goodbye
end

#display_optionsObject



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

def display_options 
    puts "Enter an option (1-12): "
    puts "1. Display all country names"
    puts "2. Search country by name"
    puts "3. Search country by suffix"
    puts "4. Search countries with given currency name"
    puts "5. Search countries with given currency symbol"
    puts "6. Search countries with given language"
    puts "7. Search countries with certain population"
    puts "8. Display all countries sorted by population(ascending or descending)"
    puts "9. Search by capital"
    puts "10. Search by region"
    puts "11. Search by subregion"
    puts "12. Exit"
end

#goodbyeObject



224
225
226
# File 'lib/countries_cli/cli.rb', line 224

def goodbye
    puts "Exited the gem. Have a nice day! =)"
end

#list_all_countries_sorted_by_populationObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/countries_cli/cli.rb', line 163

def list_all_countries_sorted_by_population
    puts "Ascending(a) or descending(d)?"
    input = gets.chomp
    if input == "d" 
        countries_sorted = CountriesCli::COUNTRY.all.sort{|country1, country2| country2.population <=> country1.population}
        countries_sorted.each do |country|
            country.info
            puts "\n"
        end 
    elsif input == "a" 
        countries_sorted = CountriesCli::COUNTRY.all.sort{|country1, country2| country1.population <=> country2.population}
        countries_sorted.each do |country|
            country.info
            puts "\n"
        end 
    else
        puts "Did not understand command. Please enter a or d."
        list_all_countries_sorted_by_population
    end
end

#list_all_country_namesObject



71
72
73
# File 'lib/countries_cli/cli.rb', line 71

def list_all_country_names
    CountriesCli::COUNTRY.all_country_names
end

#search_by_capitalObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/countries_cli/cli.rb', line 184

def search_by_capital
    puts "Please enter the capital: "
    input = gets.chomp
    country = CountriesCli::COUNTRY.search_by_capital(input)
    if country
        country.info
        puts "\n"
    else 
        puts "Invalid capital. Please enter a valid capital."
    end
end

#search_by_certain_populationObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/countries_cli/cli.rb', line 141

def search_by_certain_population
    puts "Greater(g) than or lower(l) than?"
    input = gets.chomp
    
    if input.downcase == "l" || input.downcase == "lower" || input.downcase == "g" || input.downcase == "greater" || input.downcase == "h" || input.downcase == "higher"
        puts "Enter population (ex: 10000): "
        population = gets.chomp.to_i
        countries = CountriesCli::COUNTRY.search_all_with_population(input, population)
        if countries.count > 0
            countries.each do |country|
                country.info
                puts "\n"
            end 
        else 
            puts "No countries with the specified population were found."
        end
    else
        puts "Wrong input. Please try again.\n"
        search_by_certain_population
    end 
end

#search_by_currency_nameObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/countries_cli/cli.rb', line 113

def search_by_currency_name
    puts "Please enter the currency name: "
    input = gets.chomp
    countries = CountriesCli::COUNTRY.search_all_with_currency_name(input)
    if countries.count > 0
        countries.each do |country|
            country.info
            puts "\n"
        end 
    else 
        puts "No countries with given currency name were found."
    end 
end

#search_by_currency_symbolObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/countries_cli/cli.rb', line 99

def search_by_currency_symbol
    puts "Please enter the currency symbol: "
    input = gets.chomp
    countries = CountriesCli::COUNTRY.search_all_with_currency_symbol(input)
    if countries.count > 0
        countries.each do |country|
            country.info
            puts "\n"
        end 
    else 
        puts "No countries with given currency symbol were found."
    end 
end

#search_by_languageObject



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

def search_by_language
    puts "Please enter a language: "
    input = gets.chomp
    countries = CountriesCli::COUNTRY.search_all_with_language(input)
    if countries.count > 0
        countries.each do |country|
            country.info
            puts "\n"
        end 
    else 
        puts "No countries with given language were found."
    end
end

#search_by_nameObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/countries_cli/cli.rb', line 75

def search_by_name 
    puts "Please enter the name: "
    input = gets.chomp
    country = CountriesCli::COUNTRY.search_by_name(input)
    if country
        country.info
        puts "\n"
    else 
        puts "Invalid name. Please list all the countries and choose a name from there."
    end 
end

#search_by_regionObject



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/countries_cli/cli.rb', line 196

def search_by_region
    puts "Please enter the region: "
    input = gets.chomp
    countries = CountriesCli::COUNTRY.search_by_region(input)
    if countries.count > 0
        countries.each do |country|
            country.info
            puts "\n"
        end
    else 
        puts "Invalid region. Please enter a valid region."
    end
end

#search_by_subregionObject



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/countries_cli/cli.rb', line 210

def search_by_subregion
    puts "Please enter the subregion: "
    input = gets.chomp
    countries = CountriesCli::COUNTRY.search_by_subregion(input)
    if countries.count > 0
        countries.each do |country|
            country.info
            puts "\n"
        end
    else 
        puts "Invalid subregion. Please enter a valid subregion."
    end
end

#search_by_suffixObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/countries_cli/cli.rb', line 87

def search_by_suffix
    puts "Please enter the suffix: "
    input = gets.chomp
    country = CountriesCli::COUNTRY.search_by_suffix(input)
    if country
        country.info
        puts "\n"
    else 
        puts "Country with given suffix not found."
    end 
end