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 templates OPTIMIZE: this can probably be faster



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

def dehandlebar(text, data = {})
  text.gsub(/{{([a-z0-9_-]+)}}/) { data[Regexp.last_match[1].to_sym].to_s }
end

#from_base64(text) ⇒ Object



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

def from_base64(text)
  Base64.decode64 text
end

#human_readable(text) ⇒ Object



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

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