Sha256: 196b9ae58e7ba9a96c05a333955adf57cf4c89b05114377622bc92bb27bbc43b

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module Praxis
  class RoutingConfig

    attr_reader :route
    attr_reader :version
    attr_reader :base

    def initialize(version:'n/a'.freeze, base: '', prefix:[], &block)
      @version = version
      @base = base
      @prefix_segments = Array(prefix)

      @route = nil

      if block_given?
        instance_eval(&block)
      end
    end

    def clear!
      @prefix_segments = []
    end

    def prefix(prefix=nil)
      return @prefix_segments.join.gsub('//','/') if prefix.nil?

      case prefix
      when ''
        @prefix_segments = []
      when ABSOLUTE_PATH_REGEX
        @prefix_segments = Array(prefix[1..-1])
      else
        @prefix_segments << prefix
      end
    end

    def options(path, opts={}) add_route 'OPTIONS', path, opts end
    def get(path, opts={})     add_route 'GET',     path, opts end
    def head(path, opts={})    add_route 'HEAD',    path, opts end
    def post(path, opts={})    add_route 'POST',    path, opts end
    def put(path, opts={})     add_route 'PUT',     path, opts end
    def delete(path, opts={})  add_route 'DELETE',  path, opts end
    def trace(path, opts={})   add_route 'TRACE',   path, opts end
    def connect(path, opts={}) add_route 'CONNECT', path, opts end
    def patch(path, opts={})   add_route 'PATCH',   path, opts end
    def any(path, opts={})     add_route 'ANY',     path, opts end

    ABSOLUTE_PATH_REGEX = %r|^//|

    def add_route(verb, path, options={})
      unless path =~ ABSOLUTE_PATH_REGEX
        path = prefix + path
      end
      prefixed_path = path.gsub('//','/')
      path = (base + path).gsub('//','/')
      pattern = Mustermann.new(path, {ignore_unknown_options: true}.merge( options ))
      @route = Route.new(verb, pattern, version, prefixed_path: prefixed_path, **options)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
praxis-2.0.pre.9 lib/praxis/routing_config.rb
praxis-2.0.pre.8 lib/praxis/routing_config.rb
praxis-2.0.pre.7 lib/praxis/routing_config.rb
praxis-2.0.pre.6 lib/praxis/routing_config.rb