Class: Covidstats::CLI

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

Instance Method Summary collapse

Instance Method Details

#ask_for_choicesObject



19
20
21
22
23
24
25
26
27
# File 'lib/covidstats/cli.rb', line 19

def ask_for_choices       
  puts "Would you like to do anything else? (y/n)".colorize(:light_green)
  input = gets.strip
  if input == "y"
    list_of_actions
  else
    puts "Ok, thanks for using Covidstats today!".colorize(:light_green)
  end
end

#callObject



2
3
4
5
6
# File 'lib/covidstats/cli.rb', line 2

def call
  puts "Welcome to the daily corona tracker! This CLI app provides real time data regarding the ongoing coronavirus pandemic and includes information from numerous countries and continents.".colorize(:light_green)
  get_all_countries
  world_stats
end

#continent_selectObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/covidstats/cli.rb', line 78

def continent_select
  get_all_continents
  prompt = TTY::Prompt.new
  choice = prompt.select("Please select which continent:".colorize(:light_green)) do |prompt|
    prompt.choice "Asia"
    prompt.choice "South America"
    prompt.choice "North America"
    prompt.choice "Europe"
    prompt.choice "Africa"
    prompt.choice "Australia/Oceania"
  end
  Covidstats::Continents.all.each do |continent|
    if continent.name == choice
      display_stats_continent(continent)
    end
  end
  ask_for_choices
end

#country_selectObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/covidstats/cli.rb', line 66

def country_select
  puts "Enter the name of the country you would like to search. Please note that for the country United States, please enter".colorize(:light_green) + " USA".colorize(:yellow) + " for United Kingdom, please enter".colorize(:light_green) + " UK".colorize(:yellow) + " and for South Korea, please enter".colorize(:light_green) + " S. Korea".colorize(:yellow)
  input = gets.strip
  input = input.capitalize if input != "USA" && input != "S. Korea" && input != "UK"
  Covidstats::Country.all.each do |country|
    if country.name == input
      display_stats_country(country)
    end
  end
  ask_for_choices
end

#display_stats_continent(continent) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/covidstats/cli.rb', line 112

def display_stats_continent(continent)
  puts "Total Cases:".colorize(:light_blue) + " #{continent.total_cases}".colorize(:yellow)
  puts "New Cases:".colorize(:light_blue) + " #{continent.new_cases}".colorize(:yellow)
  puts "Total Deaths:".colorize(:light_blue) + " #{continent.total_deaths}".colorize(:yellow)
  puts "New Deaths:".colorize(:light_blue) + " #{continent.new_deaths}".colorize(:yellow)
  puts "Total Recovered:".colorize(:light_blue) + " #{continent.total_recovered}".colorize(:yellow)
  puts "Active Cases:".colorize(:light_blue) + " #{continent.active_cases}".colorize(:yellow)
  puts "Total Tests:".colorize(:light_blue) + " #{continent.total_tests}".colorize(:yellow)
  puts "Number of Serious/Critical Cases:".colorize(:light_blue) + " #{continent.serious_critical}".colorize(:yellow)
end

#display_stats_country(country) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/covidstats/cli.rb', line 97

def display_stats_country(country)
  puts "Total Cases:".colorize(:magenta) + " #{country.total_cases}".colorize(:yellow)
  puts "New Cases:".colorize(:magenta) + " #{country.new_cases}".colorize(:yellow)
  puts "Total Deaths:".colorize(:magenta) + " #{country.total_deaths}".colorize(:yellow)
  puts "Total Recovered:".colorize(:magenta) + " #{country.total_recovered}".colorize(:yellow)
  puts "Active Cases:".colorize(:magenta) + " #{country.active_cases}".colorize(:yellow)
  puts "Total Tests:".colorize(:magenta) + " #{country.total_tests}".colorize(:yellow)
  puts "Population:".colorize(:magenta) + " #{country.population}".colorize(:yellow)
  puts "Continent:".colorize(:magenta) + " #{country.continent}".colorize(:yellow)
  puts "Deaths per Million:".colorize(:magenta) + " #{country.deaths_per_mil}".colorize(:yellow)
  puts "Number of Serious/Critical Cases:".colorize(:magenta) + " #{country.serious_critical}".colorize(:yellow)
  puts "Tests per Million:".colorize(:magenta) + " #{country.tests_per_mil}".colorize(:yellow)
  puts "Total Cases per Million:".colorize(:magenta) + " #{country.total_cases_per_mil}".colorize(:yellow)
end

#fatality_ratesObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/covidstats/cli.rb', line 153

def fatality_rates
  arr_fatal_rates = []
  Covidstats::Country.all.each {|country| arr_fatal_rates << country.deaths_per_mil}
  top_10_fatal_rates = arr_fatal_rates.sort.reverse[0,10]
  
  top_10_fatal_rates.each do |fatal_rate|  
    Covidstats::Country.all.each do |country|
      if country.deaths_per_mil == fatal_rate
        puts "#{country.name}:".colorize(:green) + " #{country.deaths_per_mil}".colorize(:yellow)
      end
    end
  end
  ask_for_choices
end

#get_all_continentsObject



13
14
15
16
17
# File 'lib/covidstats/cli.rb', line 13

def get_all_continents
  continents_array = Covidstats::Continents.continent_reports
  array = Covidstats::Continents.merge_hash(continents_array)
  Covidstats::Continents.create_from_collection(array)
end

#get_all_countriesObject



8
9
10
11
# File 'lib/covidstats/cli.rb', line 8

def get_all_countries
  countries_array = Covidstats::API.get_reports
  Covidstats::Country.create_from_collection(countries_array)
end

#highest_casesObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/covidstats/cli.rb', line 123

def highest_cases
  arr_total_cases = []
  Covidstats::Country.all.each {|country| arr_total_cases << country.total_cases}
  top_10_cases = arr_total_cases.sort.reverse[0,10]
  
  top_10_cases.each do |total_case|
    Covidstats::Country.all.each do |country|
      if country.total_cases == total_case
        puts "#{country.name}:".colorize(:red) + " #{country.total_cases}"
      end
    end
  end
  ask_for_choices
end

#list_of_actionsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/covidstats/cli.rb', line 50

def list_of_actions
  prompt = TTY::Prompt.new
  choice = prompt.select("Please select what you would like to do:".colorize(:light_green)) do |prompt|
    prompt.choice "stats by country"
    prompt.choice "stats by continent"
    prompt.choice "Top 10 countries with highest cases"
    prompt.choice "Top 10 countries with highest testing rates by tests per million"
    prompt.choice "Top 10 countries with highest fatality rates by deaths per million"
  end
 country_select if choice == "stats by country"
 continent_select if choice == "stats by continent"
 highest_cases if choice == "Top 10 countries with highest cases"
 testing_rates if choice == "Top 10 countries with highest testing rates by tests per million"
 fatality_rates if choice == "Top 10 countries with highest fatality rates by deaths per million"
end

#testing_ratesObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/covidstats/cli.rb', line 138

def testing_rates
  arr_test_rates = []
  Covidstats::Country.all.each {|country| arr_test_rates << country.tests_per_mil}
  top_10_test_rates = arr_test_rates.sort.reverse[0,10]
  
  top_10_test_rates.each do |test_rate|
    Covidstats::Country.all.each do |country|
      if country.tests_per_mil == test_rate
        puts "#{country.name}:".colorize(:blue) + " #{country.tests_per_mil}"
      end
    end
  end
  ask_for_choices
end

#world_statsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/covidstats/cli.rb', line 29

def world_stats
  puts "To begin, would you like to see the world statistics for today? (y/n)".colorize(:light_green)
  input = gets.strip
  if input == "y"
    puts "Here are the World Statistics:".colorize(:light_green)
    world = Covidstats::API.get_reports[0]
    puts "Total Cases:".colorize(:red) + " #{world["TotalCases"]}".colorize(:yellow)
    puts "New Cases:".colorize(:red) + " #{world["NewCases"]}".colorize(:yellow)  
    puts "Total Deaths:".colorize(:red) + " #{world["TotalDeaths"]}".colorize(:yellow)
    puts "New Deaths:".colorize(:red) + " #{world["NewDeaths"]}".colorize(:yellow)
    puts "TotalRecovered:".colorize(:red) + " #{world["TotalRecovered"]}".colorize(:yellow)
    puts "Active Cases:".colorize(:red) + " #{world["ActiveCases"]}".colorize(:yellow)
    puts "Deaths per Million:".colorize(:red) + " #{world["Deaths_1M_pop"]}".colorize(:yellow)
    puts "Number of Serious/Critical Cases:".colorize(:red) + " #{world["Serious_Critical"]}".colorize(:yellow)
    puts "Total Cases per Million:".colorize(:red) + " #{world["TotCases_1M_Pop"]}".colorize(:yellow)
    ask_for_choices
  else
    list_of_actions
  end
end