Sha256: 08401bd956ad33a5947c3e36339c5523d573bed335043b807650444b2b6a1916

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Grape
  class Router
    class Route < BaseRoute
      extend Forwardable

      attr_reader :app, :request_method

      def_delegators :pattern, :path, :origin

      def initialize(method, pattern, **options)
        @request_method = upcase_method(method)
        @pattern = Grape::Router::Pattern.new(pattern, **options)
        super(**options)
      end

      def exec(env)
        @app.call(env)
      end

      def apply(app)
        @app = app
        self
      end

      def match?(input)
        return false if input.blank?

        options[:forward_match] ? input.start_with?(pattern.origin) : pattern.match?(input)
      end

      def params(input = nil)
        return params_without_input if input.blank?

        parsed = pattern.params(input)
        return {} unless parsed

        parsed.compact.symbolize_keys
      end

      private

      def params_without_input
        pattern.captures_default.merge(attributes.params)
      end

      def upcase_method(method)
        method_s = method.to_s
        Grape::Http::Headers::SUPPORTED_METHODS.detect { |m| m.casecmp(method_s).zero? } || method_s.upcase
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
grape-2.2.0 lib/grape/router/route.rb
grape-2.1.3 lib/grape/router/route.rb
grape-2.1.2 lib/grape/router/route.rb
grape-2.1.1 lib/grape/router/route.rb
grape-2.1.0 lib/grape/router/route.rb