Class: Country::CLI
- Inherits:
-
Object
- Object
- Country::CLI
- Defined in:
- lib/Country/cli.rb
Overview
CLI Controller
Instance Method Summary collapse
- #call ⇒ Object
- #display_options ⇒ Object
- #goodbye ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #list_all_countries_sorted_by_population ⇒ Object
- #list_all_country_names ⇒ Object
- #search_by_capital ⇒ Object
- #search_by_certain_population ⇒ Object
- #search_by_currency_name ⇒ Object
- #search_by_currency_symbol ⇒ Object
- #search_by_language ⇒ Object
- #search_by_name ⇒ Object
- #search_by_region ⇒ Object
- #search_by_subregion ⇒ Object
- #search_by_suffix ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
8 9 10 11 12 13 14 15 16 |
# File 'lib/Country/cli.rb', line 8 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| Country::COUNTRY.new(country) end end |
Instance Method Details
#call ⇒ Object
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 54 |
# File 'lib/Country/cli.rb', line 18 def call puts "Information about countries around the world!" 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 user_input = gets.chomp.to_i end goodbye end |
#display_options ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/Country/cli.rb', line 56 def puts "Enter an option (1-12): " puts "1. All country names" puts "2. Search country by name" puts "3. Search countries that start with given letter" 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. 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 |
#goodbye ⇒ Object
221 222 223 |
# File 'lib/Country/cli.rb', line 221 def goodbye puts "Exited the gem. Have a nice day! =)" end |
#list_all_countries_sorted_by_population ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/Country/cli.rb', line 162 def list_all_countries_sorted_by_population puts "Ascending(a) or descending(d)?" input = gets.chomp if input == "d" countries_sorted = Country::COUNTRY.all.sort{|country1, country2| country2.population <=> country1.population} countries_sorted.each do |country| country.info end elsif input == "a" countries_sorted = Country::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_names ⇒ Object
72 73 74 |
# File 'lib/Country/cli.rb', line 72 def list_all_country_names Country::COUNTRY.all_country_names end |
#search_by_capital ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/Country/cli.rb', line 182 def search_by_capital puts "Please enter the capital: " input = gets.chomp country = Country::COUNTRY.search_by_capital(input) if country country.info else puts "Invalid capital. Please enter a valid capital." end end |
#search_by_certain_population ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/Country/cli.rb', line 140 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.downcoase == "higher" puts "Enter population: " population = gets.chomp.to_i countries = Country::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 given language were found." end else puts "Wrong input. Please try again.\n" search_by_certain_population end end |
#search_by_currency_name ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/Country/cli.rb', line 112 def search_by_currency_name puts "Please enter the currency name: " input = gets.chomp countries = Country::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_symbol ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/Country/cli.rb', line 98 def search_by_currency_symbol puts "Please enter the currency symbol: " input = gets.chomp countries = Country::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_language ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/Country/cli.rb', line 126 def search_by_language puts "Please enter a language: " input = gets.chomp countries = Country::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_name ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/Country/cli.rb', line 76 def search_by_name puts "Please enter the name: " input = gets.chomp country = Country::COUNTRY.search_by_name(input) if country country.info else puts "Invalid name. Please list all the countries and choose a name from there." end end |
#search_by_region ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/Country/cli.rb', line 193 def search_by_region puts "Please enter the region: " input = gets.chomp countries = Country::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_subregion ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/Country/cli.rb', line 207 def search_by_subregion puts "Please enter the subregion: " input = gets.chomp countries = Country::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_suffix ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/Country/cli.rb', line 87 def search_by_suffix puts "Please enter the suffix: " input = gets.chomp country = Country::COUNTRY.search_by_suffix(input) if country country.info else puts "Country with given suffix not found." end end |