Last active
December 14, 2016 11:49
-
-
Save moteus/d42745a8f98eca7e3672528ce1f12a5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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