Module: Authify::API::Helpers::TextProcessing

Included in:
Models::User
Defined in:
lib/authify/api/helpers/text_processing.rb

Overview

Helper methods for working with different text formats

Instance Method Summary collapse

Instance Method Details

#decoded_hash(hash) ⇒ Object



10
11
12
# File 'lib/authify/api/helpers/text_processing.rb', line 10

def decoded_hash(hash)
  hash.update(hash) { |_, v| v.is_a?(String) ? human_readable(v) : v }
end

#dehandlebar(text, data = {}) ⇒ Object

Interpolates handlebars-style (liquid) templates



15
16
17
# File 'lib/authify/api/helpers/text_processing.rb', line 15

def dehandlebar(text, data = {})
  Liquid::Template.parse(text).render(data, error_mode: :warn, strict_variables: true)
end

#from_base64(text) ⇒ Object



30
31
32
# File 'lib/authify/api/helpers/text_processing.rb', line 30

def from_base64(text)
  Base64.decode64 text
end

#human_readable(text) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/authify/api/helpers/text_processing.rb', line 19

def human_readable(text)
  if text =~ /^\{([a-zA-Z0-9]+)\}([^\n]+)$/
    form, data = text.match(/^\{([a-zA-Z0-9]+)\}([^\n]+)$/)[1, 2]

    raise "Invalid Conversion: #{form}" unless valid_formats.include?(form.downcase.to_sym)
    send("from_#{form.downcase}".to_sym, data)
  else
    text
  end
end

#valid_formatsObject



6
7
8
# File 'lib/authify/api/helpers/text_processing.rb', line 6

def valid_formats
  [:base64]
end