Module: Axene::Mailer::Util

Defined in:
lib/axene/mailer/util.rb

Overview

Internal helpers that translate the SDK’s ergonomic inputs into the exact JSON shape the API expects. Not part of the public API.

Class Method Summary collapse

Class Method Details

.address(addr) ⇒ Hash?

Normalize a single address. A bare String becomes { email: … }.

Parameters:

  • addr (String, Hash, nil)

Returns:

  • (Hash, nil)


24
25
26
27
28
29
# File 'lib/axene/mailer/util.rb', line 24

def address(addr)
  return nil if addr.nil?
  return { email: addr } if addr.is_a?(String)

  symbolize(addr)
end

.address_list(addr) ⇒ Array<Hash>?

Normalize one-or-many addresses into an array, or nil if absent.

Parameters:

  • addr (String, Hash, Array, nil)

Returns:

  • (Array<Hash>, nil)


35
36
37
38
39
40
# File 'lib/axene/mailer/util.rb', line 35

def address_list(addr)
  return nil if addr.nil?

  list = addr.is_a?(Array) ? addr : [addr]
  list.map { |a| address(a) }
end

.escape(value) ⇒ String

URL-escape a path segment.

Parameters:

  • value (#to_s)

Returns:

  • (String)


57
58
59
# File 'lib/axene/mailer/util.rb', line 57

def escape(value)
  URI.encode_www_form_component(value.to_s)
end

.prune(hash) ⇒ Hash

Drop keys whose value is nil so they are omitted from the JSON body.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


16
17
18
# File 'lib/axene/mailer/util.rb', line 16

def prune(hash)
  hash.reject { |_, v| v.nil? }
end

.symbolize(hash) ⇒ Hash

Shallow-symbolize the keys of a Hash so callers may pass either string or symbol keys.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


47
48
49
50
51
# File 'lib/axene/mailer/util.rb', line 47

def symbolize(hash)
  return hash unless hash.is_a?(Hash)

  hash.each_with_object({}) { |(k, v), acc| acc[k.to_sym] = v }
end