countries_cli 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/bin/country +2 -1
- data/lib/Country.rb +6 -1
- data/lib/Country/cli.rb +190 -19
- data/lib/Country/country.rb +111 -0
- data/lib/Country/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2050fe1a4e0b494c19b10d3e3012b08252655184
|
4
|
+
data.tar.gz: 792c7edd5c30702b5245cb9d957e693d3e2da4e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 862c49ca41e6b71373083ce373bdc49f134f510da29bd744d663838f39b07d92bee34eace557cccddf9ebb07c7d0e4996806b26cdfaf965b066b459d74065c43
|
7
|
+
data.tar.gz: 61a056599c7ba808fbc4cd4ad40dda52e92b2d912b973f9608f6bf25b8e5364abaecbeec758bcf2b80a554ed56c4cca727e1e23be27f7b8c36470285b5d2f086
|
data/Gemfile.lock
CHANGED
data/bin/country
CHANGED
data/lib/Country.rb
CHANGED
data/lib/Country/cli.rb
CHANGED
@@ -1,30 +1,49 @@
|
|
1
|
+
require_relative 'country'
|
2
|
+
require 'pry'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
1
5
|
# CLI Controller
|
2
6
|
class Country::CLI
|
7
|
+
|
8
|
+
def initialize()
|
9
|
+
# Get all countries from API in JSON format
|
10
|
+
page = open("https://restcountries.eu/rest/v2/all")
|
11
|
+
countries = JSON.parse(page.read)
|
12
|
+
# Parse JSON to objects
|
13
|
+
countries.each do |country|
|
14
|
+
Country::COUNTRY.new(country)
|
15
|
+
end
|
16
|
+
end
|
3
17
|
|
4
18
|
def call
|
5
19
|
puts "Information about countries around the world!"
|
6
|
-
|
7
|
-
|
20
|
+
display_options
|
21
|
+
user_input = gets.chomp.to_i
|
22
|
+
while(user_input != 12)
|
8
23
|
case user_input
|
9
24
|
when 1
|
10
|
-
|
25
|
+
list_all_country_names
|
11
26
|
when 2
|
12
|
-
|
27
|
+
search_by_name
|
13
28
|
when 3
|
14
|
-
|
29
|
+
search_by_suffix
|
15
30
|
when 4
|
16
|
-
|
31
|
+
search_by_currency_name
|
17
32
|
when 5
|
18
|
-
|
33
|
+
search_by_currency_symbol
|
19
34
|
when 6
|
20
|
-
|
35
|
+
search_by_language
|
21
36
|
when 7
|
22
|
-
|
37
|
+
search_by_certain_population
|
23
38
|
when 8
|
24
|
-
|
39
|
+
list_all_countries_sorted_by_population
|
25
40
|
when 9
|
26
|
-
|
41
|
+
search_by_capital
|
27
42
|
when 10
|
43
|
+
search_by_region
|
44
|
+
when 11
|
45
|
+
search_by_subregion
|
46
|
+
when 12
|
28
47
|
else
|
29
48
|
puts "Option does not exist. Please enter a correct option."
|
30
49
|
end
|
@@ -35,20 +54,172 @@ class Country::CLI
|
|
35
54
|
end
|
36
55
|
|
37
56
|
def display_options
|
38
|
-
puts "Enter an option (1-
|
57
|
+
puts "Enter an option (1-12): "
|
39
58
|
puts "1. All country names"
|
40
59
|
puts "2. Search country by name"
|
41
60
|
puts "3. Search countries that start with given letter"
|
42
|
-
puts "4. Search countries with given currency
|
43
|
-
puts "5. Search countries with given
|
44
|
-
puts "6. Search countries with
|
45
|
-
puts "7.
|
46
|
-
puts "8.
|
47
|
-
puts "9.
|
48
|
-
puts "10.
|
61
|
+
puts "4. Search countries with given currency name"
|
62
|
+
puts "5. Search countries with given currency symbol"
|
63
|
+
puts "6. Search countries with given language"
|
64
|
+
puts "7. Search countries with certain population"
|
65
|
+
puts "8. All countries sorted by population(ascending or descending)"
|
66
|
+
puts "9. Search by capital"
|
67
|
+
puts "10. Search by region"
|
68
|
+
puts "11. Search by subregion"
|
69
|
+
puts "12. Exit"
|
70
|
+
end
|
71
|
+
|
72
|
+
def list_all_country_names
|
73
|
+
Country::COUNTRY.all_country_names
|
74
|
+
end
|
75
|
+
|
76
|
+
def search_by_name
|
77
|
+
puts "Please enter the name: "
|
78
|
+
input = gets.chomp
|
79
|
+
country = Country::COUNTRY.search_by_name(input)
|
80
|
+
if country
|
81
|
+
country.info
|
82
|
+
else
|
83
|
+
puts "Invalid name. Please list all the countries and choose a name from there."
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def search_by_suffix
|
88
|
+
puts "Please enter the suffix: "
|
89
|
+
input = gets.chomp
|
90
|
+
country = Country::COUNTRY.search_by_suffix(input)
|
91
|
+
if country
|
92
|
+
country.info
|
93
|
+
else
|
94
|
+
puts "Country with given suffix not found."
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def search_by_currency_symbol
|
99
|
+
puts "Please enter the currency symbol: "
|
100
|
+
input = gets.chomp
|
101
|
+
countries = Country::COUNTRY.search_all_with_currency_symbol(input)
|
102
|
+
if countries.count > 0
|
103
|
+
countries.each do |country|
|
104
|
+
country.info
|
105
|
+
puts "\n"
|
106
|
+
end
|
107
|
+
else
|
108
|
+
puts "No countries with given currency symbol were found."
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def search_by_currency_name
|
113
|
+
puts "Please enter the currency name: "
|
114
|
+
input = gets.chomp
|
115
|
+
countries = Country::COUNTRY.search_all_with_currency_name(input)
|
116
|
+
if countries.count > 0
|
117
|
+
countries.each do |country|
|
118
|
+
country.info
|
119
|
+
puts "\n"
|
120
|
+
end
|
121
|
+
else
|
122
|
+
puts "No countries with given currency name were found."
|
123
|
+
end
|
49
124
|
end
|
50
125
|
|
126
|
+
def search_by_language
|
127
|
+
puts "Please enter a language: "
|
128
|
+
input = gets.chomp
|
129
|
+
countries = Country::COUNTRY.search_all_with_language(input)
|
130
|
+
if countries.count > 0
|
131
|
+
countries.each do |country|
|
132
|
+
country.info
|
133
|
+
puts "\n"
|
134
|
+
end
|
135
|
+
else
|
136
|
+
puts "No countries with given language were found."
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def search_by_certain_population
|
141
|
+
puts "Greater(g) than or lower(l) than?"
|
142
|
+
input = gets.chomp
|
143
|
+
|
144
|
+
if input.downcase == "l" || input.downcase == "lower" || input.downcase == "g" || input.downcase == "greater" || input.downcase == "h" || input.downcoase == "higher"
|
145
|
+
puts "Enter population: "
|
146
|
+
population = gets.chomp.to_i
|
147
|
+
countries = Country::COUNTRY.search_all_with_population(input, population)
|
148
|
+
if countries.count > 0
|
149
|
+
countries.each do |country|
|
150
|
+
country.info
|
151
|
+
puts "\n"
|
152
|
+
end
|
153
|
+
else
|
154
|
+
puts "No countries with given language were found."
|
155
|
+
end
|
156
|
+
else
|
157
|
+
puts "Wrong input. Please try again.\n"
|
158
|
+
search_by_certain_population
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def list_all_countries_sorted_by_population
|
163
|
+
puts "Ascending(a) or descending(d)?"
|
164
|
+
input = gets.chomp
|
165
|
+
if input == "d"
|
166
|
+
countries_sorted = Country::COUNTRY.all.sort{|country1, country2| country2.population <=> country1.population}
|
167
|
+
countries_sorted.each do |country|
|
168
|
+
country.info
|
169
|
+
end
|
170
|
+
elsif input == "a"
|
171
|
+
countries_sorted = Country::COUNTRY.all.sort{|country1, country2| country1.population <=> country2.population}
|
172
|
+
countries_sorted.each do |country|
|
173
|
+
country.info
|
174
|
+
puts "\n"
|
175
|
+
end
|
176
|
+
else
|
177
|
+
puts "Did not understand command. Please enter a or d."
|
178
|
+
list_all_countries_sorted_by_population
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def search_by_capital
|
183
|
+
puts "Please enter the capital: "
|
184
|
+
input = gets.chomp
|
185
|
+
country = Country::COUNTRY.search_by_capital(input)
|
186
|
+
if country
|
187
|
+
country.info
|
188
|
+
else
|
189
|
+
puts "Invalid capital. Please enter a valid capital."
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def search_by_region
|
194
|
+
puts "Please enter the region: "
|
195
|
+
input = gets.chomp
|
196
|
+
countries = Country::COUNTRY.search_by_region(input)
|
197
|
+
if countries.count > 0
|
198
|
+
countries.each do |country|
|
199
|
+
country.info
|
200
|
+
puts "\n"
|
201
|
+
end
|
202
|
+
else
|
203
|
+
puts "Invalid region. Please enter a valid region."
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def search_by_subregion
|
208
|
+
puts "Please enter the subregion: "
|
209
|
+
input = gets.chomp
|
210
|
+
countries = Country::COUNTRY.search_by_subregion(input)
|
211
|
+
if countries.count > 0
|
212
|
+
countries.each do |country|
|
213
|
+
country.info
|
214
|
+
puts "\n"
|
215
|
+
end
|
216
|
+
else
|
217
|
+
puts "Invalid subregion. Please enter a valid subregion."
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
51
221
|
def goodbye
|
52
222
|
puts "Exited the gem. Have a nice day! =)"
|
53
223
|
end
|
54
224
|
end
|
225
|
+
|
data/lib/Country/country.rb
CHANGED
@@ -0,0 +1,111 @@
|
|
1
|
+
class Country::COUNTRY
|
2
|
+
|
3
|
+
attr_accessor :name, :currencies, :languages, :flag, :population, :capital, :region, :subregion
|
4
|
+
|
5
|
+
@@all = []
|
6
|
+
|
7
|
+
def initialize(attr_hash)
|
8
|
+
attr_hash.each do |attribute, value|
|
9
|
+
# binding.pry
|
10
|
+
if self.respond_to?(attribute)
|
11
|
+
self.send(("#{attribute}="), value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
if self.capital == ""
|
15
|
+
self.capital = "n/a"
|
16
|
+
end
|
17
|
+
if self.region == ""
|
18
|
+
self.region = "n/a"
|
19
|
+
end
|
20
|
+
if self.subregion == ""
|
21
|
+
self.subregion = "n/a"
|
22
|
+
end
|
23
|
+
@@all << self
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.all
|
27
|
+
@@all
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.all_country_names
|
31
|
+
# binding.pry
|
32
|
+
self.all.each_with_index do |country, i|
|
33
|
+
puts "#{i+1}. #{country.name}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.search_by_name(name)
|
38
|
+
self.all.detect{|country| country.name == name}
|
39
|
+
end
|
40
|
+
|
41
|
+
def info
|
42
|
+
puts "Name: #{self.name}"
|
43
|
+
puts "Capital: #{self.capital}"
|
44
|
+
puts "Currencies: #{self.currencies}"
|
45
|
+
puts "Languages: #{self.languages}"
|
46
|
+
puts "Population: #{self.population}"
|
47
|
+
puts "Flag: #{self.flag}"
|
48
|
+
puts "Region: #{self.region}"
|
49
|
+
puts "Subregion: #{self.subregion}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.search_by_suffix(suffix)
|
53
|
+
self.all.detect{|country| country.name.match(/^(#{suffix})/)}
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.search_all_with_currency_symbol(currency_symbol)
|
57
|
+
self.all.select do |country|
|
58
|
+
bool = false
|
59
|
+
country.currencies.each do |currency_info|
|
60
|
+
if currency_info["symbol"] == currency_symbol
|
61
|
+
bool = true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
bool
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.search_all_with_currency_name(currency_name)
|
69
|
+
self.all.select do |country|
|
70
|
+
bool = false
|
71
|
+
country.currencies.each do |currency_info|
|
72
|
+
if currency_info["name"] && currency_info["name"].downcase == currency_name.downcase
|
73
|
+
bool = true
|
74
|
+
end
|
75
|
+
end
|
76
|
+
bool
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.search_all_with_language(language)
|
81
|
+
self.all.select do |country|
|
82
|
+
bool = false
|
83
|
+
country.languages.each do |language_info|
|
84
|
+
if language_info["name"] && (language_info["name"].downcase == language.downcase || language_info["nativeName"].downcase == language.downcase)
|
85
|
+
bool = true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
bool
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.search_all_with_population(comparison, population)
|
93
|
+
if comparison == "l"
|
94
|
+
self.all.select{|country| country.population < population}.sort_by{|country| country.population}
|
95
|
+
elsif comparison == "g"
|
96
|
+
self.all.select{|country| country.population > population}.sort_by{|country| country.population}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.search_by_capital(capital)
|
101
|
+
self.all.detect{|country| country.capital.downcase == capital.downcase}
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.search_by_region(region)
|
105
|
+
self.all.select{|country| country.region.downcase == region.downcase}
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.search_by_subregion(subregion)
|
109
|
+
self.all.select{|country| country.subregion.downcase == subregion.downcase}
|
110
|
+
end
|
111
|
+
end
|
data/lib/Country/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: countries_cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenneth Young Castro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|