Sha256: 665d820604e377f2d43122e59379c4cd34ee13ec87b4feca89fe84b32fa95b37

Contents?: true

Size: 1.95 KB

Versions: 24

Compression:

Stored size: 1.95 KB

Contents

class Rad::Router::BasicRouter
  include Rad::Router::AbstractRouter
  inject workspace: :workspace, logger: :logger
  
  attr_accessor :skip_routes, :redirect_routes
  
  SKIP_DEFAULT = [/^#{rad.http.url_root}\/(favicon|fs|packaged)/]

  def initialize        
    self.skip_routes = SKIP_DEFAULT.clone
    self.redirect_routes = []
  end
  
  def skip path_regexp
    path_regexp.must_be.a Regexp
    skip_routes << path_regexp unless skip_routes.include? path_regexp
  end
  
  def redirect path_regexp, processor
    path_regexp.must_be.a Regexp
    processor.must_be.a String, Proc
    redirect_routes << [path_regexp, processor] unless redirect_routes.include? [path_regexp, processor]
  end
  
  def encode *a
  end
    
  def decode path, params
    # skip
    if skip_routes.any?{|path_regexp| path_regexp =~ path}
      logger.info "RAD skipping '#{path}'"
      throw :halt, true
    end
    
    # redirect
    if meta = redirect_routes.find{|path_regexp, processor| path_regexp =~ path}
      path_regexp, processor = meta          
      target = if processor.is_a? String
        path.sub(path_regexp, processor)
      else
        processor.call path
      end
      
      response = workspace.response
      # content_type = Mime[params.format]

      response.set!(
        status:       :redirect, 
        # content_type: content_type,
        body:         %(<html><body>You are being <a href="#{target.html_escape if target}">redirected</a>.</body></html>)
      )
      response.headers['Location'] = target

      throw :halt, true
    end
  end
end


Rad::Router::Configurator.class_eval do
  def skip *a, &b
    basic_router = @router.routers[:basic_router]
    raise "There's no BasicRouter (use config to add it)!" unless basic_router
    basic_router.skip *a, &b
  end
  
  def redirect *a, &b
    basic_router = @router.routers[:basic_router]
    raise "There's no BasicRouter (use config to add it)!" unless basic_router
    basic_router.redirect *a, &b
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rad_core-0.2.6 lib/rad/router/_basic_router.rb
rad_core-0.2.5 lib/rad/router/_basic_router.rb
rad_core-0.2.4 lib/rad/router/_basic_router.rb
rad_core-0.2.3 lib/rad/router/_basic_router.rb
rad_core-0.2.2 lib/rad/router/_basic_router.rb
rad_core-0.2.1 lib/rad/router/_basic_router.rb
rad_core-0.2.0 lib/rad/router/_basic_router.rb
rad_core-0.0.30 lib/rad/router/_basic_router.rb
rad_core-0.0.29 lib/rad/router/_basic_router.rb
rad_core-0.0.28 lib/rad/router/_basic_router.rb
rad_core-0.0.27 lib/rad/router/_basic_router.rb
rad_core-0.0.26 lib/rad/router/_basic_router.rb
rad_core-0.0.25 lib/rad/router/_basic_router.rb
rad_core-0.0.24 lib/rad/router/_basic_router.rb
rad_core-0.0.23 lib/rad/router/_basic_router.rb
rad_core-0.0.22 lib/rad/router/_basic_router.rb
rad_core-0.0.21 lib/rad/router/_basic_router.rb
rad_core-0.0.20 lib/rad/router/_basic_router.rb
rad_core-0.0.19 lib/rad/router/_basic_router.rb
rad_core-0.0.18 lib/rad/router/_basic_router.rb