Sha256: 84e168c87a42eeaf5b830a4b20a280d49119bed8f5b49f8cfcefa097a8a933b0

Contents?: true

Size: 1.23 KB

Versions: 12

Compression:

Stored size: 1.23 KB

Contents

require 'orange-core/middleware/base'

module Orange::Middleware
  # This middleware handles setting orange.env['route.context'] 
  # to a value based on the route, if any. The route is then 
  # trimmed before continuing on.
  class RouteContext < Base
    def initialize(app, core, *args)
      opts = args.extract_options!
      opts.with_defaults!(:contexts => [:live, :admin, :orange], 
                          :default => :live,
                          :urls => {})
      @app = app
      @core = core
      @contexts = opts[:contexts]
      @default = opts[:default]
      @urls = opts[:urls]
    end
    def packet_call(packet)
      path_info = packet['route.path'] || packet.env['PATH_INFO']
      path = path_info.split('/')
      pad = path.shift # Shift off empty first part
      if @urls[packet.request.host]
        packet['route.context'] = urls[packet.request.host]
      elsif path.empty?
        packet['route.context'] = @default
      else
        if(@contexts.include?(path.first.to_sym))
          packet['route.context'] = path.shift.to_sym
          path.unshift(pad)
          packet['route.path'] = path.join('/')
        else
          packet['route.context'] = @default
        end
      end
      pass packet
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
orange-0.2.6 lib/orange-core/middleware/route_context.rb
orange-0.2.5 lib/orange-core/middleware/route_context.rb
orange-0.2.4 lib/orange-core/middleware/route_context.rb
orange-0.2.3 lib/orange-core/middleware/route_context.rb
orange-0.1.10 lib/orange-core/middleware/route_context.rb
orange-0.1.8 lib/orange-core/middleware/route_context.rb
orange-0.1.7 lib/orange-core/middleware/route_context.rb
orange-0.1.4 lib/orange-core/middleware/route_context.rb
orange-0.1.2 lib/orange-core/middleware/route_context.rb
orange-0.1.0 lib/orange-core/middleware/route_context.rb
orange-0.0.15 lib/orange-core/middleware/route_context.rb
orange-0.0.14 lib/orange-core/middleware/route_context.rb