Class: Rack::Adapter::Rails

Class Rack::Adapter::Rails < Object

(in files lib/rack/adapter/rails.rb )

Adapter to run a Rails app with any supported Rack handler. By default it will try to load the Rails application in the current directory in the development environment.

Options:

 root: Root directory of the Rails app
 environment: Rails environment to run in (development [default], production or test)
 prefix: Set the relative URL root.

Based on fuzed.rubyforge.org/ Rails adapter

Methods

Public Class new(options={})

    # File lib/rack/adapter/rails.rb, line 18
18:       def initialize(options={})
19:         @root   = options[:root]         || Dir.pwd
20:         @env    = options[:environment]  || 'development'
21:         @prefix = options[:prefix]
22:         
23:         load_application
24:         
25:         @file_server = Rack::File.new(::File.join(RAILS_ROOT, "public"))
26:       end

Public Instance call(env)

    # File lib/rack/adapter/rails.rb, line 59
59:       def call(env)
60:         path        = env['PATH_INFO'].chomp('/')
61:         method      = env['REQUEST_METHOD']
62:         cached_path = (path.empty? ? 'index' : path) + ActionController::Base.page_cache_extension
63:         
64:         if FILE_METHODS.include?(method)
65:           if file_exist?(path)              # Serve the file if it's there
66:             return serve_file(env)
67:           elsif file_exist?(cached_path)    # Serve the page cache if it's there
68:             env['PATH_INFO'] = cached_path
69:             return serve_file(env)
70:           end
71:         end
72:         
73:         # No static file, let Rails handle it
74:         serve_rails(env)
75:       end

Public Instance file_exist?(path)

TODO refactor this in File#can_serve?(path) ??

    # File lib/rack/adapter/rails.rb, line 38
38:       def file_exist?(path)
39:         full_path = ::File.join(@file_server.root, Utils.unescape(path))
40:         ::File.file?(full_path) && ::File.readable_real?(full_path)
41:       end

Public Instance load_application()

    # File lib/rack/adapter/rails.rb, line 28
28:       def load_application
29:         ENV['RAILS_ENV'] = @env
30: 
31:         require "#{@root}/config/environment"
32:         require 'dispatcher'
33:         
34:         ActionController::AbstractRequest.relative_url_root = @prefix if @prefix 
35:       end

Public Instance serve_file(env)

    # File lib/rack/adapter/rails.rb, line 43
43:       def serve_file(env)
44:         @file_server.call(env)
45:       end

Public Instance serve_rails(env)

    # File lib/rack/adapter/rails.rb, line 47
47:       def serve_rails(env)
48:         request         = Request.new(env)
49:         response        = Response.new
50:         
51:         session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
52:         cgi             = CGIWrapper.new(request, response)
53:     
54:         Dispatcher.dispatch(cgi, session_options, response)
55: 
56:         response.finish
57:       end