kovid 0.6.10 → 0.6.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 490171e0140b632b2cb19c40e781194aa02fb77cd5b2dd97026d761ab6a1820e
4
- data.tar.gz: e317576c9f36bd7ba3f138e03548e6115d789182c97bcf0b2db7018f482b0663
3
+ metadata.gz: d6cedbd1aa526ce6960c995100e849b0b9999376745df43c8943544792745c9c
4
+ data.tar.gz: 04547efd43c6c64d85861ed0e6d783fb4e89e03afd022167973e99812a8482ba
5
5
  SHA512:
6
- metadata.gz: 6d51a7f12226e122b242f341b9e3d3ee55b5bd90132f0d78bdae5f0730f67166fb099a878c8e591c9f7d04eafebbb7ecb184394a05cd8baf12ee8b9562962449
7
- data.tar.gz: f0f310b2f6441f6c69662bcf9b6bd3287983b9d07173f60b119b719d0595ab804041415f962fffb4f7c6fff12bd847e7e31ce0a2109f7789695b5c6629533aae
6
+ metadata.gz: 24c2ec47c8240fbdb7d1f7c5bcc8989d6eaa9f0fb3648decd612004cec192d2740d9308364ee7cd638a0b8591920f34989cfff7743df24bfe8c505f983306998
7
+ data.tar.gz: e58d5269006cbca2feb2ebdfd88522920162d82cede3fcd3c585f7d1c9163af4cc38f2f76aa78931565ce3718fc0c15825d1eaf7e14e415bfd2d292e4adb5ab5
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kovid (0.6.9)
4
+ kovid (0.6.14)
5
5
  carmen (~> 1.1.3)
6
6
  rainbow (~> 3.0)
7
7
  terminal-table (~> 1.8)
@@ -11,12 +11,12 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- activesupport (6.0.2.2)
14
+ activesupport (6.0.3.1)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
16
  i18n (>= 0.7, < 2)
17
17
  minitest (~> 5.1)
18
18
  tzinfo (~> 1.1)
19
- zeitwerk (~> 2.2)
19
+ zeitwerk (~> 2.2, >= 2.2.2)
20
20
  carmen (1.1.3)
21
21
  activesupport (>= 3.0.0)
22
22
  concurrent-ruby (1.1.6)
@@ -24,25 +24,25 @@ GEM
24
24
  docile (1.3.2)
25
25
  ethon (0.12.0)
26
26
  ffi (>= 1.3.0)
27
- ffi (1.12.2)
28
- i18n (1.8.2)
27
+ ffi (1.13.0)
28
+ i18n (1.8.3)
29
29
  concurrent-ruby (~> 1.0)
30
- minitest (5.14.0)
30
+ minitest (5.14.1)
31
31
  rainbow (3.0.0)
32
32
  rake (12.3.3)
33
33
  rspec (3.9.0)
34
34
  rspec-core (~> 3.9.0)
35
35
  rspec-expectations (~> 3.9.0)
36
36
  rspec-mocks (~> 3.9.0)
37
- rspec-core (3.9.1)
38
- rspec-support (~> 3.9.1)
39
- rspec-expectations (3.9.1)
37
+ rspec-core (3.9.2)
38
+ rspec-support (~> 3.9.3)
39
+ rspec-expectations (3.9.2)
40
40
  diff-lcs (>= 1.2.0, < 2.0)
41
41
  rspec-support (~> 3.9.0)
42
42
  rspec-mocks (3.9.1)
43
43
  diff-lcs (>= 1.2.0, < 2.0)
44
44
  rspec-support (~> 3.9.0)
45
- rspec-support (3.9.2)
45
+ rspec-support (3.9.3)
46
46
  simplecov (0.18.5)
47
47
  docile (~> 1.1)
48
48
  simplecov-html (~> 0.11)
@@ -51,7 +51,7 @@ GEM
51
51
  unicode-display_width (~> 1.1, >= 1.1.1)
52
52
  thor (1.0.1)
53
53
  thread_safe (0.3.6)
54
- typhoeus (1.3.1)
54
+ typhoeus (1.4.0)
55
55
  ethon (>= 0.9.0)
56
56
  tzinfo (1.2.7)
57
57
  thread_safe (~> 0.1)
data/README.md CHANGED
@@ -76,6 +76,12 @@ ___
76
76
  * `kovid history COUNTRY N` (history in the last N days).
77
77
  * `kovid history STATE --usa`
78
78
  ___
79
+ 😷 **Top N (by cases/deaths for countries and US States)**
80
+ * `kovid top N` (top N countries in number of cases).
81
+ * `kovid top N -d` OR `kovid top N --deaths` (top N countries in number of deaths).
82
+ * `kovid top N --states` (top N US states in number of cases).
83
+ * `kovid top N --states -d` (top N countries in number of deaths).
84
+ ___
79
85
 
80
86
  **NOTE:** If you find it irritating to have to type `kovid state STATE`, `covid state STATE` works as well.
81
87
 
@@ -139,6 +145,32 @@ To check for total figures:
139
145
 
140
146
  ![kovid](https://i.gyazo.com/e01f4769a2b9e31ce50cec212e55810c.png "Covid data.")
141
147
 
148
+ To fetch top 5 countries in number of cases or deaths:
149
+
150
+ `kovid top`
151
+
152
+ ![kovid](https://i.gyazo.com/79443079a6c834094fc21c90dd02b78c.png "Covid data.")
153
+
154
+ `kovid top --deaths` OR `kovid top -d`
155
+
156
+ ![kovid](https://i.gyazo.com/8136a7acc2cb67d1621b3db0df822cd5.png "Covid data.")
157
+
158
+ It is also possible to fetch top US states in number of cases or deaths:
159
+
160
+ `kovid top --states`
161
+
162
+ ![kovid](https://i.gyazo.com/7ee5a1e6affdec838783183024c4604d.png "Covid data.")
163
+
164
+ `kovid top --states --deaths` OR `kovid top --states -d`
165
+
166
+ ![kovid](https://i.gyazo.com/2c3cb7e1218deff44c9d440dab93a3b1.png "Covid data.")
167
+
168
+ To fetch more number of countries or US states you can pass N. eg:
169
+
170
+ `kovid top 10`
171
+
172
+ ![kovid](https://i.gyazo.com/64663ff25c1ff61701e84871948640f4.png "Covid data.")
173
+
142
174
  ## 👩🏾‍🔬 Experimental Feature
143
175
 
144
176
  `kovid histogram italy 3.20` tries to build a histogram on number of cases. In the example here `3.20` is date in the format of `M.YY`. If you can please play with it and report issues on how this could be improved.
@@ -68,15 +68,19 @@ module Kovid
68
68
  Kovid::Request.cases
69
69
  end
70
70
 
71
- def history(country, days=30)
71
+ def history(country, days = 30)
72
72
  Kovid::Request.history(country, days)
73
73
  end
74
74
 
75
- def history_us_state(state, days=30)
75
+ def history_us_state(state, days = 30)
76
76
  Kovid::Request.history_us_state(state, days)
77
77
  end
78
78
 
79
79
  def histogram(country, date)
80
80
  Kovid::Request.histogram(country, date)
81
81
  end
82
+
83
+ def top(count, options = { location: :countries, incident: :cases })
84
+ Kovid::Request.top(count, options)
85
+ end
82
86
  end
@@ -78,8 +78,8 @@ module Kovid
78
78
  desc 'history COUNTRY or history COUNTRY N or' \
79
79
  'history STATE --usa',
80
80
  'Return history of incidents of COUNTRY|STATE (in the last N days)'
81
- option :usa, :type => :boolean
82
- def history(location, days=30)
81
+ option :usa, type: :boolean
82
+ def history(location, days = 30)
83
83
  if options[:usa]
84
84
  puts Kovid.history_us_state(location, days)
85
85
  else
@@ -133,6 +133,20 @@ module Kovid
133
133
  end
134
134
  end
135
135
 
136
+ desc 'top N',
137
+ 'Returns top N countries or states in an incident (number of cases or
138
+ deaths).'
139
+ method_option :countries
140
+ method_option :states
141
+ method_option :cases, aliases: '-c'
142
+ method_option :deaths, aliases: '-d'
143
+ def top(count = 5)
144
+ count = count.to_i
145
+ count = 5 if count.zero?
146
+ puts Kovid.top(count, prepare_top_params(options))
147
+ data_source
148
+ end
149
+
136
150
  private
137
151
 
138
152
  def fetch_country_stats(country)
@@ -147,10 +161,26 @@ module Kovid
147
161
  source = <<~TEXT
148
162
  #{Time.now}
149
163
  Sources:
164
+ * worldometers.info/coronavirus
150
165
  * Johns Hopkins University
151
- * https://www.worldometers.info/coronavirus
152
166
  TEXT
153
167
  puts source
154
168
  end
169
+
170
+ def prepare_top_params(options)
171
+ params = {
172
+ location: :countries,
173
+ incident: :cases
174
+ }
175
+
176
+ if !options[:states].nil? && options[:countries].nil?
177
+ params[:location] = :states
178
+ end
179
+
180
+ if !options[:deaths].nil? && options[:cases].nil?
181
+ params[:incident] = :deaths
182
+ end
183
+ params
184
+ end
155
185
  end
156
186
  end
@@ -13,7 +13,9 @@ module Kovid
13
13
  'Cases Today'.paint_white,
14
14
  'Deaths'.paint_red,
15
15
  'Deaths Today'.paint_red,
16
- 'Recovered'.paint_green
16
+ 'Active'.paint_yellow,
17
+ 'Recovered'.paint_green,
18
+ 'Tests'.paint_white
17
19
  ].freeze
18
20
 
19
21
  DATE_CASES_DEATHS_RECOVERED = [
@@ -42,11 +44,13 @@ module Kovid
42
44
  COMPARE_COUNTRY_TABLE_FULL = [
43
45
  'Country'.paint_white,
44
46
  'Cases'.paint_white,
45
- 'Deaths'.paint_red,
46
- 'Recovered'.paint_green,
47
47
  'Cases Today'.paint_white,
48
+ 'Deaths'.paint_red,
48
49
  'Deaths Today'.paint_red,
50
+ 'Active'.paint_yellow,
51
+ 'Recovered'.paint_green,
49
52
  'Critical'.paint_yellow,
53
+ 'Tests'.paint_white,
50
54
  'Cases/Million'.paint_white
51
55
  ].freeze
52
56
 
@@ -56,16 +60,20 @@ module Kovid
56
60
  'Cases Today'.paint_white,
57
61
  'Deaths'.paint_red,
58
62
  'Deaths Today'.paint_red,
59
- 'Recovered'.paint_green
63
+ 'Active'.paint_yellow,
64
+ 'Recovered'.paint_green,
65
+ 'Tests'.paint_white
60
66
  ].freeze
61
67
 
62
68
  FULL_COUNTRY_TABLE_HEADINGS = [
63
69
  'Cases'.paint_white,
64
- 'Deaths'.paint_red,
65
- 'Recovered'.paint_green,
66
70
  'Cases Today'.paint_white,
71
+ 'Deaths'.paint_red,
67
72
  'Deaths Today'.paint_red,
73
+ 'Active'.paint_yellow,
74
+ 'Recovered'.paint_green,
68
75
  'Critical'.paint_yellow,
76
+ 'Tests'.paint_white,
69
77
  'Cases/Million'.paint_white
70
78
  ].freeze
71
79
 
@@ -100,18 +108,16 @@ module Kovid
100
108
  'Recovered'.paint_green
101
109
  ].freeze
102
110
 
103
- FOOTER_LINE_THREE_COLUMNS = [
104
- '------------',
105
- '------------',
111
+ FOOTER_LINE_COLUMN = [
106
112
  '------------'
107
113
  ].freeze
108
114
 
109
- FOOTER_LINE_FOUR_COLUMNS = [
110
- '------------',
111
- '------------',
112
- '------------',
113
- '------------'
114
- ].freeze
115
+ FOOTER_LINE_THREE_COLUMNS = FOOTER_LINE_COLUMN * 3
116
+
117
+ FOOTER_LINE_FOUR_COLUMNS = FOOTER_LINE_COLUMN * 4
118
+
119
+ # Add footer if rows exceed this number
120
+ ADD_FOOTER_SIZE = 10
115
121
 
116
122
  COUNTRY_LETTERS = 'A'.upto('Z').with_index(127_462).to_h.freeze
117
123
 
@@ -25,12 +25,6 @@ module Kovid
25
25
  num.to_i.positive? ? "+#{comma_delimit(num)}" : comma_delimit(num).to_s
26
26
  end
27
27
 
28
- def format_country_history_numbers(load)
29
- load['timeline'].values.map(&:values).transpose.each do |data|
30
- data.map! { |number| Kovid.comma_delimit(number) }
31
- end
32
- end
33
-
34
28
  def lookup_us_state(state)
35
29
  us = Carmen::Country.coded('USA')
36
30
  lookup = us.subregions.coded(state) || us.subregions.named(state)
@@ -5,42 +5,32 @@ module Kovid
5
5
  include Constants
6
6
  include AsciiCharts
7
7
 
8
- def history(location, days)
9
- rows = []
8
+ def history(data, days)
9
+ rows = history_rows(data, days)
10
10
 
11
- stats = Kovid.format_country_history_numbers(location).last(days.to_i)
12
- dates = location['timeline']['cases'].keys.last(days.to_i)
13
-
14
- if days.to_i >= 30
15
- stats = stats.reject { |stat| stat[0].to_i.zero? && stat[1].to_i.zero? }
16
- dates = dates.last(stats.count)
11
+ if rows.size > ADD_FOOTER_SIZE
12
+ rows << FOOTER_LINE_FOUR_COLUMNS
13
+ rows << DATE_CASES_DEATHS_RECOVERED
17
14
  end
18
15
 
19
- stats.each_with_index do |val, index|
20
- val.unshift(Kovid.dateman(dates[index]))
21
- end.each do |row|
22
- rows << row
23
- end
16
+ Terminal::Table.new(
17
+ title: data['country'].try(:upcase),
18
+ headings: DATE_CASES_DEATHS_RECOVERED,
19
+ rows: rows
20
+ )
21
+ end
24
22
 
25
- # Title and Column format if Country or US State
26
- if location['country']
27
- title = location['country'].try(:upcase)
28
- col_format = DATE_CASES_DEATHS_RECOVERED
29
- footer = FOOTER_LINE_FOUR_COLUMNS
30
- else
31
- title = location['state'].try(:upcase)
32
- col_format = DATE_CASES_DEATHS
33
- footer = FOOTER_LINE_THREE_COLUMNS
34
- end
23
+ def history_us_state(data, days)
24
+ rows = history_rows(data, days)
35
25
 
36
- if stats.size > 10
37
- rows << footer
38
- rows << col_format
26
+ if rows.size > ADD_FOOTER_SIZE
27
+ rows << FOOTER_LINE_THREE_COLUMNS
28
+ rows << DATE_CASES_DEATHS
39
29
  end
40
30
 
41
31
  Terminal::Table.new(
42
- title: title,
43
- headings: col_format,
32
+ title: data['state'].try(:upcase),
33
+ headings: DATE_CASES_DEATHS,
44
34
  rows: rows
45
35
  )
46
36
  end
@@ -106,5 +96,20 @@ module Kovid
106
96
  AsciiCharts::Cartesian.new(data, bar: true, hide_zero: true).draw
107
97
  end
108
98
  end
99
+
100
+ private
101
+
102
+ def history_rows(data, days)
103
+ data['timeline']['cases'].map do |date, cases|
104
+ formatted_date = Kovid.dateman(date)
105
+ cases = Kovid.comma_delimit(cases)
106
+ deaths = Kovid.comma_delimit(data['timeline']['deaths'][date])
107
+ recovered = Kovid.comma_delimit(data['timeline']['recovered'][date]) if data['timeline'].has_key? 'recovered'
108
+
109
+ row = [formatted_date, cases, deaths]
110
+ row << recovered unless recovered.nil?
111
+ row
112
+ end.last(days.to_i)
113
+ end
109
114
  end
110
115
  end
@@ -8,28 +8,28 @@ require_relative 'uri_builder'
8
8
 
9
9
  module Kovid
10
10
  class Request
11
- COUNTRIES_PATH = UriBuilder.new('/v2/countries').url
12
- STATES_URL = UriBuilder.new('/v2/states').url
13
- JHUCSSE_URL = UriBuilder.new('/v2/jhucsse').url
14
- HISTORICAL_URL = UriBuilder.new('/v2/historical').url
15
- HISTORICAL_US_URL = UriBuilder.new('/v2/historical/usacounties').url
16
-
17
- SERVER_DOWN = 'Server overwhelmed. Please try again in a moment.'
18
-
19
- EU_ISOS = %w[AT BE BG CY CZ DE DK EE ES FI FR GR HR HU IE IT LT \
20
- LU LV MT NL PL PT RO SE SI SK].freeze
21
- EUROPE_ISOS = EU_ISOS + %w[GB IS NO CH MC AD SM VA BA RS ME MK AL \
22
- BY UA RU MD].freeze
23
- AFRICA_ISOS = %w[DZ AO BJ BW BF BI CM CV CF TD KM CD CG CI DJ EG \
24
- GQ ER SZ ET GA GM GH GN GW KE LS LR LY MG MW ML \
25
- MR MU MA MZ NA NE NG RW ST SN SC SL SO ZA SS SD \
26
- TZ TG TN UG ZM ZW EH].freeze
27
- SOUTH_AMERICA_ISOS = %w[AR BO BV BR CL CO EC FK GF GY PY PE GS SR \
28
- UY VE].freeze
29
- ASIA_ISOS = %w[AE AF AM AZ BD BH BN BT CC CN CX GE HK ID IL IN \
30
- IQ IR JO JP KG KH KP KR KW KZ LA LB LK MM MN MO \
31
- MY NP OM PH PK PS QA SA SG SY TH TJ TL TM TR TW \
32
- UZ VN YE].freeze
11
+ COUNTRIES_PATH = UriBuilder.new('/v2/countries').url
12
+ STATES_URL = UriBuilder.new('/v2/states').url
13
+ JHUCSSE_URL = UriBuilder.new('/v2/jhucsse').url
14
+ HISTORICAL_URL = UriBuilder.new('/v2/historical').url
15
+ HISTORICAL_US_URL = UriBuilder.new('/v2/historical/usacounties').url
16
+
17
+ SERVER_DOWN = 'Server overwhelmed. Please try again in a moment.'
18
+
19
+ EU_ISOS = %w[AT BE BG CY CZ DE DK EE ES FI FR GR HR HU IE IT LT \
20
+ LU LV MT NL PL PT RO SE SI SK].freeze
21
+ EUROPE_ISOS = EU_ISOS + %w[GB IS NO CH MC AD SM VA BA RS ME MK AL \
22
+ BY UA RU MD].freeze
23
+ AFRICA_ISOS = %w[DZ AO BJ BW BF BI CM CV CF TD KM CD CG CI DJ EG \
24
+ GQ ER SZ ET GA GM GH GN GW KE LS LR LY MG MW ML \
25
+ MR MU MA MZ NA NE NG RW ST SN SC SL SO ZA SS SD \
26
+ TZ TG TN UG ZM ZW EH].freeze
27
+ SOUTH_AMERICA_ISOS = %w[AR BO BV BR CL CO EC FK GF GY PY PE GS SR \
28
+ UY VE].freeze
29
+ ASIA_ISOS = %w[AE AF AM AZ BD BH BN BT CC CN CX GE HK ID IL IN \
30
+ IQ IR JO JP KG KH KP KR KW KZ LA LB LK MM MN MO \
31
+ MY NP OM PH PK PS QA SA SG SY TH TJ TL TM TR TW \
32
+ UZ VN YE].freeze
33
33
 
34
34
  class << self
35
35
  def eu_aggregate
@@ -162,17 +162,11 @@ module Kovid
162
162
  end
163
163
 
164
164
  def history(country, days)
165
- history_path = UriBuilder.new('/v2/historical').url
166
-
167
- response = JSON.parse(
168
- Typhoeus.get(
169
- history_path + "/#{country}", cache_ttl: 900
170
- ).response_body
171
- )
165
+ response = fetch_history(country)
172
166
 
173
167
  if response.key?('message')
174
168
  not_found(country) do |c|
175
- "Could not find cases for #{c}.\nIf searching United States, add --usa option"
169
+ "Could not find cases for #{c}.\nIf searching United States, add --usa option"
176
170
  end
177
171
  else
178
172
  Kovid::Tablelize.history(response, days)
@@ -182,16 +176,10 @@ module Kovid
182
176
  end
183
177
 
184
178
  def history_us_state(state, days)
185
- history_path = UriBuilder.new('/v2/historical/usacounties').url
186
- state = Kovid.lookup_us_state(state).downcase()
179
+ state = Kovid.lookup_us_state(state).downcase
180
+ response = fetch_us_history(state)
187
181
 
188
- response = JSON.parse(
189
- Typhoeus.get(
190
- history_path + "/#{state}", cache_ttl: 900
191
- ).response_body
192
- )
193
-
194
- if response.respond_to?(:key?) and response.key?('message')
182
+ if response.respond_to?(:key?) && response.key?('message')
195
183
  return not_found(state)
196
184
  end
197
185
 
@@ -201,28 +189,39 @@ module Kovid
201
189
  cases = usacounties_aggregator(response, 'cases')
202
190
  deaths = usacounties_aggregator(response, 'deaths')
203
191
 
204
- # normalize data so we can call Kovid::Tablelize.history on US State data
205
192
  response = {
206
- "state" => state,
207
- "timeline" => { "cases" => cases, "deaths" => deaths }
193
+ 'state' => state,
194
+ 'timeline' => { 'cases' => cases, 'deaths' => deaths }
208
195
  }
209
196
 
210
- Kovid::Tablelize.history(response, days)
197
+ Kovid::Tablelize.history_us_state(response, days)
211
198
  rescue JSON::ParserError
212
- puts SERVER_DOWN
199
+ puts SERVER_DOWN
213
200
  end
214
201
 
215
202
  def histogram(country, date)
216
- history_path = UriBuilder.new('/v2/historical').url
217
203
  response = JSON.parse(
218
204
  Typhoeus.get(
219
- history_path + "/#{country}", cache_ttl: 900
205
+ HISTORICAL_URL + "/#{country}", cache_ttl: 900
220
206
  ).response_body
221
207
  )
222
208
 
223
209
  Kovid::Tablelize.histogram(response, date)
224
210
  end
225
211
 
212
+ def top(count, options)
213
+ response = JSON.parse(
214
+ Typhoeus.get(
215
+ top_url(options[:location]) +
216
+ "?sort=#{options[:incident]}",
217
+ cache_ttl: 900
218
+ ).response_body
219
+ )
220
+
221
+ Kovid::Tablelize.top(response.first(count),
222
+ options.merge({ count: count }))
223
+ end
224
+
226
225
  def capitalize_words(string)
227
226
  string.split.map(&:capitalize).join(' ')
228
227
  end
@@ -233,11 +232,11 @@ module Kovid
233
232
  rows = []
234
233
  default_warning = "Wrong spelling/No reported cases on #{location.upcase}."
235
234
 
236
- if block_given?
237
- rows << [ yield(location) ]
238
- else
239
- rows << [ default_warning ]
240
- end
235
+ rows << if block_given?
236
+ [yield(location)]
237
+ else
238
+ [default_warning]
239
+ end
241
240
 
242
241
  Terminal::Table.new title: "You checked: #{location.upcase}", rows: rows
243
242
  end
@@ -299,6 +298,22 @@ module Kovid
299
298
  end.first
300
299
  end
301
300
 
301
+ def fetch_history(country)
302
+ JSON.parse(
303
+ Typhoeus.get(
304
+ HISTORICAL_URL + "/#{country}", cache_ttl: 900
305
+ ).response_body
306
+ )
307
+ end
308
+
309
+ def fetch_us_history(state)
310
+ JSON.parse(
311
+ Typhoeus.get(
312
+ HISTORICAL_US_URL + "/#{state}", cache_ttl: 900
313
+ ).response_body
314
+ )
315
+ end
316
+
302
317
  def aggregator(isos, meth)
303
318
  countries_array = JSON.parse(countries_request)
304
319
  country_array = countries_array.select do |hash|
@@ -312,9 +327,7 @@ module Kovid
312
327
  end
313
328
 
314
329
  def countries_request
315
- Typhoeus.get(
316
- UriBuilder.new('/v2/countries').url, cache_ttl: 900
317
- ).response_body
330
+ Typhoeus.get(COUNTRIES_PATH, cache_ttl: 900).response_body
318
331
  end
319
332
 
320
333
  def countries_aggregator(country_array)
@@ -328,15 +341,19 @@ module Kovid
328
341
  end.compact
329
342
  end
330
343
 
331
- def usacounties_aggregator(data, key=nil)
332
- data.inject({}) do |base,other|
333
- base.merge(other['timeline'][key]) do |k,l,r|
334
- l ||= 0
344
+ def usacounties_aggregator(data, key = nil)
345
+ data.inject({}) do |base, other|
346
+ base.merge(other['timeline'][key]) do |_k, l, r|
335
347
  l ||= 0
348
+ r ||= 0
336
349
  l + r
337
350
  end
338
351
  end.compact
339
352
  end
353
+
354
+ def top_url(location)
355
+ location == :countries ? COUNTRIES_PATH : STATES_URL
356
+ end
340
357
  end
341
358
  end
342
359
  end
@@ -39,7 +39,7 @@ module Kovid
39
39
  Terminal::Table.new(
40
40
  title: state['state'].upcase,
41
41
  headings: FULL_STATE_TABLE_HEADINGS,
42
- rows: [country_row(state)]
42
+ rows: [state_row(state)]
43
43
  )
44
44
  end
45
45
 
@@ -96,6 +96,22 @@ module Kovid
96
96
  )
97
97
  end
98
98
 
99
+ def top(data, options)
100
+ headings = top_heading(options)
101
+ rows = data.map { |location| top_row(location, options) }
102
+
103
+ if options[:count] > 10
104
+ rows << FOOTER_LINE_COLUMN * headings.count
105
+ rows << headings
106
+ end
107
+
108
+ Terminal::Table.new(
109
+ title: top_title(options),
110
+ headings: headings,
111
+ rows: rows
112
+ )
113
+ end
114
+
99
115
  private
100
116
 
101
117
  def country_title(data)
@@ -120,7 +136,6 @@ module Kovid
120
136
  ]
121
137
  end
122
138
 
123
- # Also works for state
124
139
  def country_row(data)
125
140
  [
126
141
  Kovid.comma_delimit(data['cases']),
@@ -128,6 +143,7 @@ module Kovid
128
143
  Kovid.comma_delimit(data['deaths']),
129
144
  Kovid.add_plus_sign(data['todayDeaths']),
130
145
  Kovid.comma_delimit(data['active']),
146
+ Kovid.comma_delimit(data['recovered']),
131
147
  Kovid.comma_delimit(data['tests'])
132
148
  ]
133
149
  end
@@ -135,15 +151,28 @@ module Kovid
135
151
  def full_country_row(data)
136
152
  [
137
153
  Kovid.comma_delimit(data['cases']),
138
- Kovid.comma_delimit(data['deaths']),
139
- Kovid.comma_delimit(data['recovered']),
140
154
  Kovid.add_plus_sign(data['todayCases']),
155
+ Kovid.comma_delimit(data['deaths']),
141
156
  Kovid.add_plus_sign(data['todayDeaths']),
157
+ Kovid.comma_delimit(data['active']),
158
+ Kovid.comma_delimit(data['recovered']),
142
159
  Kovid.comma_delimit(data['critical']),
160
+ Kovid.comma_delimit(data['tests']),
143
161
  Kovid.comma_delimit(data['casesPerOneMillion'])
144
162
  ]
145
163
  end
146
164
 
165
+ def state_row(data)
166
+ [
167
+ Kovid.comma_delimit(data['cases']),
168
+ Kovid.add_plus_sign(data['todayCases']),
169
+ Kovid.comma_delimit(data['deaths']),
170
+ Kovid.add_plus_sign(data['todayDeaths']),
171
+ Kovid.comma_delimit(data['active']),
172
+ Kovid.comma_delimit(data['tests'])
173
+ ]
174
+ end
175
+
147
176
  def province_row(data)
148
177
  [
149
178
  data['stats']['confirmed'],
@@ -223,6 +252,33 @@ module Kovid
223
252
  end
224
253
  table
225
254
  end
255
+
256
+ def top_row(data, options)
257
+ if options[:location] == :countries
258
+ return [
259
+ country_title(data),
260
+ full_country_row(data)
261
+ ].flatten
262
+ end
263
+
264
+ [
265
+ data['state'].upcase,
266
+ country_row(data)
267
+ ].flatten
268
+ end
269
+
270
+ def top_heading(options)
271
+ full_country_table = ['Country'.paint_white] + FULL_COUNTRY_TABLE_HEADINGS
272
+ full_state_table = ['State'.paint_white] + FULL_STATE_TABLE_HEADINGS
273
+
274
+ options[:location] == :countries ? full_country_table : full_state_table
275
+ end
276
+
277
+ def top_title(options)
278
+ incident = options[:incident].to_s
279
+ location = options[:location].to_s
280
+ "🌍 Top #{options[:count]} #{location} by #{incident}".upcase
281
+ end
226
282
  end
227
283
  end
228
284
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kovid
4
- VERSION = '0.6.10'
4
+ VERSION = '0.6.15'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kovid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.10
4
+ version: 0.6.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Hayford
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-02 00:00:00.000000000 Z
11
+ date: 2020-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carmen