Class: ResolveTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/apollo_commons_ruby/ResolveTemplate.rb

Instance Method Summary collapse

Constructor Details

#initializeResolveTemplate

Returns a new instance of ResolveTemplate.



7
8
9
10
11
12
# File 'lib/apollo_commons_ruby/ResolveTemplate.rb', line 7

def initialize
  @context = V8::Context.new
  @context.load("./scripts/underscore-min.js")
  @context.eval("var generateTemplatedStringEnv = function(templateString, dataObject) { _.templateSettings = { interpolate: /\{\{(.+?)\}\}/g }; var compiledWithEnv = _.template(templateString); return compiledWithEnv(dataObject);}")
  @context.eval("var generateTemplatedStringLang = function(templateString, dataObject) { _.templateSettings = { interpolate: /\<\<(.+?)\>\>/g }; var compiledWithLang = _.template(templateString); return compiledWithLang(dataObject);}")
end

Instance Method Details

#resolve(data_template, configuration) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/apollo_commons_ruby/ResolveTemplate.rb', line 14

def resolve(data_template, configuration)
  envFilePath = configuration.env_data_file_path
  envData = read_data_from_file_path(envFilePath)
  envData = JSON.parse(envData)
  function = @context[:generateTemplatedStringEnv];
  begin
    data_template = data_template.force_encoding("UTF-8")
    function.call(data_template, envData)
  rescue Exception => e
    puts "\n", e.cause, e.javascript_backtrace;
  end
end

#resolveEnv(data_template, configuration, tenant_id, project_id) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apollo_commons_ruby/ResolveTemplate.rb', line 27

def resolveEnv(data_template, configuration, tenant_id, project_id)
  envFilePath = configuration.env_file_path(tenant_id, project_id)
  envData = read_data_from_file_path(envFilePath)
  envData = JSON.parse(envData)
  function = @context[:generateTemplatedStringEnv];
  begin
    data_template = data_template.force_encoding("UTF-8")
    function.call(data_template, envData)
  rescue Exception => e
    puts "\n", e.cause, e.javascript_backtrace;
  end
end

#resolveLang(data_template, configuration, language) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/apollo_commons_ruby/ResolveTemplate.rb', line 40

def resolveLang(data_template, configuration, language)
  langFilePath = configuration.language_file_path_for_template(language)
  langData = read_res_data_from_file_path(langFilePath)
  langData = JSON.parse(langData)
  function = @context[:generateTemplatedStringLang];
  begin
    data_template = data_template.force_encoding("UTF-8")
    function.call(data_template, langData)
  rescue Exception => e
    puts "\n", e.cause, e.javascript_backtrace;
  end
end