do let(:api_client) { described_class.new } let(:headers) do { 'Content-Type': 'application/json;charset=UTF-8' } end describe '#get_resource' do subject(:api_request) { api_client.get_resource } it do expect { api_request } .to request_to(:get, 'https://example.com/common/path/to/ .with(headers: headers, query: { key: 'value' }) end end ExampleApiClient #get_resource is expected to request to "GET https://example.com/common/pat
class ExampleApiClient < MyApiClient::Base endpoint 'https://example.com/common' error_handling status_code: 404, raise: MyErrors::NotFound # GET https://example.com/common/path/to/resource?key=value def get_resource query = { key: 'value' } get 'path/to/resource', headers: headers, query: query end # ... end api_client = ExampleApiClient.new begin response = api_client.get_resource rescue MyErrors::NotFound puts 'The resource was not found.' end
do let(:api_client) { described_class.new } let(:headers) do { 'Content-Type': 'application/json;charset=UTF-8' } end describe '#get_resource' do subject(:api_request) { api_client.get_resource } context 'when received status code is 404' do it do expect { api_request } .to be_handled_as_an_error(MyErrors::NotFound) .when_receive(status_code: 404) end ExampleApiClient #get_resource when received status code is 404 is expected to be handled as MyErrors::NotFound
do let(:api_client) { described_class.new } let(:headers) do { 'Content-Type': 'application/json;charset=UTF-8' } end describe '#get_resource' do subject(:api_request) { api_client.get_resource } context 'when received error code is 10' do let(:response_body) do { error: { code: 10, message: "You requested error code: 10" ExampleApiClient #get_resource when received error code is 10 is expected to be handled as MyErrors::ErrorCode10