Class: Analytical::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/analytical/api.rb

Defined Under Namespace

Classes: ImmediateDelegateHelper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/analytical/api.rb', line 12

def initialize(options={})
  @options = options
  @modules = @options[:modules].inject(ActiveSupport::OrderedHash.new) do |h, m|
    module_options = @options.merge(@options[m] || {})
    module_options.delete(:modules)
    module_options[:session_store] = Analytical::SessionCommandStore.new(@options[:session], m) if @options[:session]
    h[m] = "Analytical::Modules::#{m.to_s.camelize}".constantize.new(module_options)
    h
  end
  @dummy_module = Analytical::Modules::DummyModule.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Catch commands such as :track, :identify and send them on to all of the modules. Or… if a module name is passed, return that module so it can be used directly, ie: analytical.console.go ‘make’, :some=>:cookies



29
30
31
32
33
34
35
36
37
38
# File 'lib/analytical/api.rb', line 29

def method_missing(method, *args, &block)
  method = method.to_sym
  if @modules.keys.include?(method)
    @modules[method]
  elsif available_modules.include?(method)
    @dummy_module
  else
    process_command method, *args
  end
end

Instance Attribute Details

#modulesObject

Returns the value of attribute modules.



10
11
12
# File 'lib/analytical/api.rb', line 10

def modules
  @modules
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/analytical/api.rb', line 10

def options
  @options
end

Instance Method Details

#body_append_javascriptObject



92
93
94
# File 'lib/analytical/api.rb', line 92

def body_append_javascript
  [init_javascript(:body_append), tracking_javascript(:body_append)].delete_if{|s| s.blank?}.join("\n")
end

#body_prepend_javascriptObject



89
90
91
# File 'lib/analytical/api.rb', line 89

def body_prepend_javascript
  [init_javascript(:body_prepend), tracking_javascript(:body_prepend)].delete_if{|s| s.blank?}.join("\n")
end

#head_append_javascriptObject Also known as: head_javascript



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/analytical/api.rb', line 69

def head_append_javascript
  js = [
    init_javascript(:head_append),
    tracking_javascript(:head_append),
  ]

  if options[:javascript_helpers]
    if Gem::Version.new(::Rails::VERSION::STRING) >= Gem::Version.new('3.1.0')  # Rails 3.1 lets us override views in engines
      js << options[:controller].send(:render_to_string, :partial=>'analytical_javascript') if options[:controller]
    else # All other rails
      _partial_path = Pathname.new(__FILE__).dirname.join('..', '..', 'app/views/application', '_analytical_javascript.html.erb').to_s
      js << options[:controller].send(:render_to_string, :file=>_partial_path, :layout=>false) if options[:controller]
    end
  end

  js.delete_if{|s| s.blank?}.join("\n")
end

#head_prepend_javascriptObject

These methods return the javascript that should be inserted into each section of your layout



65
66
67
# File 'lib/analytical/api.rb', line 65

def head_prepend_javascript
  [init_javascript(:head_prepend), tracking_javascript(:head_prepend)].delete_if{|s| s.blank?}.join("\n")
end

#nowObject

Returns a new delegation object for immediate processing of a command



58
59
60
# File 'lib/analytical/api.rb', line 58

def now
  ImmediateDelegateHelper.new(self)
end