24 lines
557 B
Ruby
24 lines
557 B
Ruby
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
|
|
"cpi\n#{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
|