Skip to content

Commit

Permalink
Merge pull request #7 from controlshift/raise_on_404
Browse files Browse the repository at this point in the history
Raise an error for responses with 404 status
  • Loading branch information
anero authored Sep 14, 2021
2 parents 2541f60 + e404f20 commit 910fe4e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/mobilize_america_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'mobilize_america_client/client/events'
require 'mobilize_america_client/client/organizations'
require 'mobilize_america_client/request'
require 'mobilize_america_client/errors'

module MobilizeAmericaClient
class Client
Expand Down
4 changes: 4 additions & 0 deletions lib/mobilize_america_client/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module MobilizeAmericaClient
class NotFoundError < StandardError
end
end
4 changes: 4 additions & 0 deletions lib/mobilize_america_client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def request(method:, path:, params: {}, body: {})
req.body = ::JSON.generate(body) unless body.empty?
end

if response.status == 404
raise MobilizeAmericaClient::NotFoundError
end

JSON.parse(response.body)
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/client/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
let(:events_url) { "#{base_url}/organizations/#{org_id}/events" }
let(:response) { {'data' => [{'id' => 1, 'description' => 'event 1'}, {'id' => 2, 'description' => 'event 2'}]} }

it 'should raise if response status is 404' do
stub_request(:get, events_url).with(headers: standard_headers).to_return(status: 404, body: {error: 'not found'}.to_json)

expect { subject.organization_events(organization_id: org_id) }.to raise_error MobilizeAmericaClient::NotFoundError
end

it 'should call the endpoint and return JSON' do
stub_request(:get, events_url).with(headers: standard_headers).to_return(body: response.to_json)
expect(subject.organization_events(organization_id: org_id)).to eq(response)
Expand Down

0 comments on commit 910fe4e

Please sign in to comment.