Sha256: 9af704d5107ae1f480e5033613f69447d1875c11a6d39782ff57c54371571b2c

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module Tennpipes
  class Filter
    attr_reader :block

    def initialize(mode, scoped_controller, options, args, &block)
      @mode = mode
      @scoped_controller = scoped_controller
      @options = options
      @args = args
      @block = block
    end

    def apply?(request)
      detect = match_with_arguments?(request) || match_with_options?(request)
      detect ^ !@mode
    end

    def to_proc
      if @args.empty? && @options.empty?
        block
      else
        filter = self
        proc { instance_eval(&filter.block) if filter.apply?(request) }
      end
    end

    private

    def scoped_controller_name
      @scoped_controller_name ||= Array(@scoped_controller).join("_")
    end

    def match_with_arguments?(request)
      route = request.route_obj
      path = request.path_info
      @args.any? do |argument|
        if argument.instance_of?(Symbol)
          next unless route
          name = route.name
          argument == name || name == [scoped_controller_name, argument].join(" ").to_sym
        else
          argument === path
        end
      end
    end

    def match_with_options?(request)
      user_agent = request.user_agent
      @options.any?{|name, value| value === (name == :agent ? user_agent : request.send(name)) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tennpipes-base-3.6.6 lib/tennpipes-base/filter.rb