Module: Roda::RodaPlugins::RestApi::RequestMethods

Defined in:
lib/roda/plugins/rest_api.rb

Instance Method Summary collapse

Instance Method Details

#api(options = {}, &block) ⇒ Object



79
80
81
82
83
84
# File 'lib/roda/plugins/rest_api.rb', line 79

def api(options={}, &block)
	path = options.delete(:path) || 'api'
	subdomain = options.delete(:subdomain)
	options.merge!(host: /\A#{Regexp.escape(subdomain)}\./) if subdomain
	on([path, true], options, &block)
end

#create(options = {}, &block) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/roda/plugins/rest_api.rb', line 109

def create(options={}, &block)
	block ||= ->{@resource.perform(:save)}
post(["", true], options) do
	response.status = 201
	block.call(*captures) if block
end
end

#destroy(options = {}, &block) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/roda/plugins/rest_api.rb', line 123

def destroy(options={}, &block)
block ||= default_block(:delete)
delete(path, options) do
	response.status = 204
	block.call(*captures) if block
end
end

#edit(options = {}, &block) ⇒ Object



131
132
133
134
# File 'lib/roda/plugins/rest_api.rb', line 131

def edit(options={}, &block)
block ||= default_block(:one)
get(path("edit"), options, &block)
end

#index(options = {}, &block) ⇒ Object



99
100
101
102
# File 'lib/roda/plugins/rest_api.rb', line 99

def index(options={}, &block)
 block ||= ->{ @resource.perform(:list) }
get(['', true], options, &block)
end

#new(options = {}, &block) ⇒ Object



136
137
138
139
# File 'lib/roda/plugins/rest_api.rb', line 136

def new(options={}, &block)
 block ||= ->{@resource.perform(:one, "new")}
get("new", options, &block)
end

#resource(name, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/roda/plugins/rest_api.rb', line 90

def resource(name, options={})
	@resource = Resource.new(self, options)
	on(name.to_s, options) do
		yield @resource
  	@resource.routes!
  	response.status = 404
  end
end

#show(options = {}, &block) ⇒ Object



104
105
106
107
# File 'lib/roda/plugins/rest_api.rb', line 104

def show(options={}, &block)
 block ||= default_block(:one)
get(path, options, &block)
end

#update(options = {}, &block) ⇒ Object



117
118
119
120
121
# File 'lib/roda/plugins/rest_api.rb', line 117

def update(options={}, &block)
block ||= default_block(:save)
 options.merge!(method: [:put, :patch])
is(path, options, &block)
end

#version(version, &block) ⇒ Object



86
87
88
# File 'lib/roda/plugins/rest_api.rb', line 86

def version(version, &block)
 		on("v#{version}", &block)
end