Sha256: 1ace24579e66693dce729994d7fb09597f91cfd74e30a2716b2a404e8ac1d9b7

Contents?: true

Size: 780 Bytes

Versions: 2

Compression:

Stored size: 780 Bytes

Contents

module Vundabar
  module Routing
    class Mapper
      attr_reader :endpoints, :request
      def initialize(endpoints)
        @endpoints = endpoints
      end

      def find_route(request)
        @request = request
        path = request.path_info
        method = request.request_method.downcase.to_sym
        endpoints[method].detect do |endpoint|
          match_path_with_endpoint path, endpoint
        end
      end

      def match_path_with_endpoint(path, endpoint)
        regex, placeholders = endpoint[:pattern]
        if regex =~ path
          match_data = Regexp.last_match
          placeholders.each do |placeholder|
            request.update_param(placeholder, match_data[placeholder])
          end
          true
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vundabar-0.2.0 lib/vundabar/routing/mapper.rb
vundabar-0.1.0 lib/vundabar/routing/mapper.rb