From fd25c046d0cae092d3a353b23858f2790ffbc1f2 Mon Sep 17 00:00:00 2001 From: Gawin Date: Thu, 21 Sep 2017 14:43:44 +0200 Subject: [PATCH] Added Rate Limit error --- lib/faraday/raise_http_exception.rb | 3 ++- lib/postmates/error.rb | 3 ++- spec/error_spec.rb | 19 ++++++++++++++++++- spec/fixtures/rate_limit.json | 5 +++++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/rate_limit.json diff --git a/lib/faraday/raise_http_exception.rb b/lib/faraday/raise_http_exception.rb index c24e5af..dc1e1bd 100644 --- a/lib/faraday/raise_http_exception.rb +++ b/lib/faraday/raise_http_exception.rb @@ -14,6 +14,7 @@ def call(env) when 401 ; raise Postmates::Unauthorized, msg when 403 ; raise Postmates::Forbidden, msg when 404 ; raise Postmates::NotFound, msg + when 429 ; raise Postmates::RateLimit, msg when 500 ; raise Postmates::InternalServerError, msg when 503 ; raise Postmates::ServiceUnavailable, msg end @@ -25,4 +26,4 @@ def initialize(app) @parser = nil end end -end \ No newline at end of file +end diff --git a/lib/postmates/error.rb b/lib/postmates/error.rb index 72bd3ce..9c740c6 100644 --- a/lib/postmates/error.rb +++ b/lib/postmates/error.rb @@ -4,6 +4,7 @@ class BadRequest < Error; end # 400 class Unauthorized < Error; end # 401 class Forbidden < Error; end # 403 class NotFound < Error; end # 404 + class RateLimit < Error; end # 429 class InternalServerError < Error; end # 500 class ServiceUnavailable < Error; end # 503 -end \ No newline at end of file +end diff --git a/spec/error_spec.rb b/spec/error_spec.rb index 7b4bcb8..3a24d85 100644 --- a/spec/error_spec.rb +++ b/spec/error_spec.rb @@ -55,6 +55,23 @@ end end + describe 'rate limit reached' do + before do + 50.times do + stub_post(path_to('deliveries'), + params.merge(response_code: 400, + returns: 'invalid_params.json')) + end + stub_get(path_to('deliveries'), + response_code: 429, returns: 'rate_limit.json') + end + + it 'raises Postmates::RateLimit' do + expect { client.list ) } + .to raise_error Postmates::RateLimit + end + end + describe 'when there is a problem processing the request' do before do stub_get(path_to('deliveries'), @@ -79,4 +96,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/fixtures/rate_limit.json b/spec/fixtures/rate_limit.json new file mode 100644 index 0000000..4374059 --- /dev/null +++ b/spec/fixtures/rate_limit.json @@ -0,0 +1,5 @@ +{ + "kind": "error", + "code": "rate_limit_exceeded", + "message": "You are attempting to exceed our rate limit" +}