Skip to content

Commit

Permalink
Expose headers as attr on response and on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Sep 21, 2020
1 parent 3568b59 commit 0dac76e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/pco/api/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/pco/api/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/pco/api/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 0dac76e

Please sign in to comment.