Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ end
StatusClient.status
```

> **Note:** If your API responds with an empty body (for example, a `204` or a `200` without a payload), do not declare a
> response `body` structure for that status. Either omit `body` entirely or set `body :default` so Purple::Client skips
> validation—otherwise the client will raise an error to alert you that the declared structure can never be satisfied.

### Handling empty responses

```ruby
class PingClient < Purple::Client
domain 'https://status.example.com'

path :ping do
# No `body` declaration, because the endpoint always returns HTTP 204 with no payload
response :no_content
root_method :ping
end
end

# Performs GET https://status.example.com/ping
# Returns a response object without validating a body schema
PingClient.ping
```


### Path with a dynamic parameter

```ruby
Expand Down
24 changes: 24 additions & 0 deletions spec/purple/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,30 @@ class Unipile::Linkedin::PurpleClient < Purple::Client
expect(result).to eq(:sent)
expect(headers['X-API-KEY']).to eq('secret')
end

context 'when API responds with an empty body' do
it 'raises a helpful error when the body is an empty string' do
response = instance_double(Faraday::Response,
status: 201,
body: '')
allow(connection).to receive(:post).and_return(response)

expect do
described_class.linkedin_invite(provider_id: 1, account_id: 2, message: 'hi', resource: resource)
end.to raise_error(/returns empty body, but it is defined in the client/)
end

it 'raises a helpful error when the body is nil' do
response = instance_double(Faraday::Response,
status: 201,
body: nil)
allow(connection).to receive(:post).and_return(response)

expect do
described_class.linkedin_invite(provider_id: 1, account_id: 2, message: 'hi', resource: resource)
end.to raise_error(/returns empty body, but it is defined in the client/)
end
end
end

describe '.get_user' do
Expand Down