Class: TopholdRack::Redispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/tophold_rack/redispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Redispatcher

Returns a new instance of Redispatcher.



8
9
10
11
# File 'lib/tophold_rack/redispatcher.rb', line 8

def initialize app
  @app = app
  @redis = Rails.configuration.tophold_statistics_redis
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tophold_rack/redispatcher.rb', line 17

def call env
  unless Rails.configuration.tophold_rack_disabled
    request = Rack::Request.new env
    if request.get? && !request.xhr?
      path, query = request.path, request.query_string
      if scope = env["rack.session"]["warden.user.#{Rails.configuration.tophold_rack_devise_scope}.key"]
        user_id = scope[1][0].to_s =~ /\d+/ ? scope[1][0] : scope[0][0]
      else
        user_id = nil
      end
      unless path =~ request_black_list
        begin
          @redis.lpush(Rails.configuration.tophold_statistics_queue,{user_id: user_id, url: path, visited_date: Date.today}.to_json)
        rescue Exception => e
          raise e
          Rails.logger.fatal 'RedisDispatcher can not work!'
        end
      end
    end
  end
  @app.call env #pass the buckets
end

#request_black_listObject



13
14
15
# File 'lib/tophold_rack/redispatcher.rb', line 13

def request_black_list 
  @black_list ||= /^\/(#{Rails.configuration.tophold_rack_request_black_list.join('|')})/
end