Class: Webmachine::Resource

Inherits:
Object
  • Object
show all
Includes:
Callbacks, Encodings, EntityTags, Tracing
Defined in:
lib/webmachine/resource.rb,
lib/webmachine/resource/tracing.rb,
lib/webmachine/resource/callbacks.rb,
lib/webmachine/resource/encodings.rb,
lib/webmachine/resource/entity_tags.rb,
lib/webmachine/resource/authentication.rb

Overview

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 src="http://webmachine.basho.com/images/http-headers-status-v3.png" />.

Direct Known Subclasses

Test::Resource, Trace::TraceResource

Defined Under Namespace

Modules: Authentication, Callbacks, Encodings, EntityTags, Tracing

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Tracing

#trace?

Methods included from EntityTags

#weak_etag

Methods included from Encodings

#encode_deflate, #encode_gzip, #encode_identity

Methods included from Callbacks

#allow_missing_post?, #allowed_methods, #base_uri, #charsets_provided, #content_types_accepted, #content_types_provided, #create_path, #delete_completed?, #delete_resource, #encodings_provided, #expires, #finish_request, #forbidden?, #generate_etag, #handle_exception, #is_authorized?, #is_conflict?, #known_content_type?, #known_methods, #language_chosen, #languages_provided, #last_modified, #malformed_request?, #moved_permanently?, #moved_temporarily?, #multiple_choices?, #options, #post_is_create?, #previously_existed?, #process_post, #resource_exists?, #service_available?, #uri_too_long?, #valid_content_headers?, #valid_entity_length?, #validate_content_checksum, #variances

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



29
30
31
# File 'lib/webmachine/resource.rb', line 29

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



29
30
31
# File 'lib/webmachine/resource.rb', line 29

def response
  @response
end

Class Method Details

.new(request, response) ⇒ Resource

Creates a new Webmachine::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.

Parameters:

  • request (Request)

    the request object

  • response (Response)

    the response object

Returns:



38
39
40
41
42
43
44
# 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

.runvoid

This method returns an undefined value.

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



52
53
54
55
56
57
58
59
# 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