Module: PatchRetention::Util

Included in:
Contacts::Delete, Contacts::Find, Contacts::FindOrCreate, Contacts::Update, Events::Create, Events::Find
Defined in:
lib/patch_retention/util.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#parse_error_message(response) ⇒ String

Extracts error message from the response.

Parameters:

  • response (Object)

    The response to parse the error message from.

Returns:

  • (String)

    The error message parsed from the response body.



23
24
25
26
27
28
29
30
31
# File 'lib/patch_retention/util.rb', line 23

def parse_error_message(response)
  if response.body.is_a?(Hash) && response.body["error"]
    response.body["error"]
  elsif response.status == 502
    "Internal Server Error: Patch API"
  else
    "Request failed with status #{response.status}"
  end
end

#raise_error_if_present {|response| ... } ⇒ response

Raises a PatchRetention::Error if the yielded block’s response status is not 200.

Yields:

  • (response)

    The block to execute, which should return a response.

Returns:

  • (response)

    The response from the yielded block if no error is raised.

Raises:



10
11
12
13
14
15
16
17
18
# File 'lib/patch_retention/util.rb', line 10

def raise_error_if_present
  response = yield
  raise PatchRetention::Error, parse_error_message(response) unless response.status.between?(200, 206)

  case response.body
  when Hash, Array then response.body
  else true
  end
end