Sha256: f02ed3bd07ab3fe894105ce66a2d2e773450bbddd45c3036ab82ba7266db1ae2

Contents?: true

Size: 823 Bytes

Versions: 1

Compression:

Stored size: 823 Bytes

Contents

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

      def map_to_route(request)
        @request = request
        path   = request.path_info
        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

1 entries across 1 versions & 1 rubygems

Version Path
mgt-0.1.0 lib/routing/mapper.rb