Skip to content

Commit 172b3ed

Browse files
authored
Merge pull request #8 from Springest/add_rate_limiting_error
Add TooManyRequests error for 429
2 parents ed1f70e + 0e1d86d commit 172b3ed

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/help_scout.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class HelpScout
55
class ValidationError < StandardError; end
66
class NotImplementedError < StandardError; end
77
class NotFoundError < StandardError; end
8+
class TooManyRequestsError < StandardError; end
89
class InternalServerError < StandardError; end
910

1011
# Status codes used by Help Scout, not all are implemented in this gem yet.
@@ -14,6 +15,7 @@ class InternalServerError < StandardError; end
1415
HTTP_NO_CONTENT = 204
1516
HTTP_BAD_REQUEST = 400
1617
HTTP_NOT_FOUND = 404
18+
HTTP_TOO_MANY_REQUESTS = 429
1719
HTTP_INTERNAL_SERVER_ERROR = 500
1820

1921
attr_accessor :last_response
@@ -206,6 +208,10 @@ def request(method, path, options)
206208
raise NotFoundError
207209
when HTTP_INTERNAL_SERVER_ERROR
208210
raise InternalServerError
211+
when HTTP_TOO_MANY_REQUESTS
212+
retry_after = last_response.headers["Retry-After"]
213+
error_message = "Rate limit of 200 RPM or 12 POST/PUT/DELETE requests per 5 seconds reached. Next request possible in #{retry_after} seconds."
214+
raise TooManyRequestsError, error_message
209215
else
210216
raise NotImplementedError, "Help Scout returned something that is not implemented by the help_scout gem yet: #{@last_response.code}: #{@last_response.parsed_response["message"] if @last_response.parsed_response}"
211217
end

spec/helpscout_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,20 @@
144144
expect(req).to have_been_requested
145145
end
146146
end
147+
148+
describe 'general rate limiting error' do
149+
it 'returns TooManyRequestsError' do
150+
url = 'https://api.helpscout.net/v1/conversations/1337.json'
151+
stub_request(:get, url).
152+
to_return(
153+
status: 429,
154+
headers: {
155+
retry_after: '10',
156+
}
157+
)
158+
159+
error_message = "Rate limit of 200 RPM or 12 POST/PUT/DELETE requests per 5 seconds reached. Next request possible in 10 seconds."
160+
expect { client.get_conversation(1337) }.to raise_error(HelpScout::TooManyRequestsError, error_message)
161+
end
162+
end
147163
end

0 commit comments

Comments
 (0)