Sha256: 1644a3c72101e333e6385e051974cb947bd3c41824f980beb2fd17ca29a95713

Contents?: true

Size: 1.65 KB

Versions: 8

Compression:

Stored size: 1.65 KB

Contents

require 'orange/middleware/base'

module Orange::Middleware
  # This middleware handles setting orange.env['route.site_url'] 
  # 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
  class RouteSite < Base
    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
    
    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
orange-0.0.13 lib/orange/middleware/route_site.rb
orange-0.0.12 lib/orange/middleware/route_site.rb
orange-0.0.10 lib/orange/middleware/route_site.rb
orange-0.0.8 lib/orange/middleware/route_site.rb
orange-0.0.7 lib/orange/middleware/route_site.rb
orange-0.0.6 lib/orange/middleware/route_site.rb
orange-0.0.5 lib/orange/middleware/route_site.rb
orange-0.0.4 lib/orange/middleware/route_site.rb