Class: Beeta::Resource::Generic

Inherits:
Watts::Resource
  • Object
show all
Includes:
Beeta
Defined in:
lib/beeta/resource.rb

Overview

The Generic resource, with some utility methods for the other Beeta resources.

Direct Known Subclasses

App, AppList, Discovery, User, UserList

Constant Summary

Constants included from Beeta

VERSION

Instance Method Summary collapse

Methods included from Beeta

init

Instance Method Details

#error_404Object



13
14
15
# File 'lib/beeta/resource.rb', line 13

def error_404
	@error_404 ||= json_resp({'error'=>'Not Found'}, 404)
end

#json_bodyObject

Returns the req_body, run through the JSON parser. Returns nil if we can’t parse it.



24
25
26
27
28
29
30
31
# File 'lib/beeta/resource.rb', line 24

def json_body
	@json_body ||=
		begin
			JSON.parse(req_body)
		rescue JSON::ParserError
			nil
		end
end

#json_error(str, status = 400) ⇒ Object



42
43
44
# File 'lib/beeta/resource.rb', line 42

def json_error str, status = 400
	json_resp({'error' => str}, status)
end

#json_resp(body, status = 200) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/beeta/resource.rb', line 33

def json_resp body, status = 200
	js = JSON.unparse body
	[status,
		{ 'Content-Type' => 'application/json',
		  'Content-Length' => js.size.to_s,
		},
	 [js]]
end

#req_bodyObject

Returns the body given with the request, as with a POST or PUT.



18
19
20
# File 'lib/beeta/resource.rb', line 18

def req_body
	@req_body ||= env['rack.input'].read
end