class Covid19

To Follow

Constants

API_URL
VERSION

Public Class Methods

latest_stats_all() click to toggle source
# File lib/covid19.rb, line 14
def latest_stats_all
    return create_live_data_set
end
latest_stats_global() click to toggle source
# File lib/covid19.rb, line 18
def latest_stats_global
    return create_live_data_set['Global']
end
latest_stats_split_by_country() click to toggle source
# File lib/covid19.rb, line 22
def latest_stats_split_by_country
    return create_live_data_set['Countries']
end

Private Class Methods

create_live_data_set() click to toggle source
# File lib/covid19.rb, line 32
def create_live_data_set
    global = { 'todays_confirmed' => 0, 'todays_recovered' => 0, 'todays_deaths' => 0, 'total_confirmed' => 0, 'total_recovered' => 0, 'total_deaths' => 0, 'total_active' => 0 }
    country_list = []

    data = retrieve_api_data['data']

    data.each do |country|
        todays_data = get_hash_value(country, 'today', [])
        latest_data = get_hash_value(country, 'latest_data', [])
        calculated_data = get_hash_value(latest_data, 'calculated', [])
        timeline_data = get_hash_value(country, 'timeline', [])

        #
        # If we have timeline data then lets grab the most recent (current row)
        #
        current_data = if timeline_data.empty?
                           []
                       else
                           timeline_data.first
                       end

        timeline_confirmed       = get_hash_value(current_data, 'new_confirmed')
        timeline_recovered       = get_hash_value(current_data, 'new_recovered')
        timeline_deaths          = get_hash_value(current_data, 'new_deaths')
        timeline_total_confirmed = get_hash_value(current_data, 'confirmed')
        timeline_total_recovered = get_hash_value(current_data, 'recovered')
        timeline_total_deaths    = get_hash_value(current_data, 'deaths')
        timeline_total_active    = get_hash_value(current_data, 'active')

        #
        # Get todays data from the todays data set
        #
        todays_confirmed = get_hash_value(todays_data, 'confirmed')
        todays_recovered = get_hash_value(todays_data, 'recovered')
        todays_deaths    = get_hash_value(todays_data, 'deaths')

        #
        # Get the latest data from the latest dataset
        #
        total_confirmed = get_hash_value(latest_data, 'confirmed')
        total_recovered = get_hash_value(latest_data, 'recovered')
        total_deaths    = get_hash_value(latest_data, 'deaths')
        total_active    = total_confirmed - (total_deaths + total_recovered)

        #
        # Use the biggest value - as timeline is updated at a different rate
        #
        todays_confirmed = get_max(timeline_confirmed, todays_confirmed)
        todays_recovered = get_max(timeline_recovered, todays_recovered)
        todays_deaths    = get_max(timeline_deaths, todays_deaths)
        total_confirmed  = get_max(timeline_total_confirmed, total_confirmed)
        total_recovered  = get_max(timeline_total_recovered, total_recovered)
        total_deaths     = get_max(timeline_total_deaths, total_deaths)
        total_active     = get_max(timeline_total_active, total_active)

        # This normally means the country has stopped given updates on recoveries
        if get_hash_value(latest_data, 'recovered').zero?
            todays_recovered = 0
            total_recovered = 0
            total_active = 0
        end

        global['todays_confirmed'] += todays_confirmed
        global['todays_recovered'] += todays_recovered
        global['todays_deaths']    += todays_deaths
        global['total_confirmed']  += total_confirmed
        global['total_recovered']  += total_recovered
        global['total_deaths']     += total_deaths
        global['total_active']     += total_active

        row = {
            'Country'         => country['name'],
            'Population'      => get_hash_value(country, 'population', 'unknown'),
            'ConfirmedToday'  => todays_confirmed,
            'RecoveredToday'  => todays_recovered,
            'DeathsToday'     => todays_deaths,
            'ConfirmedTotal'  => total_confirmed,
            'RecoveredTotal'  => total_recovered,
            'DeathsTotal'     => total_deaths,
            'ActiveTotal'     => total_active,
            'DeathRate'       => get_hash_value(calculated_data, 'death_rate').to_f,
            'RecoveryRate'    => get_hash_value(calculated_data, 'recovery_rate').to_f,
            'CasesPerMillion' => get_hash_value(calculated_data, 'cases_per_million_population')
        }

        country_list << row
    end

    global = {
        'ConfirmedToday' => global['todays_confirmed'],
        'RecoveredToday' => global['todays_recovered'],
        'DeathsToday'    => global['todays_deaths'],
        'ConfirmedTotal' => global['total_confirmed'],
        'RecoveredTotal' => global['total_recovered'],
        'DeathsTotal'    => global['total_deaths'],
        'ActiveTotal'    => global['total_active']
    }

    results = { 'Global' => global, 'Countries' => country_list }

    return results
end
get_max(left, right) click to toggle source
# File lib/covid19.rb, line 28
def get_max(left, right)
    return (left <=> right) >= 0 ? left : right
end
retrieve_api_data() click to toggle source
# File lib/covid19.rb, line 135
def retrieve_api_data
    begin
        # encoded_url = URI.encode(API_URL)
        uri = URI.parse(API_URL)
        response = Net::HTTP.get_response(uri)
        return JSON.parse(response.body)
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
        raise StandardError.new(e.to_s)
    end
end