Directive ‘single’
single({SINGLE}) { ... }
Declares that a model exposes a class method returning a single instance. {SINGLE} is the name of a class method (as Symbol).
Singles are exposed by their URI, which has the form:
https://{HOST}/{PATH}/{RESOURCE}/{SINGLE}?{PARAMS}, where
https://{HOST}is a FQDN.{PATH}is an optional path segment (seeexposeparameterunder:),{RESOURCE}is name of the resource/model{SINGLE}is the name the class method{PARAMS}is an optional list of query parameters
Within the block it must be declared which methods the exposed URI accepts. This is done by the directives which have further sub-directives for permissions and custom handlers:
Only GET is available. For updates or deletions use the canonical URI of the resource/model instance.
Note, that also pre-defined methods of ActiveRecord can be exposed. E.g. :first, :second, :last.
expose(Person) {
readables :first_name, :last_name
single(:first) {
via_get {
allow do |*args|
true # allow for all
end
}
}
}gives:
GET https://example.com/people/first:
{
"self" : "https://example.com/people/1",
"first_name": "John",
"last_name" : "Smith"
}