Class: Rack::Track::PixelSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/track.rb

Defined Under Namespace

Classes: Pixel

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ PixelSet

Returns a new instance of PixelSet.



35
36
37
38
39
# File 'lib/rack/track.rb', line 35

def initialize(&block)
  @pixels = []
  @areas = {}
  self.instance_eval(&block) if block_given?
end

Instance Method Details

#apply(request, response) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rack/track.rb', line 50

def apply(request, response)
  url = URI::parse(request.url) rescue nil
  return response if url.nil?
  
  matching_areas = @areas.find_all { |k, a| a.include? url.path.downcase }.collect { |k, v| k }
  @pixels.each do |p|
    paths = @areas[p.area]
    response.insert(response.rindex("</body>"), p.content) if ([:all_pages, :all].include?(p.area) || 
                                                              paths.include?(url.path.downcase)) &&
                                                              p.except.find_all { |ea| matching_areas.include? ea }.empty?
  end
  response
end

#area(id, *paths) ⇒ Object



41
42
43
44
# File 'lib/rack/track.rb', line 41

def area(id, *paths)
  @areas[id] = [] unless @areas[id]
  paths.each { |path| @areas[id] << path.downcase }
end

#pixel(name, opts = {}, &block) ⇒ Object



46
47
48
# File 'lib/rack/track.rb', line 46

def pixel(name, opts = {}, &block)
  @pixels << Pixel.new(name, opts[:on], opts[:except] ? [opts[:except]].flatten : [], block[])
end