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

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

Instance Method Summary collapse

Instance Method Details

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



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

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



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

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



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

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



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

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

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



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

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

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



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

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

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



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

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



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

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

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



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

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

#version(version, &block) ⇒ Object



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

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