Skip to content

Instantly share code, notes, and snippets.

@moteus
Last active December 14, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moteus/d42745a8f98eca7e3672528ce1f12a5d to your computer and use it in GitHub Desktop.
Save moteus/d42745a8f98eca7e3672528ce1f12a5d to your computer and use it in GitHub Desktop.
local log = require "resources.functions.log".apply_var
function apply_global_var(str, t)
return (string.gsub(str, "$${.-}", function(key)
key = string.sub(key, 4, -2)
local var = t and t[key] or freeswitch.getGlobalVariable(key)
if not var then
log.warningf("unknown global variable: %s", key)
end
return var or ''
end))
end
function apply_session_var(str, t)
return (string.gsub(str, "${.-}", function(key)
key = string.sub(key, 3, -2)
local var = t and t[key] or freeswitch.getGlobalVariable(key)
return var or ''
end))
end
function apply_var(str, t)
local str = apply_global_var(str, t)
if session then
str = apply_session_var(str, t)
end
return str
end
-- usage
-- apply_global_var('$${hold_music}')
--
-- dofile( apply_global_var('$${script_dir}/foo.lua') )
--
-- apply_global_var('$${storage_dir}/$${domain}',{
-- domain = 'my_domain';
-- })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment