You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.8 KiB

#!/usr/bin/env ruby
require 'uri'
require 'net/http'
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 = URI(ENV['CPI_SERIES_URL'] || DEFAULT_CPI_SERIES_URL)
end
def cpi
resp = fetch_raw_response
return nil unless resp['Results']['series']
resp['Results']['series'].first['data'].first['value']
end
private
def fetch_raw_response
JSON.parse(Net::HTTP.get_response(cpi_series_url).body)
end
end
if $PROGRAM_NAME == __FILE__
puts CPIFetcher.new.cpi
exit 0
end
__END__
{
"status": "REQUEST_SUCCEEDED",
"responseTime": 36,
"message": [],
"Results": {
"series": [
{
"seriesID": "CUUSA210SA0",
"data": [
{
"year": "2015",
"period": "S01",
"periodName": "1st Half",
"value": "220.381",
"footnotes": [
{}
]
},
{
"year": "2014",
"period": "S02",
"periodName": "2nd Half",
"value": "220.891",
"footnotes": [
{}
]
},
{
"year": "2014",
"period": "S01",
"periodName": "1st Half",
"value": "220.352",
"footnotes": [
{}
]
},
{
"year": "2013",
"period": "S02",
"periodName": "2nd Half",
"value": "217.983",
"footnotes": [
{}
]
},
{
"year": "2013",
"period": "S01",
"periodName": "1st Half",
"value": "216.941",
"footnotes": [
{}
]
}
]
}
]
}
}