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

Parses the error message from the response. Raises a PatchRetention::Error if the response status is 502 and the body is blank.

Parameters:

  • response (Object)

    The response to parse the error message from.

Returns:

  • (String)

    The error message parsed from the response body.



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

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

#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
19
# 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)

  begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    true
  end
end