Sha256: a997b93a3a21812967acf561f14f649989a9d1b4625d2ee1f4e5f75678de02e6

Contents?: true

Size: 1.56 KB

Versions: 15

Compression:

Stored size: 1.56 KB

Contents

module RestfulResource
  class MaximumAttemptsReached < StandardError
    def message
      'The maximum attempts limit was reached before the resource was ready'
    end
  end

  module Redirections
    def self.included(base)
      base.instance_eval do
        def post(data: {}, delay: 1.0, max_attempts: 10, headers: {}, open_timeout: nil, timeout: nil, **params)
          url = collection_url(**params)

          response = accept_redirected_result(
            response: http.post(url, data: data, headers: headers, open_timeout: nil, timeout: nil),
            delay: delay,
            max_attempts: max_attempts
          )

          new(parse_json(response.body))
        end

        def self.accept_redirected_result(response:, delay:, max_attempts:)
          new_response = response
          if response.status == 303
            attempts = 0
            resource_location = response.headers[:location]

            RestfulResource::Redirections.wait(delay)
            new_response = http.get(resource_location, headers: {}, open_timeout: nil, timeout: nil)

            while (new_response.status == 202) && (attempts < max_attempts)
              attempts += 1
              RestfulResource::Redirections.wait(delay)
              new_response = http.get(resource_location, headers: {}, open_timeout: nil, timeout: nil)
            end

            raise RestfulResource::MaximumAttemptsReached if attempts == max_attempts
          end
          response = new_response
        end
      end
    end

    def self.wait(seconds)
      Kernel.sleep(seconds)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
restful_resource-2.18.1 lib/restful_resource/redirections.rb
restful_resource-2.18.0 lib/restful_resource/redirections.rb
restful_resource-2.17.0 lib/restful_resource/redirections.rb
restful_resource-2.16.0 lib/restful_resource/redirections.rb
restful_resource-2.15.0 lib/restful_resource/redirections.rb
restful_resource-2.14.0 lib/restful_resource/redirections.rb
restful_resource-2.13.4 lib/restful_resource/redirections.rb
restful_resource-2.13.3 lib/restful_resource/redirections.rb
restful_resource-2.13.2 lib/restful_resource/redirections.rb
restful_resource-2.13.1 lib/restful_resource/redirections.rb
restful_resource-2.13.0 lib/restful_resource/redirections.rb
restful_resource-2.12.1 lib/restful_resource/redirections.rb
restful_resource-2.12.0 lib/restful_resource/redirections.rb
restful_resource-2.11.0 lib/restful_resource/redirections.rb
restful_resource-2.10.3 lib/restful_resource/redirections.rb