class Covid19::Decorators::Chart

Constants

COLORS
DEFAULT_FILL
DIARY_TOTALS
FILLS
GENERAL_TOTALS
SIZE
TYPES

Public Class Methods

create_pie(data:, type:, colored: nil) click to toggle source
# File lib/covid19/decorators/chart.rb, line 27
def self.create_pie(data:, type:, colored: nil)
  pie_data = case type
             when TYPES[:all_continent_general_data]
               mount_all_continent_general_data(data)
             when TYPES[:continent_general_data]
               mount_general_data(data)
             when TYPES[:country_general_data]
               mount_general_data(data)
             when TYPES[:continent_diary_data]
               mount_diary_data(data)
             when TYPES[:country_diary_data]
               mount_diary_data(data)
             else
               raise ArgumentError.new('Error in chart plot!')
             end

  formatted_chart = colored ? add_color(pie_data) : add_fill(pie_data)
 
  TTY::Pie.new(data: formatted_chart, radius: SIZE)
end

Private Class Methods

add_color(pie_data) click to toggle source
# File lib/covid19/decorators/chart.rb, line 48
def self.add_color(pie_data)
  pie_data.each_with_index {|hash, index| hash.merge!({fill: DEFAULT_FILL, color: COLORS[index]})}
end
add_fill(pie_data) click to toggle source
# File lib/covid19/decorators/chart.rb, line 52
def self.add_fill(pie_data)
  pie_data.each_with_index {|hash, index| hash.merge!({fill: FILLS[index]})}
end
mount_all_continent_general_data(data) click to toggle source
# File lib/covid19/decorators/chart.rb, line 56
def self.mount_all_continent_general_data(data)
  data.map {|occurrency| {name: occurrency['continent'], value: occurrency['cases']}}
end
mount_diary_data(data) click to toggle source
# File lib/covid19/decorators/chart.rb, line 66
def self.mount_diary_data(data)
  data
    .select { |key, value| DIARY_TOTALS.include?(key) }
    .map { |occurrency| {name: occurrency.first, value: occurrency.last} }
end
mount_general_data(data) click to toggle source
# File lib/covid19/decorators/chart.rb, line 60
def self.mount_general_data(data)
  data
    .select { |key, value| GENERAL_TOTALS.include?(key) }
    .map { |occurrency| {name: occurrency.first, value: occurrency.last} }
end