-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.rb
130 lines (100 loc) · 2.66 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require 'net/http'
require 'json'
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/cross_origin'
require './config/environments'
require './models/total'
require './models/country'
require './models/news'
require './models/asset'
require './models/timeline'
get '/' do
content_type :json
@data = {
stats: Total.last,
news: News.last,
assets: Asset.last,
countries: Country.last,
timeline: Timeline.last
}.to_json
# @data.delete! '\\'
@data
end
set :bind, '0.0.0.0'
configure do
enable :cross_origin
end
before do
response.headers['Access-Control-Allow-Origin'] = '*'
# response.headers['Access-Control-Allow-Origin'] = 'http://example.com'
end
# routes...
options "*" do
response.headers["Allow"] = "GET"
response.headers["Access-Control-Allow-Origin"] = "*"
200
end
def get_total_covid_stats
response = Net::HTTP.get_response('coronavirus-19-api.herokuapp.com', '/all')
if response.code == '200'
@total = Total.last || Total.new
@total.data = response.body
@total.save
end
end
def get_country_covid_stats
response = Net::HTTP.get_response('coronavirus-19-api.herokuapp.com', '/countries')
if response.code == '200'
@country = Country.last || Country.new
@country.data = response.body
@country.save
end
end
def get_global_news
uri = URI('http://newsapi.org/v2/top-headlines?q=covid&language=en&apiKey=e6a54260a8e0419ebbfddaa48b5c5a1c')
response = Net::HTTP.get_response(uri)
if response.code == '200'
@news = News.last || News.new
@news.data = response.body
@news.save
end
end
def get_asset_data
@asset = Asset.last || Asset.new
get_spy_1m_data(@asset)
get_btc_1m(@asset)
@asset.save
end
def get_spy_1m_data(asset)
uri = URI('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=SPY&apikey=EZIRZ2T7WKWHB3P0')
response = Net::HTTP.get_response(uri)
if response.code == '200'
@asset.spy1m = response.body
end
end
def get_btc_1m(asset)
uri = URI('https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_MONTHLY&symbol=BTC&market=CNY&apikey=EZIRZ2T7WKWHB3P0')
response = Net::HTTP.get_response(uri)
if response.code == '200'
@asset.btc1m = response.body
end
end
def get_time_series_data
uri = URI('https://pomber.github.io/covid19/timeseries.json')
response = Net::HTTP.get_response(uri)
data = JSON.parse(response.body)
if response.code == '200'
@timeseries = Timeline.last || Timeline.new
@timeseries.data = data['US'].to_json # response.body
@timeseries.save
end
end
# __END__
get '/generate' do
get_total_covid_stats
get_country_covid_stats
get_global_news
get_asset_data
get_time_series_data
end