lib/namely/resource_gateway.rb in namely-0.2.3 vs lib/namely/resource_gateway.rb in namely-0.2.4

- old
+ new

@@ -42,11 +42,11 @@ def json_index_paged Enumerator.new do |y| params = {} loop do - objects = get("/#{endpoint}", params)[resource_name] + objects = with_retry { get("/#{endpoint}", params)[resource_name] } break if objects.empty? objects.each { |o| y << o } params[:after] = objects.last["id"] @@ -69,9 +69,18 @@ rescue StandardError => e raise( FailedRequestError, "Couldn't parse \"id\" from response: #{e.message}" ) + end + + def with_retry + retries ||= 0 + yield + rescue RestClient::Exception => e + raise unless Namely.configuration.http_codes_to_retry.include?(e.http_code) + retry if (retries += 1) < Namely.configuration.retries + raise end def get(path, params = {}) params.merge!(access_token: access_token) JSON.parse(RestClient.get(url(path), accept: :json, params: params))