Class: Webmachine::Adapters::Rack

Inherits:
Webmachine::Adapter show all
Defined in:
lib/webmachine/adapters/rack.rb

Overview

A minimal “shim” adapter to allow Webmachine to interface with Rack. The intention here is to allow Webmachine to run under Rack-compatible web-servers, like unicorn and pow, and is not intended to allow Webmachine to be “plugged in” to an existing Rack app as middleware.

To use this adapter, create a config.ru file and populate it like so:

require 'webmachine/adapters/rack'

# put your own Webmachine resources in another file:
require 'my/resources'

run MyApplication.adapter

Servers like pow and unicorn will read config.ru by default and it should all “just work”.

And for development or testing your application can be run with Rack's builtin Server identically to the Mongrel and WEBrick adapters with:

MyApplication.run

Defined Under Namespace

Classes: RackResponse, RequestBody

Constant Summary

DEFAULT_OPTIONS =

Used to override default Rack server options (useful in testing)

{}

Instance Attribute Summary

Attributes inherited from Webmachine::Adapter

#application

Instance Method Summary (collapse)

Methods inherited from Webmachine::Adapter

#initialize, run

Constructor Details

This class inherits a constructor from Webmachine::Adapter

Instance Method Details

- (Object) call(env)

Handles a Rack-based request.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/webmachine/adapters/rack.rb', line 55

def call(env)
  headers = Webmachine::Headers.from_cgi(env)

  rack_req = ::Rack::Request.new env
  request = Webmachine::Request.new(rack_req.request_method,
                                    URI.parse(rack_req.url),
                                    headers,
                                    RequestBody.new(rack_req))

  response = Webmachine::Response.new
  application.dispatcher.dispatch(request, response)

  response.headers['Server'] = [Webmachine::SERVER_STRING, "Rack/#{::Rack.version}"].join(" ")

  rack_status  = response.code
  rack_headers = response.headers.flattened("\n")
  rack_body = case response.body
              when String # Strings are enumerable in ruby 1.8
                [response.body]
              else
                if response.body.respond_to?(:call)
                  Webmachine::ChunkedBody.new(Array(response.body.call))
                elsif response.body.respond_to?(:each)
                  # This might be an IOEncoder with a Content-Length, which shouldn't be chunked.
                  if response.headers["Transfer-Encoding"] == "chunked"
                    Webmachine::ChunkedBody.new(response.body)
                  else
                    response.body
                  end
                else
                  [response.body.to_s]
                end
              end

  rack_res = RackResponse.new(rack_body, rack_status, rack_headers)
  rack_res.finish
end

- (Object) run

Start the Rack adapter



38
39
40
41
42
43
44
45
46
47
# File 'lib/webmachine/adapters/rack.rb', line 38

def run
  options = DEFAULT_OPTIONS.merge({
    :app => self,
    :Port => application.configuration.port,
    :Host => application.configuration.ip
  }).merge(application.configuration.adapter_options)

  @server = ::Rack::Server.new(options)
  @server.start
end

- (Object) shutdown



49
50
51
# File 'lib/webmachine/adapters/rack.rb', line 49

def shutdown
  @server.server.shutdown if @server
end