Skip to content

Commit

Permalink
Merge pull request #58 from ResultadosDigitais/feature/add-to_many_re…
Browse files Browse the repository at this point in the history
…quests-error

Add TooManyRequests error
  • Loading branch information
Almir Mendes authored Sep 16, 2020
2 parents 1dc55d3 + 4898276 commit 0f7eb52
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.4.0

- Add the TooManyRequests errors in case of rate limit exceeded. See [API request limit](https://developers.rdstation.com/en/request-limit) for more details

## 2.3.1

- Fixed a bug when no error is found in the known errors list (issue [#52](https://github.com/ResultadosDigitais/rdstation-ruby-client/issues/52))
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ RDStation.configure do |config|
# authorization.access_token_expires_in is the time (in seconds for with the token is valid)
# authorization.access_token is the new token
# authorization.refresh_token is the existing refresh_token
#
#
# If you are using ActiveRecord, you may want to update the stored access_token, like in the following code:
MyStoredAuth.where(refresh_token: authorization.refresh_token).update_all(access_token: authorization.access_token)
end
Expand Down Expand Up @@ -232,7 +232,7 @@ client.fields.create builder.build
```ruby
payload = {} # hash representing the payload
client = RDStation::Client.new(access_token: 'access_token', refresh_token: 'refresh_token')
client.fields.update('FIELD_UUID', payload)
client.fields.update('FIELD_UUID', payload)
```
Or you can use the new `RDStation::Builder::Field`

Expand Down Expand Up @@ -315,6 +315,7 @@ Each endpoint may raise errors accoording to the HTTP response code from RDStati
- `RDStation::Error::Conflict` (409)
- `RDStation::Error::UnsupportedMediaType` (415)
- `RDStation::Error::UnprocessableEntity` (422)
- `RDStation::Error::TooManyRequests` (429)
- `RDStation::Error::InternalServerError` (500)
- `RDStation::Error::NotImplemented` (501)
- `RDStation::Error::BadGateway` (502)
Expand Down
1 change: 1 addition & 0 deletions lib/rdstation/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class NotAcceptable < Error; end
class Conflict < Error; end
class UnsupportedMediaType < Error; end
class UnprocessableEntity < Error; end
class TooManyRequests < Error; end
class InternalServerError < Error; end
class NotImplemented < Error; end
class BadGateway < Error; end
Expand Down
8 changes: 8 additions & 0 deletions lib/rdstation/error/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ class Format
ARRAY_OF_HASHES = 'ARRAY_OF_HASHES'
HASH_OF_MULTIPLE_TYPES = 'HASH_OF_MULTIPLE_TYPES'
HASH_OF_HASHES = 'HASH_OF_HASHES'
SINGLE_HASH = 'SINGLE_HASH'

def initialize(errors)
@errors = errors
end

def format
return FLAT_HASH if flat_hash?
return SINGLE_HASH if single_hash?
return HASH_OF_ARRAYS if hash_of_arrays?
return HASH_OF_HASHES if hash_of_hashes?
return HASH_OF_MULTIPLE_TYPES if hash_of_multiple_types?
Expand All @@ -24,6 +26,12 @@ def format

private

def single_hash?
return unless @errors.is_a?(Hash)

@errors.key?('error')
end

def flat_hash?
return unless @errors.is_a?(Hash)

Expand Down
17 changes: 16 additions & 1 deletion lib/rdstation/error/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def to_array
return @error_response unless @error_response.is_a?(Hash)

case error_format.format
when RDStation::Error::Format::SINGLE_HASH
return from_single_hash
when RDStation::Error::Format::FLAT_HASH
return from_flat_hash
when RDStation::Error::Format::HASH_OF_ARRAYS
Expand All @@ -26,6 +28,19 @@ def to_array
errors
end

def from_single_hash
error_hash = @error_response.dup
error_message = error_hash.delete('error')

[
{
'error_type' => 'TOO_MANY_REQUESTS',
'error_message' => error_message,
'details' => error_hash
}
]
end

def from_flat_hash
[errors]
end
Expand Down Expand Up @@ -60,7 +75,7 @@ def error_format
end

def errors
@errors ||= @error_response['errors']
@errors ||= @error_response.fetch('errors', @error_response)
end

private
Expand Down
3 changes: 2 additions & 1 deletion lib/rdstation/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def error_class
when 409 then RDStation::Error::Conflict
when 415 then RDStation::Error::UnsupportedMediaType
when 422 then RDStation::Error::UnprocessableEntity
when 429 then RDStation::Error::TooManyRequests
when 500 then RDStation::Error::InternalServerError
when 501 then RDStation::Error::NotImplemented
when 502 then RDStation::Error::BadGateway
Expand All @@ -57,7 +58,7 @@ def error_formatter
end

def additional_error_attributes
{
attrs = {
'headers' => response.headers,
'body' => JSON.parse(response.body),
'http_status' => response.code,
Expand Down
2 changes: 1 addition & 1 deletion lib/rdstation/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RDStation
VERSION = '2.3.1'.freeze
VERSION = '2.4.0'.freeze
end
17 changes: 17 additions & 0 deletions spec/lib/rdstation/error/format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,22 @@
expect(result).to eq(RDStation::Error::Format::HASH_OF_HASHES)
end
end

context 'when receives a single hash with error' do
let(:errors) do
{
'error' => "'lead_limiter' rate limit exceeded for 86400 second(s) period for key ...",
'max' => 24,
'usage' => 55,
'remaining_time' => 20745,
}
end

it 'returns the SINGLE_HASH format' do
result = error_format.format
expect(result).to eq(RDStation::Error::Format::SINGLE_HASH)
end

end
end
end
30 changes: 30 additions & 0 deletions spec/lib/rdstation/error/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,35 @@
expect(result).to eq(expected_result)
end
end

context 'when receives a single hash of errors' do
let(:error_format) { instance_double(RDStation::Error::Format, format: RDStation::Error::Format::SINGLE_HASH) }

let(:error_response) do
{
'error' => "'lead_limiter' rate limit exceeded for 86400 second(s) period for key",
'max' => 24,
'usage' => 55,
'remaining_time' => 20745
}
end

let(:error_formatter) { described_class.new(error_response) }

let(:expected_result) do
[
{
'error_type' => 'TOO_MANY_REQUESTS',
'error_message' => "'lead_limiter' rate limit exceeded for 86400 second(s) period for key",
'details' => { 'max' => 24, 'usage' => 55, 'remaining_time' => 20745 }
}
]
end

it 'returns an array of errors' do
result = error_formatter.to_array
expect(result).to eq(expected_result)
end
end
end
end

0 comments on commit 0f7eb52

Please sign in to comment.