Class: Covidstats::Continents
- Inherits:
-
Object
- Object
- Covidstats::Continents
- Defined in:
- lib/covidstats/continents.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#active_cases ⇒ Object
Returns the value of attribute active_cases.
-
#name ⇒ Object
Returns the value of attribute name.
-
#new_cases ⇒ Object
Returns the value of attribute new_cases.
-
#new_deaths ⇒ Object
Returns the value of attribute new_deaths.
-
#serious_critical ⇒ Object
Returns the value of attribute serious_critical.
-
#total_cases ⇒ Object
Returns the value of attribute total_cases.
-
#total_deaths ⇒ Object
Returns the value of attribute total_deaths.
-
#total_recovered ⇒ Object
Returns the value of attribute total_recovered.
-
#total_tests ⇒ Object
Returns the value of attribute total_tests.
Class Method Summary collapse
- .all ⇒ Object
- .continent_reports ⇒ Object
- .create_from_collection(continents_array) ⇒ Object
- .merge_hash(continents_array) ⇒ Object
Instance Method Summary collapse
-
#hash_attr(hash) ⇒ Object
given a hash, returns all the attributes.
-
#initialize(continent_hash) ⇒ Continents
constructor
A new instance of Continents.
- #save ⇒ Object
Constructor Details
#initialize(continent_hash) ⇒ Continents
Returns a new instance of Continents.
5 6 7 8 |
# File 'lib/covidstats/continents.rb', line 5 def initialize(continent_hash) hash_attr(continent_hash) save end |
Instance Attribute Details
#active_cases ⇒ Object
Returns the value of attribute active_cases.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def active_cases @active_cases end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def name @name end |
#new_cases ⇒ Object
Returns the value of attribute new_cases.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def new_cases @new_cases end |
#new_deaths ⇒ Object
Returns the value of attribute new_deaths.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def new_deaths @new_deaths end |
#serious_critical ⇒ Object
Returns the value of attribute serious_critical.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def serious_critical @serious_critical end |
#total_cases ⇒ Object
Returns the value of attribute total_cases.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def total_cases @total_cases end |
#total_deaths ⇒ Object
Returns the value of attribute total_deaths.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def total_deaths @total_deaths end |
#total_recovered ⇒ Object
Returns the value of attribute total_recovered.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def total_recovered @total_recovered end |
#total_tests ⇒ Object
Returns the value of attribute total_tests.
2 3 4 |
# File 'lib/covidstats/continents.rb', line 2 def total_tests @total_tests end |
Class Method Details
.all ⇒ Object
94 95 96 |
# File 'lib/covidstats/continents.rb', line 94 def self.all @@all end |
.continent_reports ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/covidstats/continents.rb', line 54 def self.continent_reports continents_array = [] hash_continents = Covidstats::API.get_reports asia = hash_continents.select{|hash| hash["Continent"] == "Asia"} continents_array << asia africa = hash_continents.select{|hash| hash["Continent"] == "Africa"} continents_array << africa europe = hash_continents.select{|hash| hash["Continent"] == "Europe"} continents_array << europe n_america = hash_continents.select{|hash| hash["Continent"] == "North America"} continents_array << n_america s_america = hash_continents.select{|hash| hash["Continent"] == "South America"} continents_array << s_america australia = hash_continents.select{|hash| hash["Continent"] == "Australia/Oceania"} continents_array << australia continents_array end |
.create_from_collection(continents_array) ⇒ Object
72 73 74 75 76 |
# File 'lib/covidstats/continents.rb', line 72 def self.create_from_collection(continents_array) continents_array.each do |continent| #this is a hash self.new(continent) end end |
.merge_hash(continents_array) ⇒ Object
10 11 12 13 14 15 16 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 |
# File 'lib/covidstats/continents.rb', line 10 def self.merge_hash(continents_array) new_array = [] continents_array.each do |array| new_hash = {} new_hash["Continent"] = "" new_hash["TotalCases"] = 0 new_hash["NewCases"] = 0 new_hash["TotalDeaths"] = 0 new_hash["NewDeaths"] = 0 new_hash["TotalRecovered"] = 0 new_hash["ActiveCases"] = 0 new_hash["TotalTests"] = 0 new_hash["Serious_Critical"] = 0 array.each do |hash| hash.each do |key, value| new_hash["Continent"] = hash["Continent"] if key != "Country" && key != "" && key != "Deaths_1M_pop" && key != "TotCases_1M_Pop" && key != "Population" && key != "Tests_1M_Pop" && key != "Continent" && value.class == String hash[key] = value.gsub(",","").gsub("+","") hash[key] = hash[key].to_i if key == "TotalCases" new_hash["TotalCases"] += hash[key] elsif key == "NewCases" new_hash["NewCases"] += hash[key] elsif key == "TotalDeaths" new_hash["TotalDeaths"] += hash[key] elsif key == "NewDeaths" new_hash["NewDeaths"] += hash[key] elsif key == "TotalRecovered" new_hash["TotalRecovered"] += hash[key] elsif key == "ActiveCases" new_hash["ActiveCases"] += hash[key] elsif key == "TotalTests" new_hash["TotalTests"] += hash[key] elsif key == "Serious_Critical" new_hash["Serious_Critical"] += hash[key] end end end end new_array << new_hash end new_array end |
Instance Method Details
#hash_attr(hash) ⇒ Object
given a hash, returns all the attributes
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/covidstats/continents.rb', line 78 def hash_attr(hash) #given a hash, returns all the attributes @total_cases = hash["TotalCases"] @new_cases = hash["NewCases"] @total_deaths = hash["TotalDeaths"] @new_deaths = hash["NewDeaths"] @total_recovered = hash["TotalRecovered"] @active_cases = hash["ActiveCases"] @total_tests = hash["TotalTests"] @name = hash["Continent"] @serious_critical = hash["Serious_Critical"] end |
#save ⇒ Object
90 91 92 |
# File 'lib/covidstats/continents.rb', line 90 def save @@all << self end |