class Mjml::Parser

Public Class Methods

new(input) click to toggle source

Create new parser

@param input [String] The string to transform in html

# File lib/mjml/parser.rb, line 7
def initialize input
  file = File.open(in_tmp_file, 'w')
  file.write(input)
  file.close
end

Public Instance Methods

render() click to toggle source

Render mjml template

@return [String]

# File lib/mjml/parser.rb, line 16
def render
  result = run
  remove_tmp_files
  result
rescue

  ""
end
run() click to toggle source

Exec mjml command

@return [String] The result as string

# File lib/mjml/parser.rb, line 28
def run
  command = "#{mjml_bin} -r #{in_tmp_file} -o #{out_tmp_file}"
  # puts command
  `#{command}`
  file = File.open(out_tmp_file, 'r')
  str  = file.read
  file.close
  str
end

Private Instance Methods

in_tmp_file() click to toggle source

Get parser tpm file to get result

@return [String]

# File lib/mjml/parser.rb, line 67
def in_tmp_file

  @_in_tmp_file ||= "#{tmp_dir}/in_#{(0...8).map { (65 + rand(26)).chr }.join}.mjml"
  # puts @_in_tmp_file
  return @_in_tmp_file
end
mjml_bin() click to toggle source

Get mjml bin path

@return [String]

# File lib/mjml/parser.rb, line 77
def mjml_bin
  Mjml::BIN
end
out_tmp_file() click to toggle source

Get parser tpm file to store result

@return [String]

# File lib/mjml/parser.rb, line 59
def out_tmp_file

  @_out_tmp_file ||= "#{tmp_dir}/out_#{(0...8).map { (65 + rand(26)).chr }.join}.html"
end
remove_tmp_files() click to toggle source

Remove tmp files

@return nil

# File lib/mjml/parser.rb, line 43
def remove_tmp_files
  FileUtils.rm(in_tmp_file)
  FileUtils.rm(out_tmp_file)
  nil
end
tmp_dir() click to toggle source

Return tmp dir

@return [String]

# File lib/mjml/parser.rb, line 52
def tmp_dir
  "/tmp"
end