module Roda::RodaPlugins::RestApi::RequestMethods

Constants

CONTENT_TYPE

Public Instance Methods

api(options={}, &block) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 204
def api(options={}, &block)
  extract_resource_options options
  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) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 242
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) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 256
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) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 264
def edit(options={}, &block)
  block ||= default_block(:one)
  get(_path('edit'), options, &block)
end
index(options={}, &block) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 232
def index(options={}, &block)
  block ||= ->{ @resource.perform(:list) }
  get(['', true], options, &block)
end
new(options={}, &block) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 269
def new(options={}, &block)
  block ||= ->{@resource.perform(:one, "new")}
  get('new', options, &block)
end
resource(path, options={}) { |resource| ... } click to toggle source
# File lib/roda/plugins/rest_api.rb, line 218
def resource(path, options={})
  extract_resource_options options
  @resource = Resource.new(path, self, @resource, @resource_options)
  on(@resource.path, options) do
    roda_class.symbol_matcher(:id, @resource.id_pattern)
    @resource.captures = captures.dup unless captures.empty?
    yield @resource
    @resource.routes!
    response.status = 404
  end
  @resource_options.pop
  @resource = @resource.parent
end
show(options={}, &block) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 237
def show(options={}, &block)
  block ||= default_block(:one)
  get(_path, options, &block)
end
update(options={}, &block) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 250
def update(options={}, &block)
  block ||= default_block(:save)
  options.merge!(method: [:put, :patch])
  is(_path, options, &block)
end
version(version, options={}, &block) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 213
def version(version, options={}, &block)
  extract_resource_options options
  on("v#{version}", options, &block)
end

Private Instance Methods

_path(path=nil) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 285
def _path(path=nil)
  if @resource and @resource.singleton
    path = ['', true] unless path
  else
    path = [':id', path].compact.join("/")
  end
  path
end
block_result_body(result) click to toggle source
Calls superclass method
# File lib/roda/plugins/rest_api.rb, line 304
def block_result_body(result)
  if result && @resource
      response[CONTENT_TYPE] = @resource.content_type
      @resource.serialize.call(result)
    else
      super
  end
end
default_block(method) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 294
def default_block(method)
  if @resource.singleton
    ->(){@resource.perform(method)}
  else
    ->(id){@resource.perform(method, id)}
  end
end
extract_resource_options(options) click to toggle source
# File lib/roda/plugins/rest_api.rb, line 276
def extract_resource_options(options)
  @resource_options ||= []
  opts = {}
  (Resource::OPTIONS.keys & options.keys).each do |key|
    opts[key] = options.delete(key)
  end
  @resource_options << opts
end