Class: Countries
- Inherits:
-
Object
- Object
- Countries
- Defined in:
- lib/Countries.rb
Instance Attribute Summary collapse
-
#countries ⇒ Object
Returns the value of attribute countries.
Instance Method Summary collapse
-
#initialize ⇒ Countries
constructor
A new instance of Countries.
- #make_shortlist(key1, key2) ⇒ Object
- #region_count(region_name) ⇒ Object
- #save_data ⇒ Object
- #search_country(name) ⇒ Object
Constructor Details
#initialize ⇒ Countries
Returns a new instance of Countries.
8 9 10 11 |
# File 'lib/Countries.rb', line 8 def initialize jsondata_temp = File.read('countries.json') @countries = JSON.parse(jsondata_temp) end |
Instance Attribute Details
#countries ⇒ Object
Returns the value of attribute countries.
6 7 8 |
# File 'lib/Countries.rb', line 6 def countries @countries end |
Instance Method Details
#make_shortlist(key1, key2) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/Countries.rb', line 25 def make_shortlist(key1, key2) short_list = [] @countries.each do |index| short_list << { key1 => index[key1]['common'], key2 => index[key2] } end short_list end |
#region_count(region_name) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/Countries.rb', line 33 def region_count(region_name) region_number = 0 @countries.each do |index| region_number += 1 if index['region'] == region_name end region_number end |
#save_data ⇒ Object
20 21 22 23 |
# File 'lib/Countries.rb', line 20 def save_data jsondata = @countries.to_json File.write('countries.json', jsondata) end |
#search_country(name) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/Countries.rb', line 13 def search_country(name) @countries.each do |country| return country if country['name']['common'] == name end nil end |