Class: Rack::Zetetic::CampaignLink

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

Constant Summary collapse

UTM_VARS =
{ 
  'campaign' => 'utm_campaign',
  'source' => 'utm_source',
  'medium' => 'utm_medium',
  'term' => 'utm_term',
  'content' => 'utm_content'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ CampaignLink

Returns a new instance of CampaignLink.

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
30
# File 'lib/rack/zetetic/campaign_link.rb', line 25

def initialize *args
  @app = args.shift if args.first.respond_to? :call
  @campaign_file = args.shift
  @campaigns = open( @campaign_file ){ |yf| YAML::load( yf ) }
  yield self if block_given?
end

Instance Attribute Details

#campaign_fileObject

Returns the value of attribute campaign_file.



19
20
21
# File 'lib/rack/zetetic/campaign_link.rb', line 19

def campaign_file
  @campaign_file
end

#campaignsObject

Returns the value of attribute campaigns.



20
21
22
# File 'lib/rack/zetetic/campaign_link.rb', line 20

def campaigns
  @campaigns
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack/zetetic/campaign_link.rb', line 32

def call(env)
  # get a convenient request wrapper from Rack
  req = Rack::Request.new(env)
  id = req.path.split('/').last # take the last path piece as the campaign id        
  if campaign = @campaigns[id]
    # we've got a match!
    destination = campaign['url'] + '?'
    UTM_VARS.each do |key, utm_name|
      destination << "#{utm_name}=#{Rack::Utils.escape(campaign['tokens'][key])}&"
    end
    
    [ 302, { 'Location' => destination, "Content-Type" => "text/plain" }, ['Redirecting...'] ]
  else
    # drop a 404 on the head!
    [ 404, {'Content-Type' => 'text/html'}, ["Page not found"] ]
  end # if
end