Module: Kerbi::Mixins::Mixer

Included in:
Kerbi::Mixer, Utils::Values::ErbWrapper
Defined in:
lib/mixins/mixer.rb

Instance Method Summary collapse

Instance Method Details

#b64dec(string) ⇒ String

Returns encoded string.

Parameters:

  • string (String)

    string to be base64 encoded

Returns:

  • (String)

    encoded string



45
46
47
48
49
50
51
# File 'lib/mixins/mixer.rb', line 45

def b64dec(string)
  if string
    Base64.decode64(string).strip
  else
    ''
  end
end

#b64enc(string) ⇒ String

Returns encoded string.

Parameters:

  • string (String)

    string to be base64 encoded

Returns:

  • (String)

    encoded string



35
36
37
38
39
40
41
# File 'lib/mixins/mixer.rb', line 35

def b64enc(string)
  if string
    Base64.strict_encode64(string)
  else
    ''
  end
end

#b64enc_file(fname) ⇒ String

Returns encoded string.

Parameters:

  • fname (String)

    absolute path of file to be encoded

Returns:

  • (String)

    encoded string



55
56
57
58
# File 'lib/mixins/mixer.rb', line 55

def b64enc_file(fname)
  file_contents = File.read(fname) rescue nil
  b64enc(file_contents)
end

#embed(dict, indent: 25) ⇒ String

Returns encoded string.

Parameters:

  • dict (Hash)

    hash or array

Returns:

  • (String)

    encoded string



7
8
9
10
11
12
13
14
# File 'lib/mixins/mixer.rb', line 7

def embed(dict, indent: 25)
  _dict = dict.is_a?(Array) ? dict.first : dict
  raw = YAML.dump(_dict).sub("---", "")
  indented_lines = raw.split("\n").map do |line|
    line.indent(indent)
  end
  "\n#{indented_lines.join("\n")}"
end

#embed_array(dicts, indent: 25) ⇒ String

Returns encoded string.

Parameters:

  • dicts (Array|Hash)

    hash or array

Returns:

  • (String)

    encoded string



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mixins/mixer.rb', line 18

def embed_array(dicts, indent: 25)
  return "[]" unless dicts.present?

  unless dicts.is_a?(Array)
    raise "embed_array called with non-array #{dicts.class} #{dicts}"
  end

  raw = YAML.dump(dicts).sub("---", "")
  indented_lines = raw.split("\n").map do |line|
    line.indent(indent)
  end

  "\n#{indented_lines.join("\n")}"
end

#http_descriptor_to_url(**opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (**opts):

  • url (String)

    full URL to raw yaml file contents on the web

  • from (String)

    one of [github]

  • except (String)

    list of filenames to avoid

Raises:

  • (Exception)

    if project-id/file missing in github hash



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mixins/mixer.rb', line 66

def http_descriptor_to_url(**opts)
  return opts[:url] if opts[:url]

  if opts[:from] == 'github'
    base = "https://raw.githubusercontent.com"
    branch = opts[:branch] || 'master'
    project, file = (opts[:project] || opts[:id]), opts[:file]
    raise "Project and/or file not found" unless project && file
    "#{base}/#{project}/#{branch}/#{file}"
  end
end