Sha256: 62c6c038bb4acc9d300ffd1727202ed9f7dd5a64d2d8b5434613d3afac305792

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Oldskool
  class Router
    def initialize(config)
      @config = config
    end

    def error(msg)
      Oldskool::ErrorHandler.new({}, {:type => :error, :msg => msg}, @config).handle_request(nil, nil)
    end

    def route(params)
      defaulting = false

      if params[:q] =~ /^(\w+?)(\s.+)*$/
        k = $1
        q = $2.strip rescue ""
        default = nil
        handler = nil

        @config[:keywords].each do |keyword|
          default = keyword if [keyword[:keywords]].flatten.include?(:default)

          handler = keyword if [keyword[:keywords]].flatten.include?(k)

          break if handler
        end

        if (!handler && default)
          defaulting = true
          handler = default
        elsif !handler
          return error("No handler for keyword #{k} found and no default handler specified")
        end

        handler_class = "%sHandler" % [handler[:type].to_s.capitalize]

        if Oldskool.constants.include?(handler_class)
          if defaulting
            Oldskool.const_get(handler_class).new(params, handler, @config).handle_request("", params[:q])
          else
            Oldskool.const_get(handler_class).new(params, handler, @config).handle_request(k, q)
          end
        else
          return error("Do not know how to handle type %s keywords" % [ handler[:type] ])
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
oldskool-0.0.2 lib/oldskool/router.rb
oldskool-0.0.1 lib/oldskool/router.rb