class Webmachine::Resource

Resource is the primary building block of Webmachine applications, and describes families of HTTP resources. It includes all of the methods you might want to override to customize the behavior of the resource. The simplest resource family you can implement looks like this:

class HelloWorldResource < Webmachine::Resource
  def to_html
    "<html><body>Hello, world!</body></html>"
  end
end

For more information about how response decisions are made in Webmachine based on your resource class, refer to the diagram at {}.

Attributes

request[R]
response[R]

Public Class Methods

new(request, response) click to toggle source

Creates a new {Resource}, initializing it with the request and response. Note that you may still override the ‘initialize` method to initialize your resource. It will be called after the request and response ivars are set. @param [Request] request the request object @param [Response] response the response object @return [Resource] the new resource

# File lib/webmachine/resource.rb, line 38
def self.new(request, response)
  instance = allocate
  instance.instance_variable_set(:@request, request)
  instance.instance_variable_set(:@response, response)
  instance.send :initialize
  instance
end
run() click to toggle source

Starts a web server that serves requests for a subclass of Webmachine::Resource.

@return [void]

# File lib/webmachine/resource.rb, line 52
def self.run
  resource = self
  Application.new do |app|
    app.routes do |router|
      router.add [:*], resource
    end
  end.run
end

Private Instance Methods

charset_nop(x) click to toggle source

When no specific charsets are provided, this acts as an identity on the response body. Probably deserves some refactoring.

# File lib/webmachine/resource.rb, line 65
def charset_nop(x)
  x
end