Class: Rack::Tracker::Handler

Inherits:
Object
  • Object
show all
Includes:
JavaScriptHelper
Defined in:
lib/rack/tracker/handler.rb

Constant Summary

Constants included from JavaScriptHelper

JavaScriptHelper::JS_ESCAPE_MAP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JavaScriptHelper

#escape_javascript

Constructor Details

#initialize(env, options = {}) ⇒ Handler

Returns a new instance of Handler.



11
12
13
14
15
# File 'lib/rack/tracker/handler.rb', line 11

def initialize(env, options = {})
  self.env = env
  self.options  = options
  self.position = options[:position] if options.has_key?(:position)
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/rack/tracker/handler.rb', line 6

def env
  @env
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/rack/tracker/handler.rb', line 5

def options
  @options
end

Class Method Details

.track(name, event) ⇒ Object

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/rack/tracker/handler.rb', line 36

def self.track(name, event)
  raise NotImplementedError.new("class method `#{__callee__}` is not implemented.")
end

Instance Method Details

#eventsObject



17
18
19
20
# File 'lib/rack/tracker/handler.rb', line 17

def events
  events = env.fetch('tracker', {})[self.class.to_s.demodulize.underscore] || []
  events.map{ |ev| "#{self.class}::#{ev['class_name']}".constantize.new(ev.except('class_name')) }
end

#inject(response) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rack/tracker/handler.rb', line 26

def inject(response)
  # Sub! is enough, in well formed html there's only one head or body tag.
  # Block syntax need to be used, otherwise backslashes in input will mess the output.
  # @see http://stackoverflow.com/a/4149087/518204 and https://github.com/railslove/rack-tracker/issues/50
  response.sub! %r{</#{self.position}>} do |m|
    self.render << m.to_s
  end
  response
end

#renderObject

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/rack/tracker/handler.rb', line 22

def render
  raise NotImplementedError.new('needs implementation')
end