Class: Countries

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCountries

Returns a new instance of Countries.



8
9
10
11
12
# File 'lib/Countries.rb', line 8

def initialize
  @root = File.expand_path('..', __FILE__)
  jsondata_temp = File.read(@root + '/countries.json')
  @countries = JSON.parse(jsondata_temp)
end

Instance Attribute Details

#countriesObject

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



26
27
28
29
30
31
32
# File 'lib/Countries.rb', line 26

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



34
35
36
37
38
39
40
# File 'lib/Countries.rb', line 34

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_dataObject



21
22
23
24
# File 'lib/Countries.rb', line 21

def save_data
  jsondata = @countries.to_json
  File.write(@root + '/countries.json', jsondata)
end

#search_country(name) ⇒ Object



14
15
16
17
18
19
# File 'lib/Countries.rb', line 14

def search_country(name)
  @countries.each do |country|
    return country if country['name']['common'] == name
  end
  nil
end