Sha256: 93fd4984933aa8a1cc0fd2d14b81e1fadcf68a2aaa286b3b6fa3a470a402e53b

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

# TODO: Add support of this: https://github.com/andrewberls/regularity
module Walle
  class Robot
    class Router
      class Route
        attr_reader :controller, :regexp, :options

        class Mutator
          attr_reader :env, :regexp, :options

          def initialize(env, regexp, options = {})
            @env = env
            @regexp = regexp
            @options = options
          end
        end

        class Prefix < Mutator
          def mutate
            return regexp unless prefix

            if regexp
              Regexp.new("#{prefix}#{regexp.source}")
            else
              Regexp.new(prefix)
            end
          end

          def prefix
            return if options[:prefix] === false
            options[:prefix] || '.*'
          end
        end

        class Direct < Mutator
          def mutate
            return regexp unless direct?

            robot_name = env.client.self.id
            expr = regexp || /.*/
            Regexp.new("<@#{robot_name}>\\s+#{expr.source}")
          end

          def direct?
            options[:direct]
          end
        end

        MUTATORS = [Direct, Prefix]

        def initialize(controller:, regexp: nil, **options)
          @controller = controller
          @options = options
          @regexp = regexp
        end

        def match?(env)
          expr = final_regexp(env)
          !!expr.match(env.data.text)
        end

        def call(env)
          expr = final_regexp(env)
          env.matches = expr.match(env.data.text) if expr
          cntrl.respond_to?(:call) ? cntrl.call(env) : cntrl.new(env).call
        end

        alias :cntrl :controller

        private

        def final_regexp(env)
          MUTATORS.reduce(regexp) { |result, mutator| mutator.new(env, result, options).mutate }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
walle-0.1.0 lib/walle/robot/router/route.rb