Sha256: 8e693dd4d5c9d083485a8d7f123d568370b32ca22f18dd7888974fac06084034

Contents?: true

Size: 886 Bytes

Versions: 2

Compression:

Stored size: 886 Bytes

Contents

module Rollerskates
  module Routing
    class Mapper
      def initialize(endpoints)
        @endpoints = endpoints
      end

      def map_to_route(request)
        @request = request
        path = request.path_info
        path[-1] = "" if path[-1] == "/" && path != "/"
        method = request.request_method.downcase.to_sym
        result = @endpoints[method].detect do |endpoint|
          match_path_with_pattern path, endpoint
        end
        return Route.new(@request, result[:klass_and_method]) if result
      end

      def match_path_with_pattern(path, endpoint)
        regexp, placeholders = endpoint[:pattern]
        if path =~ regexp
          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
rollerskates-0.1.1 lib/rollerskates/routing/mapper.rb
rollerskates-0.1.0 lib/rollerskates/routing/mapper.rb