Sha256: 102c2ea09403af6c104c4298e1d2ccfc1a9afa565e8b303da5d8c4179c420ac1

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module PatchRetention # rubocop:disable Style/ClassAndModuleChildren
  # The Util module provides utility methods used across the PatchRetention library.
  # These methods are designed to handle common tasks such as error handling and response parsing.
  module Util
    # Raises a PatchRetention::Error if the yielded block's response status is not 200.
    # @yield [response] The block to execute, which should return a response.
    # @return [response] The response from the yielded block if no error is raised.
    def raise_error_if_present
      response = yield
      raise PatchRetention::Error, parse_error_message(response) unless response.status.between?(200, 206)

      JSON.parse(response.body)
    end

    # Parses the error message from the response.
    # Raises a PatchRetention::Error if the response status is 502 and the body is blank.
    # @param response [Object] The response to parse the error message from.
    # @return [String] The error message parsed from the response body.
    def parse_error_message(response)
      if response.status == 502 && response.body.blank?
        raise PatchRetention::Error,
              "Internal Server Error: Patch API"
      end

      JSON.parse(response.body)["error"]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
patch_retention-0.1.2 lib/patch_retention/util.rb
patch_retention-0.1.1 lib/patch_retention/util.rb
patch_retention-0.1.0 lib/patch_retention/util.rb