Module: Assinafy::Utils

Defined in:
lib/assinafy/utils.rb,
sig/assinafy.rbs

Overview

Small, stateless helpers shared across resources. Intentionally private by convention — callers should reach for these via the resource methods, not directly.

Constant Summary collapse

MAX_NORMALIZATION_DEPTH =

Returns:

  • (Integer)
100

Class Method Summary collapse

Class Method Details

.body_params(hash) ⇒ Hash{String=>Object}

Build a body Hash, translating documented hyphenated body keys (signer-access-code, verification-code) while passing everything else through as-is.

Parameters:

  • hash (Hash, nil)

Returns:

  • (Hash{String=>Object})


60
61
62
# File 'lib/assinafy/utils.rb', line 60

def body_params(hash)
  normalize_keys(clean_params(hash), body_key_map)
end

.clean_params(hash) ⇒ Hash

Drop nil values (but keep false).

Parameters:

  • hash (Hash, nil)

Returns:

  • (Hash)

Raises:



35
36
37
38
39
40
41
42
# File 'lib/assinafy/utils.rb', line 35

def clean_params(hash)
  return {} if hash.nil?
  raise ValidationError.new('Parameters must be a Hash') unless hash.is_a?(Hash)

  hash.each_with_object({}) do |(key, value), result|
    result[key] = value unless value.nil?
  end
end

.handle_assinafy_response(body) ⇒ Object

Unwrap an Assinafy envelope — a Hash with a numeric status and an optional data key. Returns data (or nil) for 2xx, raises ApiError otherwise, and passes through non-envelope bodies.

Parameters:

  • body (Hash, Object)

Returns:

  • (Object)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/assinafy/utils.rb', line 17

def handle_assinafy_response(body)
  return body unless body.is_a?(Hash)
  return body unless body.key?('status')

  status = Integer(body['status'], exception: false)
  return body unless status

  if status >= 200 && status < 300
    body['data'] if body.key?('data')
  else
    raise ApiError.from_response(status, body)
  end
end

.query_params(hash) ⇒ Hash{String=>Object}

Build a query-string Hash, translating the Ruby-friendly snake_case aliases in query_key_map to the hyphenated forms documented in the Assinafy API (e.g. per_pageper-page).

Parameters:

  • hash (Hash, nil)

Returns:

  • (Hash{String=>Object})


50
51
52
# File 'lib/assinafy/utils.rb', line 50

def query_params(hash)
  normalize_keys(clean_params(hash), query_key_map)
end