class Kovid::Request
Constants
- AFRICA_ISOS
- ASIA_ISOS
- COUNTRIES_PATH
- EUROPE_ISOS
- EU_ISOS
- HISTORICAL_URL
- HISTORICAL_US_URL
- JHUCSSE_URL
- SERVER_DOWN
- SOUTH_AMERICA_ISOS
- STATES_URL
Public Class Methods
africa_aggregate()
click to toggle source
# File lib/kovid/request.rb, line 51 def africa_aggregate africa_proc = proc do |data| Kovid::Tablelize.africa_aggregate(data) end aggregator(AFRICA_ISOS, africa_proc) end
all_us_states()
click to toggle source
# File lib/kovid/request.rb, line 133 def all_us_states state_data = fetch_state_data Kovid::Tablelize.compare_us_states(state_data) rescue JSON::ParserError puts SERVER_DOWN end
asia_aggregate()
click to toggle source
# File lib/kovid/request.rb, line 67 def asia_aggregate asia_proc = proc do |data| Kovid::Tablelize.asia_aggregate(data) end aggregator(ASIA_ISOS, asia_proc) end
by_country(country_name)
click to toggle source
# File lib/kovid/request.rb, line 75 def by_country(country_name) response = fetch_country(country_name) if response.key?('message') not_found(country_name) else Kovid::Tablelize.country_table(response) end rescue JSON::ParserError puts SERVER_DOWN end
by_country_comparison(list)
click to toggle source
# File lib/kovid/request.rb, line 140 def by_country_comparison(list) array = fetch_countries(list) Kovid::Tablelize.compare_countries_table(array) rescue JSON::ParserError puts SERVER_DOWN end
by_country_comparison_full(list)
click to toggle source
# File lib/kovid/request.rb, line 147 def by_country_comparison_full(list) array = fetch_countries(list) Kovid::Tablelize.compare_countries_table_full(array) rescue JSON::ParserError puts SERVER_DOWN end
by_country_full(country_name)
click to toggle source
# File lib/kovid/request.rb, line 87 def by_country_full(country_name) response = fetch_country(country_name) if response.key?('message') not_found(country_name) else Kovid::Tablelize.full_country_table(response) end rescue JSON::ParserError puts SERVER_DOWN end
capitalize_words(string)
click to toggle source
# File lib/kovid/request.rb, line 225 def capitalize_words(string) string.split.map(&:capitalize).join(' ') end
cases()
click to toggle source
# File lib/kovid/request.rb, line 154 def cases response = JSON.parse( Typhoeus.get(UriBuilder.new('/v2/all').url, cache_ttl: 900).response_body ) Kovid::Tablelize.cases(response) rescue JSON::ParserError puts SERVER_DOWN end
eu_aggregate()
click to toggle source
# File lib/kovid/request.rb, line 35 def eu_aggregate eu_proc = proc do |data| Kovid::Tablelize.eu_aggregate(data) end aggregator(EU_ISOS, eu_proc) end
europe_aggregate()
click to toggle source
# File lib/kovid/request.rb, line 43 def europe_aggregate europe_proc = proc do |data| Kovid::Tablelize.europe_aggregate(data) end aggregator(EUROPE_ISOS, europe_proc) end
histogram(country, date)
click to toggle source
# File lib/kovid/request.rb, line 202 def histogram(country, date) response = JSON.parse( Typhoeus.get( HISTORICAL_URL + "/#{country}", cache_ttl: 900 ).response_body ) Kovid::Tablelize.histogram(response, date) end
history(country, days)
click to toggle source
# File lib/kovid/request.rb, line 164 def history(country, days) response = fetch_history(country) if response.key?('message') not_found(country) do |c| "Could not find cases for #{c}.\nIf searching United States, add --usa option" end else Kovid::Tablelize.history(response, days) end rescue JSON::ParserError puts SERVER_DOWN end
history_us_state(state, days)
click to toggle source
# File lib/kovid/request.rb, line 178 def history_us_state(state, days) state = Kovid.lookup_us_state(state).downcase response = fetch_us_history(state) if response.respond_to?(:key?) && response.key?('message') return not_found(state) end # API Endpoint returns list of counties for given state, so # we aggreage cases for all counties # Note: no data for 'Recovered' cases = usacounties_aggregator(response, 'cases') deaths = usacounties_aggregator(response, 'deaths') response = { 'state' => state, 'timeline' => { 'cases' => cases, 'deaths' => deaths } } Kovid::Tablelize.history_us_state(response, days) rescue JSON::ParserError puts SERVER_DOWN end
province(province)
click to toggle source
# File lib/kovid/request.rb, line 99 def province(province) response = fetch_province(province) if response.nil? not_found(province) else Kovid::Tablelize.full_province_table(response) end end
provinces(names)
click to toggle source
# File lib/kovid/request.rb, line 108 def provinces(names) array = fetch_provinces(names) Kovid::Tablelize.compare_provinces(array) end
south_america_aggregate()
click to toggle source
# File lib/kovid/request.rb, line 59 def south_america_aggregate south_america_proc = proc do |data| Kovid::Tablelize.south_america_aggregate(data) end aggregator(SOUTH_AMERICA_ISOS, south_america_proc) end
state(state)
click to toggle source
# File lib/kovid/request.rb, line 114 def state(state) response = fetch_state(Kovid.lookup_us_state(state)) if response.nil? not_found(state) else Kovid::Tablelize.full_state_table(response) end rescue JSON::ParserError puts SERVER_DOWN end
states(states)
click to toggle source
# File lib/kovid/request.rb, line 126 def states(states) compared_states = fetch_compared_states(states) Kovid::Tablelize.compare_us_states(compared_states) rescue JSON::ParserError puts SERVER_DOWN end
top(count, options)
click to toggle source
# File lib/kovid/request.rb, line 212 def top(count, options) response = JSON.parse( Typhoeus.get( top_url(options[:location]) + "?sort=#{options[:incident]}", cache_ttl: 900 ).response_body ) Kovid::Tablelize.top(response.first(count), options.merge({ count: count })) end
Private Class Methods
aggregator(isos, meth)
click to toggle source
# File lib/kovid/request.rb, line 317 def aggregator(isos, meth) countries_array = JSON.parse(countries_request) country_array = countries_array.select do |hash| isos.include?(hash['countryInfo']['iso2']) end data = countries_aggregator(country_array) meth === data rescue JSON::ParserError puts SERVER_DOWN end
countries_aggregator(country_array)
click to toggle source
# File lib/kovid/request.rb, line 333 def countries_aggregator(country_array) country_array.inject do |base, other| base.merge(other) do |key, left, right| left ||= 0 right ||= 0 left + right unless %w[country countryInfo].include?(key) end end.compact end
countries_request()
click to toggle source
# File lib/kovid/request.rb, line 329 def countries_request Typhoeus.get(COUNTRIES_PATH, cache_ttl: 900).response_body end
fetch_compared_states(submitted_states)
click to toggle source
# File lib/kovid/request.rb, line 254 def fetch_compared_states(submitted_states) submitted_states.map! { |s| Kovid.lookup_us_state(s) } fetch_state_data.select do |state| submitted_states.include?(state['state']) end end
fetch_countries(list)
click to toggle source
# File lib/kovid/request.rb, line 244 def fetch_countries(list) list.map do |country| JSON.parse( Typhoeus.get( COUNTRIES_PATH + "/#{country}", cache_ttl: 900 ).response_body ) end.sort_by { |json| -json['cases'] } end
fetch_country(country_name)
click to toggle source
# File lib/kovid/request.rb, line 266 def fetch_country(country_name) # TODO: Match ISOs to full country names country_name = 'nl' if country_name == 'netherlands' country_url = COUNTRIES_PATH + "/#{country_name}" JSON.parse(Typhoeus.get(country_url, cache_ttl: 900).response_body) end
fetch_history(country)
click to toggle source
# File lib/kovid/request.rb, line 301 def fetch_history(country) JSON.parse( Typhoeus.get( HISTORICAL_URL + "/#{country}", cache_ttl: 900 ).response_body ) end
fetch_jhucsse()
click to toggle source
# File lib/kovid/request.rb, line 274 def fetch_jhucsse JSON.parse(Typhoeus.get(JHUCSSE_URL, cache_ttl: 900).response_body) end
fetch_province(province)
click to toggle source
# File lib/kovid/request.rb, line 278 def fetch_province(province) response = fetch_jhucsse response.select do |datum| datum['province'] == capitalize_words(province) end.first end
fetch_provinces(provinces)
click to toggle source
# File lib/kovid/request.rb, line 285 def fetch_provinces(provinces) provinces.map!(&method(:capitalize_words)) response = fetch_jhucsse response.select { |datum| provinces.include? datum['province'] } end
fetch_state(state)
click to toggle source
# File lib/kovid/request.rb, line 291 def fetch_state(state) states_array = JSON.parse( Typhoeus.get(STATES_URL, cache_ttl: 900).response_body ) states_array.select do |state_name| state_name['state'] == capitalize_words(state) end.first end
fetch_state_data()
click to toggle source
# File lib/kovid/request.rb, line 262 def fetch_state_data JSON.parse(Typhoeus.get(STATES_URL, cache_ttl: 900).response_body) end
fetch_us_history(state)
click to toggle source
# File lib/kovid/request.rb, line 309 def fetch_us_history(state) JSON.parse( Typhoeus.get( HISTORICAL_US_URL + "/#{state}", cache_ttl: 900 ).response_body ) end
not_found(location) { |location| ... }
click to toggle source
# File lib/kovid/request.rb, line 231 def not_found(location) rows = [] default_warning = "Wrong spelling/No reported cases on #{location.upcase}." rows << if block_given? [yield(location)] else [default_warning] end Terminal::Table.new title: "You checked: #{location.upcase}", rows: rows end
top_url(location)
click to toggle source
# File lib/kovid/request.rb, line 354 def top_url(location) location == :countries ? COUNTRIES_PATH : STATES_URL end
usacounties_aggregator(data, key = nil)
click to toggle source
# File lib/kovid/request.rb, line 344 def usacounties_aggregator(data, key = nil) data.inject({}) do |base, other| base.merge(other['timeline'][key]) do |_k, l, r| l ||= 0 r ||= 0 l + r end end.compact end