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

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

Instance Method Summary collapse

Instance Method Details

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



165
166
167
168
169
170
171
# File 'lib/roda/plugins/rest_api.rb', line 165

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

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



198
199
200
201
202
203
204
# File 'lib/roda/plugins/rest_api.rb', line 198

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



212
213
214
215
216
217
218
# File 'lib/roda/plugins/rest_api.rb', line 212

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



220
221
222
223
# File 'lib/roda/plugins/rest_api.rb', line 220

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

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



188
189
190
191
# File 'lib/roda/plugins/rest_api.rb', line 188

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

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



225
226
227
228
# File 'lib/roda/plugins/rest_api.rb', line 225

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

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



177
178
179
180
181
182
183
184
185
186
# File 'lib/roda/plugins/rest_api.rb', line 177

def resource(path, options={})
	@resource = Resource.new(path, self, @resource, options)
	on(@resource.path, options) do
		@resource.captures = captures.dup unless captures.empty?
		yield @resource
	 	@resource.routes!
		response.status = 404
  end
 	@resource = @resource.parent
end

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



193
194
195
196
# File 'lib/roda/plugins/rest_api.rb', line 193

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

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



206
207
208
209
210
# File 'lib/roda/plugins/rest_api.rb', line 206

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

#version(version, &block) ⇒ Object



173
174
175
# File 'lib/roda/plugins/rest_api.rb', line 173

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