Class: Prefab::JavaScriptStub
- Inherits:
-
Object
- Object
- Prefab::JavaScriptStub
- Defined in:
- lib/prefab/javascript_stub.rb
Constant Summary collapse
- LOG =
Prefab::InternalLogger.new(self)
Instance Method Summary collapse
-
#bootstrap(context) ⇒ Object
Generate the JavaScript snippet to bootstrap the client SDK.
-
#generate_stub(context) ⇒ Object
Generate the JavaScript snippet to replace the client SDK.
-
#initialize(client = nil) ⇒ JavaScriptStub
constructor
A new instance of JavaScriptStub.
Constructor Details
#initialize(client = nil) ⇒ JavaScriptStub
Returns a new instance of JavaScriptStub.
7 8 9 |
# File 'lib/prefab/javascript_stub.rb', line 7 def initialize(client = nil) @client = client || Prefab.instance end |
Instance Method Details
#bootstrap(context) ⇒ Object
Generate the JavaScript snippet to bootstrap the client SDK. This will include the configuration values that are permitted to be sent to the client SDK.
If the context provided to the client SDK is not the same as the context used to generate the configuration values, the client SDK will still generate a fetch to get the correct values for the context.
Any keys that could not be resolved will be logged as a warning to the console.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/prefab/javascript_stub.rb', line 21 def bootstrap(context) configs, warnings = data(context) <<~JS window._prefabBootstrap = { configs: #{JSON.dump(configs)}, context: #{JSON.dump(context)} } #{log_warnings(warnings)} JS end |
#generate_stub(context) ⇒ Object
Generate the JavaScript snippet to replace the client SDK. Use this to get ‘prefab.get` and `prefab.isEnabled` functions on the window object.
Only use this if you are not using the client SDK and do not need client-side context.
Any keys that could not be resolved will be logged as a warning to the console.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/prefab/javascript_stub.rb', line 40 def generate_stub(context) configs, warnings = data(context) <<~JS window.prefab = window.prefab || {}; window.prefab.config = #{JSON.dump(configs)}; window.prefab.get = function(key) { return window.prefab.config[key]; }; window.prefab.isEnabled = function(key) { return window.prefab.config[key] === true; }; #{log_warnings(warnings)} JS end |