Class: RamenRails::ScriptTag

Inherits:
Object
  • Object
show all
Defined in:
lib/ramen-rails/script_tag.rb

Defined Under Namespace

Classes: EmptySettings

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, ramen_settings, options = {}) ⇒ ScriptTag

Returns a new instance of ScriptTag.



14
15
16
17
18
# File 'lib/ramen-rails/script_tag.rb', line 14

def initialize(template, ramen_settings, options = {})
  self.template = template
  self.ramen_settings = ramen_settings
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/ramen-rails/script_tag.rb', line 13

def options
  @options
end

#ramen_settingsObject

Returns the value of attribute ramen_settings.



13
14
15
# File 'lib/ramen-rails/script_tag.rb', line 13

def ramen_settings
  @ramen_settings
end

#templateObject

Returns the value of attribute template.



13
14
15
# File 'lib/ramen-rails/script_tag.rb', line 13

def template
  @template
end

Class Method Details

.generate(template, ramen_settings, options = {}) ⇒ Object



8
9
10
11
# File 'lib/ramen-rails/script_tag.rb', line 8

def self.generate(template, ramen_settings, options = {})
  st = new(template, ramen_settings, options)
  st.generate
end

Instance Method Details

#add_auth_hash!Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ramen-rails/script_tag.rb', line 56

def add_auth_hash!
  return unless ramen_settings[:user].present? &&
    options[:organization_secret].present? &&
    ramen_settings[:auth_hash].blank?

  user = ramen_settings[:user]

  secret_string = "#{user[:email]}:#{user[:id]}:#{user[:name]}:#{ramen_settings[:timestamp]}:#{options[:organization_secret]}"

  ramen_settings[:auth_hash] = (Digest::SHA2.new << secret_string).to_s
end

#controllerObject



68
69
70
# File 'lib/ramen-rails/script_tag.rb', line 68

def controller
  template.try :controller
end

#generateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ramen-rails/script_tag.rb', line 24

def generate
  if ramen_settings.blank?
    raise EmptySettings.new("need to pass ramen_script_tag a non-empty ramen_settings argument")
  end

  ramen_settings[:timestamp] ||= Time.now.to_i
  
  add_auth_hash!

  ramen_script = <<-RAMEN_SCRIPT
  <script id="RamenSettingsScriptTag">
(function() {
  var opts = #{ActiveSupport::JSON.encode(ramen_settings)};
  window.ramenSettings = window.ramenSettings || {};
  for (var property in opts) {
    if (opts.hasOwnProperty(property)) {
      window.ramenSettings[property] = opts[property];
    }
  }
})()
  </script>
  <script src="#{ramen_js_asset_uri}" async></script>
  RAMEN_SCRIPT

  if controller
    controller.
      instance_variable_set(RamenRails::SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE, true)
  end

  ramen_script.respond_to?(:html_safe) ? ramen_script.html_safe : ramen_script
end

#ramen_js_asset_uriObject



20
21
22
# File 'lib/ramen-rails/script_tag.rb', line 20

def ramen_js_asset_uri
  RamenRails.config.ramen_js_asset_uri || "https://cdn.ramen.is/assets/ramen.js"
end