Sha256: e1eec7522571447a8ed9979ecb81efa9e9d86a4ccd8f190cc9dc51c09b967c10

Contents?: true

Size: 1.91 KB

Versions: 9

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require 'mustermann/grape'
require 'grape/util/cache'

module Grape
  class Router
    class Pattern
      DEFAULT_PATTERN_OPTIONS   = { uri_decode: true }.freeze
      DEFAULT_SUPPORTED_CAPTURE = %i[format version].freeze

      attr_reader :origin, :path, :pattern, :to_regexp

      extend Forwardable
      def_delegators :pattern, :named_captures, :params
      def_delegators :to_regexp, :===
      alias match? ===

      def initialize(pattern, **options)
        @origin  = pattern
        @path    = build_path(pattern, **options)
        @pattern = Mustermann::Grape.new(@path, **pattern_options(options))
        @to_regexp = @pattern.to_regexp
      end

      private

      def pattern_options(options)
        capture = extract_capture(**options)
        options = DEFAULT_PATTERN_OPTIONS.dup
        options[:capture] = capture if capture.present?
        options
      end

      def build_path(pattern, anchor: false, suffix: nil, **_options)
        unless anchor || pattern.end_with?('*path')
          pattern = +pattern
          pattern << '/' unless pattern.end_with?('/')
          pattern << '*path'
        end

        pattern = -pattern.split('/').tap do |parts|
          parts[parts.length - 1] = '?' + parts.last
        end.join('/') if pattern.end_with?('*path')

        PatternCache[[pattern, suffix]]
      end

      def extract_capture(requirements: {}, **options)
        requirements = {}.merge(requirements)
        DEFAULT_SUPPORTED_CAPTURE.each_with_object(requirements) do |field, capture|
          option = Array(options[field])
          capture[field] = option.map(&:to_s) if option.present?
        end
      end

      class PatternCache < Grape::Util::Cache
        def initialize
          @cache = Hash.new do |h, (pattern, suffix)|
            h[[pattern, suffix]] = -"#{pattern}#{suffix}"
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
grape-1.5.3 lib/grape/router/pattern.rb
grape-1.5.2 lib/grape/router/pattern.rb
grape-1.5.1 lib/grape/router/pattern.rb
grape-1.5.0 lib/grape/router/pattern.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/grape-1.4.0/lib/grape/router/pattern.rb
grape-1.4.0 lib/grape/router/pattern.rb
grape-1.3.3 lib/grape/router/pattern.rb
grape-1.3.2 lib/grape/router/pattern.rb
grape-1.3.1 lib/grape/router/pattern.rb