Module: Paubox::FormatHelper

Included in:
MailToMessage, Message
Defined in:
lib/paubox/format_helper.rb

Overview

Utility methods for Message and MailToMessage

Constant Summary collapse

BASE64_REGEX =
%r(/^(?:[A-Za-z0-9+\/]{4}\n?)*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/).freeze

Instance Method Summary collapse

Instance Method Details

#base64_encode_if_needed(str) ⇒ Object



15
16
17
18
19
# File 'lib/paubox/format_helper.rb', line 15

def base64_encode_if_needed(str)
  return str if base64_encoded?(str.to_s)

  Base64.encode64(str.to_s)
end

#base64_encoded?(value) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/paubox/format_helper.rb', line 9

def base64_encoded?(value)
  return false unless value.is_a?(String)

  !value.strip.match(BASE64_REGEX).nil?
end

#convert_keys_to_json_version(hash) ⇒ Object

Converts hash keys to strings and maps them to expected JSON key. Also converts hashes in shallow arrays.



23
24
25
26
27
28
29
30
31
32
# File 'lib/paubox/format_helper.rb', line 23

def convert_keys_to_json_version(hash)
  converted = {}
  hash.each_pair do |key, val|
    converted[ruby_to_json_key(key)] = val.is_a?(Hash) ? convert_keys_to_json_version(val) : val
    next unless val.is_a?(Array)

    val.each_with_index { |el, i| val[i] = convert_keys_to_json_version(el) if el.is_a?(Hash) }
  end
  converted
end

#ruby_to_json_key(key) ⇒ Object



34
35
36
37
38
# File 'lib/paubox/format_helper.rb', line 34

def ruby_to_json_key(key)
  { reply_to: 'reply-to', html_content: 'text/html', text_content: 'text/plain',
    filename: 'fileName', file_name: 'fileName', content_type: 'contentType',
    allow_non_tls: 'allowNonTLS', force_secure_notification: 'forceSecureNotification' }[key] || key.to_s
end

#squish(str) ⇒ Object



56
57
58
# File 'lib/paubox/format_helper.rb', line 56

def squish(str)
  str.to_s.split.join(' ')
end

#string_or_array_to_array(object) ⇒ Object

def get_values_whitelist(*vals)

vals.map { |k| next unless mail[k]; [ruby_to_json_key(k), mail[k]] }.to_h

end



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/paubox/format_helper.rb', line 44

def string_or_array_to_array(object)
  case object
  when String
    a = object.split(',').map { |str| squish(str) }
  when Array
    a = object.map { |s| squish(s) }
  else
    return []
  end
  a.reject(&:empty?)
end