Class: Orange::Middleware::RouteContext

Inherits:
Base show all
Defined in:
lib/orange-core/middleware/route_context.rb

Overview

This middleware handles setting orange.env to a value based on the route, if any. The route is then trimmed before continuing on.

Instance Method Summary collapse

Methods inherited from Base

#call, #init, #inspect, #orange, #pass, #recapture

Constructor Details

#initialize(app, core, *args) ⇒ RouteContext

Returns a new instance of RouteContext.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/orange-core/middleware/route_context.rb', line 8

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

Instance Method Details

#packet_call(packet) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/orange-core/middleware/route_context.rb', line 19

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