class Rack::Redir

Bounce those annoying favicon.ico requests

Public Class Methods

new(app) click to toggle source
# File lib/rack/redir.rb, line 6
def initialize(app)
  @app = app
end

Public Instance Methods

append_parameter_separater(url) click to toggle source
# File lib/rack/redir.rb, line 43
def append_parameter_separater url
  url.match('\?') ? "#{url}&" : "#{url}?"
end
call(env) click to toggle source
# File lib/rack/redir.rb, line 10
def call(env)
  env['PATH_INFO'] =~ %r{^a/(.+)$}
  parsed_path = env['PATH_INFO'].split(/\//)
  if "#{parsed_path.second}/" == Redirect.path
    redir_asset = parsed_path.last 
    redirect_record = (Redirect.where shortened_id: redir_asset).to_a.last
    headers = env.select {|k,v| k.start_with? 'HTTP_'}
    if redirect_record and redirect_record.shortened_id == redir_asset
      redirect_url = redirect_record.original_url.strip 
      redirect_to_url = redirect_url.start_with?('http') ? redirect_url : "http://#{redirect_url}"
      redirect_log = RedirectLog.new(:redirect_id => redirect_record.id, 
                                     :headers => headers, :request_time => Time.now)
      redirect_log.save!
      if Redirect.append_tracking_info
        redirect_to_url = append_parameter_separater redirect_to_url
        redirect_to_url = "#{redirect_to_url}redirect_mongo_id=" +
                          "#{redirect_log.redirect_id}&redirect_log_mongo_id=#{redirect_log._id}"
      end
      if Redirect.append_google_analytics
        redirect_to_url = append_parameter_separater redirect_to_url 
        redirect_to_url = "#{redirect_to_url}utm_source=#{Rack::Utils.escape Redirect.host}" +
                          "&utm_campaign=#{redirect_log.redirect_id}__#{redirect_log._id}"
      end  
      [302, {'Location' => redirect_to_url}, self]
    else
      [404, {}, self]
    end
  else
    @app.call env
  end

end
each(&block) click to toggle source
# File lib/rack/redir.rb, line 47
def each(&block)
end