Module: Kovid::Historians

Includes:
AsciiCharts, Constants
Included in:
Tablelize
Defined in:
lib/kovid/historians.rb

Constant Summary

Constants included from AsciiCharts

AsciiCharts::VERSION

Constants included from Constants

Constants::ADD_FOOTER_SIZE, Constants::CASES_DEATHS_RECOVERED, Constants::CASES_DEATHS_RECOVERED_CTODAY_DTODAY, Constants::COMPARE_COUNTRIES_TABLE_HEADINGS, Constants::COMPARE_COUNTRY_TABLE_FULL, Constants::COMPARE_PROVINCES_HEADINGS, Constants::COMPARE_STATES_HEADINGS, Constants::CONTINENTAL_AGGREGATE_HEADINGS, Constants::COUNTRY_LETTERS, Constants::DATE_CASES_DEATHS, Constants::DATE_CASES_DEATHS_RECOVERED, Constants::FOOTER_LINE_COLUMN, Constants::FOOTER_LINE_FOUR_COLUMNS, Constants::FOOTER_LINE_THREE_COLUMNS, Constants::FULL_COUNTRY_TABLE_HEADINGS, Constants::FULL_PROVINCE_TABLE_HEADINGS, Constants::FULL_STATE_TABLE_HEADINGS, Constants::RIGHT_ALIGN_COLUMNS, Constants::USA_ABBREVIATIONS

Instance Method Summary collapse

Instance Method Details

#histogram(country, date_string) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kovid/historians.rb', line 38

def histogram(country, date_string)
  @date = date_string.split('.')

  if @date.last.to_i != 20
    Kovid.info_table('Only 2020 histgrams are available.')
    return
  end

  # From dates where number of !cases.zero?
  country_cases = country['timeline']['cases']
  positive_cases_figures = country_cases.values.reject(&:zero?)
  dates = country_cases.reject { |_k, v| v.zero? }.keys
  data = []

  # TODO: Refactor
  # Returns array of days.to_i from the date param
  dates = dates.map do |date|
    date.split('/')
  end.select do |date|
    date.last == @date.last
  end.select do |date|
    date.first == @date.first
  end.map do |array|
    array[1]
  end.map(&:to_i).last(positive_cases_figures.count)

  # Arranges dates and figures in [x,y] for histogram
  # With x being day, y being number of cases
  if dates.empty?
    if @date.first.to_i > Time.now.month
      msgs = [
        'Seriously...??! 😏', 'Did you just check the future??',
        'You just checked the future Morgan.',
        'Knowing too much of your future is never a good thing.'
      ]

      Kovid.info_table(msgs.sample)
    else
      Kovid.info_table('Check your spelling/No infections for this month.')
    end

  else
    dates.each_with_index do |val, index|
      data << [val, positive_cases_figures[index]]
    end
    y_range = Kovid::AsciiCharts::Cartesian.new(
      data, bar: true, hide_zero: true
    ).y_range

    last_two_y = y_range.last 2
    y_interval = last_two_y.last - last_two_y.first

    scale("Scale on Y: #{y_interval}:#{(
      y_interval / last_two_y.last.to_f * positive_cases_figures.last
    ).round(2) / y_interval}")

    puts 'Experimental feature, please report issues.'

    AsciiCharts::Cartesian.new(data, bar: true, hide_zero: true).draw
  end
end

#history(data, days) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kovid/historians.rb', line 8

def history(data, days)
  rows = history_rows(data, days)

  if rows.size > ADD_FOOTER_SIZE
    rows << FOOTER_LINE_FOUR_COLUMNS
    rows << DATE_CASES_DEATHS_RECOVERED
  end

  Terminal::Table.new(
    title: data['country']&.upcase,
    headings: DATE_CASES_DEATHS_RECOVERED,
    rows: rows
  )
end

#history_us_state(data, days) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kovid/historians.rb', line 23

def history_us_state(data, days)
  rows = history_rows(data, days)

  if rows.size > ADD_FOOTER_SIZE
    rows << FOOTER_LINE_THREE_COLUMNS
    rows << DATE_CASES_DEATHS 
  end

  Terminal::Table.new(
    title: data['state']&.upcase,
    headings:  DATE_CASES_DEATHS,
    rows: rows
  )
end