Class: Orange::Middleware::RouteSite

Inherits:
Base show all
Defined in:
lib/orange-core/middleware/route_site.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.

Options -

:multi   - does Orange need to handle multiple urls
:fake_it - host url(s) that Orange will fake requests on
            ex: :multi => true, :fake_it => 'localhost'
                will fake hostnames as first component of url
                only on localhost

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Returns a new instance of RouteSite.



15
16
17
18
19
20
21
22
23
24
# File 'lib/orange-core/middleware/route_site.rb', line 15

def initialize(app, core, *args)
  opts = args.extract_options!
  opts.with_defaults!(:multi => false, :fake_it => ['localhost'])
  @app = app
  @core = core
  @multi = opts[:multi]
  # Put fake_it into an array, if necessary
  @fake_it = opts[:fake_it].respond_to?(:include?) ? 
    opts[:fake_it] : [opts[:fake_it]]
end

Instance Method Details

#packet_call(packet) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/orange-core/middleware/route_site.rb', line 26

def packet_call(packet)
  request = packet.request
  path_info = packet['route.path'] || packet.env['PATH_INFO']
  path = path_info.split('/')
  pad = path.shift # Shift off empty first part
  packet['route.faked_site'] = false
  if @multi
    if path.empty?
      packet['route.site_url'] = request.host
    else
      if @fake_it.include?(request.host)
        packet['route.site_url'] = path.shift
        packet['route.faked_site'] = true
      else
        packet['route.site_url'] = request.host
      end
      path.unshift(pad)
      packet['route.path'] = path.join('/')
    end
  else
    packet['route.site_url'] = request.host
  end
  pass packet
end