cpi-feed/cpi_feed.rb
2015-08-27 00:51:56 -04:00

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