Extract bits into a separate file for offline generation
This commit is contained in:
parent
1f789f1674
commit
8f56f32d73
21
app.rb
Normal file
21
app.rb
Normal file
@ -0,0 +1,21 @@
|
||||
require 'sinatra/base'
|
||||
require_relative 'cpi_fetcher'
|
||||
|
||||
class App < Sinatra::Base
|
||||
get '/' do
|
||||
current_cpi = fetcher.cpi
|
||||
halt 502 unless current_cpi
|
||||
|
||||
expires 300, :public, :must_revalidate
|
||||
content_type :text
|
||||
"#{current_cpi}\n"
|
||||
end
|
||||
|
||||
run! if app_file == $PROGRAM_FILE
|
||||
|
||||
private
|
||||
|
||||
def fetcher
|
||||
@fetcher ||= CPIFetcher.new
|
||||
end
|
||||
end
|
23
cpi_feed.rb
23
cpi_feed.rb
@ -1,23 +0,0 @@
|
||||
require 'sinatra/base'
|
||||
require 'faraday'
|
||||
require 'json'
|
||||
|
||||
class CPIFeed < Sinatra::Base
|
||||
DEFAULT_CPI_SERIES_URL = 'http://api.bls.gov/publicAPI/v2/timeseries/data/CUUSA210SA0'
|
||||
|
||||
get '/' do
|
||||
resp = JSON.parse(Faraday.get(cpi_series_url).body)
|
||||
|
||||
halt 502 unless resp['Results']['series']
|
||||
|
||||
expires 300, :public, :must_revalidate
|
||||
content_type :csv
|
||||
"#{resp['Results']['series'].first['data'].first['value']}\n"
|
||||
end
|
||||
|
||||
def cpi_series_url
|
||||
ENV['CPI_SERIES_URL'] || DEFAULT_CPI_SERIES_URL
|
||||
end
|
||||
|
||||
run! if app_file == $PROGRAM_FILE
|
||||
end
|
24
cpi_fetcher.rb
Normal file
24
cpi_fetcher.rb
Normal file
@ -0,0 +1,24 @@
|
||||
require 'faraday'
|
||||
require 'json'
|
||||
|
||||
class CPIFetcher
|
||||
DEFAULT_CPI_SERIES_URL = 'http://api.bls.gov/publicAPI/v2/timeseries/data/CUUSA210SA0'
|
||||
attr_reader :cpi_series_url
|
||||
|
||||
def initialize
|
||||
@cpi_series_url = (ENV['CPI_SERIES_URL'] || DEFAULT_CPI_SERIES_URL)
|
||||
end
|
||||
|
||||
def cpi
|
||||
resp = JSON.parse(Faraday.get(cpi_series_url).body)
|
||||
|
||||
return nil unless resp['Results']['series']
|
||||
|
||||
resp['Results']['series'].first['data'].first['value']
|
||||
end
|
||||
end
|
||||
|
||||
if $PROGRAM_NAME == __FILE__
|
||||
puts CPIFetcher.new.cpi
|
||||
exit 0
|
||||
end
|
Loading…
Reference in New Issue
Block a user