Class: Rack::Zetetic::Campaign
- Inherits:
-
Object
- Object
- Rack::Zetetic::Campaign
- Defined in:
- lib/rack/zetetic/campaign.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
-
#campaign_file ⇒ Object
Returns the value of attribute campaign_file.
-
#campaigns ⇒ Object
Returns the value of attribute campaigns.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(*args) {|_self| ... } ⇒ Campaign
constructor
A new instance of Campaign.
Constructor Details
#initialize(*args) {|_self| ... } ⇒ Campaign
Returns a new instance of Campaign.
25 26 27 28 29 30 |
# File 'lib/rack/zetetic/campaign.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_file ⇒ Object
Returns the value of attribute campaign_file.
19 20 21 |
# File 'lib/rack/zetetic/campaign.rb', line 19 def campaign_file @campaign_file end |
#campaigns ⇒ Object
Returns the value of attribute campaigns.
20 21 22 |
# File 'lib/rack/zetetic/campaign.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.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 |