Sha256: 1a713c88ff3c0f3ff7abf8cf2315ee9e127d9c20097be2bdafd34638f48a7050

Contents?: true

Size: 1.24 KB

Versions: 27

Compression:

Stored size: 1.24 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 => [:preview, :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

27 entries across 27 versions & 2 rubygems

Version Path
orange-core-0.7.1 lib/orange-core/middleware/route_context.rb
orange-core-0.7.0 lib/orange-core/middleware/route_context.rb
orange-core-0.6.0 lib/orange-core/middleware/route_context.rb
orange-core-0.5.9 lib/orange-core/middleware/route_context.rb
orange-core-0.5.8 lib/orange-core/middleware/route_context.rb
orange-core-0.5.5 lib/orange-core/middleware/route_context.rb
orange-core-0.5.3 lib/orange-core/middleware/route_context.rb
orange-0.5.5 lib/orange-core/middleware/route_context.rb
orange-0.5.4 lib/orange-core/middleware/route_context.rb
orange-0.5.3 lib/orange-core/middleware/route_context.rb
orange-0.5.2 lib/orange-core/middleware/route_context.rb
orange-0.5.1 lib/orange-core/middleware/route_context.rb
orange-0.4.0 lib/orange-core/middleware/route_context.rb
orange-0.3.9 lib/orange-core/middleware/route_context.rb
orange-0.3.8 lib/orange-core/middleware/route_context.rb
orange-0.3.7 lib/orange-core/middleware/route_context.rb
orange-0.3.6 lib/orange-core/middleware/route_context.rb
orange-0.3.5 lib/orange-core/middleware/route_context.rb
orange-0.3.4 lib/orange-core/middleware/route_context.rb
orange-0.3.3 lib/orange-core/middleware/route_context.rb