From 0dac76e8cc0439bc41d480f39cde0cc158ff845a Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Mon, 21 Sep 2020 14:02:21 -0500 Subject: [PATCH] Expose headers as attr on response and on errors --- lib/pco/api/endpoint.rb | 8 ++++++-- lib/pco/api/errors.rb | 3 ++- spec/pco/api/endpoint_spec.rb | 2 ++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/pco/api/endpoint.rb b/lib/pco/api/endpoint.rb index bf130ee..86053d9 100644 --- a/lib/pco/api/endpoint.rb +++ b/lib/pco/api/endpoint.rb @@ -5,6 +5,10 @@ module PCO module API URL = 'https://api.planningcenteronline.com' + class Response < Hash + attr_accessor :headers + end + class Endpoint attr_reader :url, :last_result @@ -69,8 +73,8 @@ def delete def _build_response(result) case result.status when 200..299 - res = result.body - res['headers'] = result.headers + res = Response[result.body] + res.headers = result.headers res when 400 fail Errors::BadRequest, result diff --git a/lib/pco/api/errors.rb b/lib/pco/api/errors.rb index 3d9b4b7..63402cf 100644 --- a/lib/pco/api/errors.rb +++ b/lib/pco/api/errors.rb @@ -4,11 +4,12 @@ module Errors class AuthRequiredError < StandardError; end class BaseError < StandardError - attr_reader :status, :detail + attr_reader :status, :detail, :headers def initialize(response) @status = response.status @detail = response.body + @headers = response.headers end def to_s diff --git a/spec/pco/api/endpoint_spec.rb b/spec/pco/api/endpoint_spec.rb index b70decb..7bc9ad7 100644 --- a/spec/pco/api/endpoint_spec.rb +++ b/spec/pco/api/endpoint_spec.rb @@ -50,6 +50,7 @@ it 'returns the result of making a GET request to the endpoint' do expect(@result).to be_a(Hash) expect(@result['data']).to eq(result) + expect(@result.headers).to eq('Content-Type' => 'application/vnd.api+json') end end @@ -76,6 +77,7 @@ end expect(error.status).to eq(404) expect(error.message).to eq('Resource Not Found') + expect(error.headers).to eq('Content-Type' => 'application/vnd.api+json') end end