Attempt to make an authenticated request to bls

???????????????????????????????????????????????
This commit is contained in:
Dan Buch 2016-08-30 11:17:20 -04:00
parent 2033546272
commit 4d33de8154
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC

View File

@ -6,10 +6,11 @@ require 'json'
class CPIFetcher class CPIFetcher
DEFAULT_CPI_SERIES_URL = 'http://api.bls.gov/publicAPI/v2/timeseries/data/CUUSA210SA0'.freeze DEFAULT_CPI_SERIES_URL = 'http://api.bls.gov/publicAPI/v2/timeseries/data/CUUSA210SA0'.freeze
attr_reader :cpi_series_url attr_reader :url, :bls_token
def initialize def initialize
@cpi_series_url = URI(ENV['CPI_SERIES_URL'] || DEFAULT_CPI_SERIES_URL) @url = URI(ENV['CPI_SERIES_URL'] || DEFAULT_CPI_SERIES_URL)
@bls_token = ENV.fetch('BLS_TOKEN')
end end
def cpi def cpi
@ -25,7 +26,16 @@ class CPIFetcher
private private
def fetch_raw_response def fetch_raw_response
JSON.parse(Net::HTTP.get_response(cpi_series_url).body) res = Net::HTTP.start(url.hostname, url.port) do |http|
http.request(build_request)
end
JSON.parse(res.body)
end
def build_request
req = Net::HTTP::Get.new(url)
req['Authorization'] = "token #{bls_token}"
req
end end
end end