Class: Roda::RodaPlugins::RestApi::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/rest_api.rb

Constant Summary collapse

POST_BODY =
'rack.input'.freeze
FORM_INPUT =
'rack.request.form_input'.freeze
FORM_HASH =
'rack.request.form_hash'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, request, parent, options = {}) ⇒ Resource

Returns a new instance of Resource.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/roda/plugins/rest_api.rb', line 21

def initialize(path, request, parent, options={})
	@request = request
	@path = path.to_s
	bare = options.delete(:bare) || false
	@singleton = options.delete(:singleton) || false
	@primary_key = options.delete(:primary_key) || "id"
	@parent_key = options.delete(:parent_key) || "parent_id"
	@content_type = options.delete(:content_type) || APPLICATION_JSON
	if parent
		@parent = parent
		@path = [':d', @path].join('/') unless bare
	end
end

Instance Attribute Details

#capturesObject

Returns the value of attribute captures.



19
20
21
# File 'lib/roda/plugins/rest_api.rb', line 19

def captures
  @captures
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



18
19
20
# File 'lib/roda/plugins/rest_api.rb', line 18

def content_type
  @content_type
end

#parentObject (readonly)

Returns the value of attribute parent.



18
19
20
# File 'lib/roda/plugins/rest_api.rb', line 18

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/roda/plugins/rest_api.rb', line 18

def path
  @path
end

#requestObject (readonly)

Returns the value of attribute request.



18
19
20
# File 'lib/roda/plugins/rest_api.rb', line 18

def request
  @request
end

#singletonObject (readonly)

Returns the value of attribute singleton.



18
19
20
# File 'lib/roda/plugins/rest_api.rb', line 18

def singleton
  @singleton
end

Instance Method Details

#delete(&block) ⇒ Object



50
51
52
53
54
# File 'lib/roda/plugins/rest_api.rb', line 50

def delete(&block)
	@delete = block if block
	@content_type = nil
	@delete || ->(_){raise NotImplementedError, "delete"}
end

#list(&block) ⇒ Object



35
36
37
38
# File 'lib/roda/plugins/rest_api.rb', line 35

def list(&block)
	@list = block if block
	@list || ->(_){raise NotImplementedError, "list"}
end

#one(&block) ⇒ Object



40
41
42
43
# File 'lib/roda/plugins/rest_api.rb', line 40

def one(&block)
	@one = block if block
	@one || ->(_){raise NotImplementedError, "one"}
end

#perform(method, id = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/roda/plugins/rest_api.rb', line 84

def perform(method, id = nil)
	begin
		args = self.arguments(method)
		args.merge!(@primary_key.to_sym => id) if id
		args.merge!(@parent_key.to_sym => @captures[0]) if @captures
		self.send(method).call(args)
	rescue StandardError => e
		raise if ENV['RACK_ENV'] == 'development'
		@request.response.status = method === :save ? 422 : 404
		@request.response.write e
	end
end

#permit(*permitted) ⇒ Object



67
68
69
# File 'lib/roda/plugins/rest_api.rb', line 67

def permit(*permitted)
	@permitted = permitted
end

#routes(*routes) ⇒ Object



61
62
63
64
65
# File 'lib/roda/plugins/rest_api.rb', line 61

def routes(*routes)
	routes! if @routes
	yield if block_given?
	@routes = routes
end

#routes!Object



71
72
73
74
75
76
77
# File 'lib/roda/plugins/rest_api.rb', line 71

def routes!
	unless @routes
		@routes = SINGLETON_ROUTES.dup
		@routes << :index unless @singleton
	end
	@routes.each { |route| @request.send(route) }
end

#save(&block) ⇒ Object



45
46
47
48
# File 'lib/roda/plugins/rest_api.rb', line 45

def save(&block)
	@save = block if block
	@save || ->(_){raise NotImplementedError, "save"}
end

#serialize(&block) ⇒ Object



56
57
58
59
# File 'lib/roda/plugins/rest_api.rb', line 56

def serialize(&block)
	@serialize = block if block
	@serialize || ->(obj){obj.is_a?(String) ? obj : obj.send(:to_json)}
end