Sha256: c2e6cf1fa67413f0d73fd3ba8ea5567dc6a1a7d624eb07c9a2996387bc3a24dd

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

module Workarea
  module BasicAuth
    class Path
      attr_reader :regexp

      def initialize(string, *http_methods)
        path = Regexp.escape(string).gsub('\*', ".*?")
        @regexp = Regexp.new("^#{path}$", true)

        if http_methods && http_methods.first.is_a?(Proc)
          @proc = http_methods.first
        end

        @http_methods = http_methods
      end

      def matches?(request)
        path_matches?(request) && request_matches?(request)
      end

      private

        def path_matches?(request)
          !@regexp.match(request.path_info).nil?
        end

        def request_matches?(request)
          if @proc
            @proc.call(request)
          else
            method = request.env["REQUEST_METHOD"].downcase.to_sym
            @http_methods.empty? || @http_methods.include?(method)
          end
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-basic_auth-1.1.2 lib/workarea/basic_auth/path.rb
workarea-basic_auth-1.1.1 lib/workarea/basic_auth/path.rb