Class: Kovid::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/kovid/request.rb

Constant Summary collapse

COUNTRIES_PATH =
UriBuilder.new('/v2/countries').url
STATES_URL =
UriBuilder.new('/v2/states').url
JHUCSSE_URL =
UriBuilder.new('/v2/jhucsse').url
HISTORICAL_URL =
UriBuilder.new('/v2/historical').url
HISTORICAL_US_URL =
UriBuilder.new('/v2/historical/usacounties').url
SERVER_DOWN =
'Server overwhelmed. Please try again in a moment.'
EU_ISOS =
%w[AT BE BG CY CZ DE DK EE ES FI FR GR HR HU IE IT LT \
LU LV MT NL PL PT RO SE SI SK].freeze
EUROPE_ISOS =
EU_ISOS + %w[GB IS NO CH MC AD SM VA BA RS ME MK AL \
BY UA RU MD].freeze
AFRICA_ISOS =
%w[DZ AO BJ BW BF BI CM CV CF TD KM CD CG CI DJ EG \
GQ ER SZ ET GA GM GH GN GW KE LS LR LY MG MW ML \
MR MU MA MZ NA NE NG RW ST SN SC SL SO ZA SS SD \
TZ TG TN UG ZM ZW EH].freeze
SOUTH_AMERICA_ISOS =
%w[AR BO BV BR CL CO EC FK GF GY PY PE GS SR \
UY VE].freeze
ASIA_ISOS =
%w[AE AF AM AZ BD BH BN BT CC CN CX GE HK ID IL IN \
IQ IR JO JP KG KH KP KR KW KZ LA LB LK MM MN MO \
MY NP OM PH PK PS QA SA SG SY TH TJ TL TM TR TW \
UZ VN YE].freeze

Class Method Summary collapse

Class Method Details

.africa_aggregateObject



51
52
53
54
55
56
57
# 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_statesObject



133
134
135
136
137
138
# 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_aggregateObject



67
68
69
70
71
72
73
# 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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



140
141
142
143
144
145
# 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) ⇒ Object



147
148
149
150
151
152
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



225
226
227
# File 'lib/kovid/request.rb', line 225

def capitalize_words(string)
  string.split.map(&:capitalize).join(' ')
end

.casesObject



154
155
156
157
158
159
160
161
162
# 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_aggregateObject



35
36
37
38
39
40
41
# 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_aggregateObject



43
44
45
46
47
48
49
# 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) ⇒ Object



202
203
204
205
206
207
208
209
210
# 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) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# 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) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 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) ⇒ Object



99
100
101
102
103
104
105
106
# 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) ⇒ Object



108
109
110
111
112
# File 'lib/kovid/request.rb', line 108

def provinces(names)
  array = fetch_provinces(names)

  Kovid::Tablelize.compare_provinces(array)
end

.south_america_aggregateObject



59
60
61
62
63
64
65
# 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) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# 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) ⇒ Object



126
127
128
129
130
131
# 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) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
# 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