Sha256: 1b1dd22cf39aaf55b0645e12c216dc99903f12d1006bc8a4d55b968f2f2d01ee

Contents?: true

Size: 813 Bytes

Versions: 19

Compression:

Stored size: 813 Bytes

Contents

require 'forwardable'

module Jeanine
  class Router
    extend Forwardable

    def_delegators :@routes, :[]

    attr_reader :routes
    def initialize
      @routes = {
          GET: [],
          POST: [],
          PATCH: [],
          PUT: [],
          DELETE: [],
          OPTIONS: [],
          HEAD: [],
      }
    end

    def add(verb, path, options = {}, &block)
      routes[verb] << build_route("#{path}", options, &block)
    end

    private

    def build_route(path, options = {}, &block)
      route = { path: "#{path}", compiled_path: nil, params: [], block: block }
      compiled_path = route[:path].gsub(/:\w+/) do |match|
        route[:params] << match.tr(':', '').to_sym
        '([^/?#]+)'
      end
      route[:compiled_path] = /^#{compiled_path}?$/
      route
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
jeanine-0.8.1 lib/jeanine/router.rb
jeanine-0.8 lib/jeanine/router.rb
jeanine-0.7.8 lib/jeanine/router.rb
jeanine-0.7.7 lib/jeanine/router.rb
jeanine-0.7.6 lib/jeanine/router.rb
jeanine-0.7.5 lib/jeanine/router.rb
jeanine-0.7.4 lib/jeanine/router.rb
jeanine-0.7.3.2 lib/jeanine/router.rb
jeanine-0.7.3.1 lib/jeanine/router.rb
jeanine-0.7.3 lib/jeanine/router.rb
jeanine-0.7.2 lib/jeanine/router.rb
jeanine-0.7.1 lib/jeanine/router.rb
jeanine-0.7.0 lib/jeanine/router.rb
jeanine-0.6.0 lib/jeanine/router.rb
jeanine-0.5.0 lib/jeanine/router.rb
jeanine-0.4.0 lib/jeanine/router.rb
jeanine-0.3.0 lib/jeanine/router.rb
jeanine-0.2.0 lib/jeanine/router.rb
jeanine-0.1.0 lib/jeanine/router.rb