Class: Watts::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/watts.rb

Overview

HTTP is all about resources, and this class represents them. You’ll want to subclass it and then define some HTTP methods on it, then use your application’s resource method to tell it where to find these resources. (See Watts::App.resource().) If you want your resource to respond to GET with a cheery, text/plain greeting, for example: class Foo < Watts::Resource get { || “Hello, world!” } end

Or you could do something odd like this: class RTime < Watts::Resource class << self; attr_accessor :last_post_time; end

get { || “The last POST was #last_post_time.” } post { || self.class.last_post_time = Time.now.strftime(‘%F %R’) [204, {}, []] }

def last_post_time self.class.last_post_time || “…never” end end

It is also possible to define methods in the usual way (e.g., ‘def get …’), although you’ll need to add them to the list of allowed methods (for OPTIONS) manually. Have a look at the README and doc/examples.

Direct Known Subclasses

HTMLViewResource

Constant Summary collapse

HTTPMethods =
Set.new(%i(get post put delete head options trace connect))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Resource

Every resource, on being instantiated, is given the Rack env.



288
289
290
291
292
293
# File 'lib/watts.rb', line 288

def initialize(env)
	if app = env.delete(:watts_app)
		self.app = app
	end
	self.env = env
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



285
286
287
# File 'lib/watts.rb', line 285

def app
  @app
end

#envObject

Returns the value of attribute env.



285
286
287
# File 'lib/watts.rb', line 285

def env
  @env
end

Class Method Details

.auto_headObject

This method generates a HEAD that just calls #get and only passes back the headers. It’s sub-optimal when GET is expensive, so it is disabled by default.



276
277
278
279
280
281
# File 'lib/watts.rb', line 276

def self.auto_head
	head { |*a|
		status, headers, = get(*a)
		[status, headers, []]
	}
end

.for_html_view(klass, method) ⇒ Object

This method is for creating Resources that simply wrap first-class HTML views. It was created with Hoshi in mind, although you can use any class that can be instantiated and render some HTML when the specified method is called. It takes two arguments: the view class, and the method to call to render the HTML.



266
267
268
269
270
271
# File 'lib/watts.rb', line 266

def self.for_html_view klass, method
	c = Class.new HTMLViewResource
	c.view_class = klass
	c.view_method = method
	c
end

Instance Method Details

#default_http_method(*args) ⇒ Object

By default, we return “405 Method Not Allowed” and set the Allow: header appropriately.



316
317
318
319
320
321
322
323
324
# File 'lib/watts.rb', line 316

def default_http_method(*args)
	s = 'Method not allowed.'
	[405,
		{ 'Allow' => http_methods.join(', '),
		  'Content-Type' => 'text/plain',
		  'Content-Length' => s.bytesize.to_s,
		},
		['Method not allowed.']]
end

#head(*args) ⇒ Object

HEAD responds the same way as the default_method, but does not return a body.



328
329
330
331
332
# File 'lib/watts.rb', line 328

def head(*args)
	r = default_http_method
	r[2] = []
	r
end

#options(*args) ⇒ Object

The default options method, to comply with RFC 2616, returns a list of allowed methods in the Allow header. These are filled in when the method-defining methods (i.e., get() et al) are called.



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/watts.rb', line 298

def options(*args)
	[
		200,
		{
			'Content-Length' => '0', # cf. RFC 2616
			'Content-Type' => 'text/plain', # Appease Rack::Lint
			'Allow' => http_methods.join(', ')
		},
		[]
	]
end

#requestObject



310
311
312
# File 'lib/watts.rb', line 310

def request
	@request ||= Rack::Request.new(env)
end