rack-track 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ require 'jeweler'
15
15
  Jeweler::Tasks.new do |gem|
16
16
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
17
  gem.name = "rack-track"
18
- gem.version = "0.1.3"
18
+ gem.version = "0.1.4"
19
19
  gem.homepage = "http://github.com/michaelbaldry/rack-track"
20
20
  gem.license = "MIT"
21
21
  gem.summary = %Q{Taking all the pain away from managing Tracking Pixels}
data/lib/rack/track.rb CHANGED
@@ -13,9 +13,8 @@ module Rack
13
13
  response_body = ""
14
14
  response.each { |p| response_body += p }
15
15
  response = [@rules.apply(request, response_body)] if response_body.include? "</body>"
16
- headers["Content-Length"] = response.inject(0){|sum,x| sum + x.length }.to_s
16
+ headers["Content-Length"] = get_length(response)
17
17
  end
18
- p headers
19
18
  [status, headers, response]
20
19
  end
21
20
 
@@ -24,8 +23,14 @@ module Rack
24
23
 
25
24
  private
26
25
 
26
+ def get_length(response)
27
+ sum_length = 0
28
+ response.each {|x| sum_length += x.length }
29
+ sum_length.to_s
30
+ end
31
+
27
32
  class PixelSet
28
- Pixel = Struct.new(:name, :area, :content)
33
+ Pixel = Struct.new(:name, :area, :except, :content)
29
34
 
30
35
  def initialize(&block)
31
36
  @pixels = []
@@ -39,14 +44,19 @@ module Rack
39
44
  end
40
45
 
41
46
  def pixel(name, opts = {}, &block)
42
- @pixels << Pixel.new(name, opts[:on], block[])
47
+ @pixels << Pixel.new(name, opts[:on], opts[:except] ? [opts[:except]].flatten : [], block[])
43
48
  end
44
49
 
45
50
  def apply(request, response)
46
51
  url = URI::parse(request.url)
52
+ p "applying to #{url.path.downcase}"
53
+ matching_areas = @areas.find_all { |k, a| a.include? url.path.downcase }.collect { |k, v| k }
54
+ p matching_areas
47
55
  @pixels.each do |p|
48
56
  paths = @areas[p.area]
49
- response.insert(response.rindex("</body>"), p.content) if [:all_pages, :all].include?(p.area) || paths.include?(url.path.downcase)
57
+ response.insert(response.rindex("</body>"), p.content) if ([:all_pages, :all].include?(p.area) ||
58
+ paths.include?(url.path.downcase)) &&
59
+ p.except.find_all { |ea| matching_areas.include? ea }.empty?
50
60
  end
51
61
  response
52
62
  end
data/rack-track.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rack-track}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Baldry"]
12
- s.date = %q{2011-08-24}
12
+ s.date = %q{2011-09-01}
13
13
  s.description = %q{A rack middleware that allows you to define and document tracking pixels in one location}
14
14
  s.email = %q{clockwize@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -35,7 +35,7 @@ class TestRackTrack < Test::Unit::TestCase
35
35
  end
36
36
 
37
37
  assert tp.instance_variable_get(:@areas) == {:confirmation => ["business-energy/confirmation", "business-communications/confirmation"]}
38
- assert tp.instance_variable_get(:@pixels) == [Rack::Track::PixelSet::Pixel.new("Confirmation (GA)", :confirmation, "THIS IS A GA CONFIRMATION TEST")]
38
+ assert tp.instance_variable_get(:@pixels) == [Rack::Track::PixelSet::Pixel.new("Confirmation (GA)", :confirmation, [], "THIS IS A GA CONFIRMATION TEST")]
39
39
  end
40
40
 
41
41
  def test_psudo_area_all
@@ -46,6 +46,31 @@ class TestRackTrack < Test::Unit::TestCase
46
46
  assert last_response.body.include? "THIS-IS-A-TEST</body>"
47
47
  end
48
48
 
49
+ def test_area_exclusion
50
+ @rules = Proc.new do
51
+ area :an_area, "/blah", "/blah/test"
52
+
53
+ pixel "Test pixel", :on => :an_area do
54
+ "THIS-IS-A-TEST"
55
+ end
56
+
57
+ pixel "All except an_area", :on => :all, :except => :an_area do
58
+ "ALL-EXCEPT-AN-AREA"
59
+ end
60
+ end
61
+ get '/'
62
+ assert !last_response.body.include?("THIS-IS-A-TEST")
63
+ assert last_response.body.include?("ALL-EXCEPT-AN-AREA")
64
+
65
+ get '/blah'
66
+ assert last_response.body.include?("THIS-IS-A-TEST")
67
+ assert !last_response.body.include?("ALL-EXCEPT-AN-AREA")
68
+
69
+ get '/blah/test'
70
+ assert last_response.body.include?("THIS-IS-A-TEST")
71
+ assert !last_response.body.include?("ALL-EXCEPT-AN-AREA")
72
+ end
73
+
49
74
  def test_area_inclusion
50
75
  @rules = Proc.new do
51
76
  area :an_area, "/blah", "/blah/test"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-track
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Baldry
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-24 00:00:00 +01:00
18
+ date: 2011-09-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency