Hello,
I'm starting on building a hypermedia API.
My first question pertains to responding with just a responder, and not a particular object to pass with it.
The root of the API for now is to just contain links. I managed to achieve this by doing:
class RootRepresenter < Roar::Decorator
include Roar::Representer::JSON::HAL
include Roar::Representer::Feature::Hypermedia
link :self do
api_url
end
link :segments do
api_segment_types_url
end
end
And in the controller:
def index
respond_with Object.new, represent_with: RootRepresenter
end
Which feels rather ugly to put in a new object. Is there a way I can do this without specifying an object?
My second question is around collections. The documentation states specifying a method name that will return the collection on the passed object. However, i have a const array of an ObjectType that I wish to return so I can render links out to them:
class SegmentTypeRepresenter < Roar::Decorator
include Roar::Representer::JSON::HAL
include Roar::Representer::Feature::Hypermedia
collection :types, class: SegmentType, embedded: true do
property :name
end
link :self do
api_url
end
end
But I currently have to do this:
class Api::SegmentTypesController < ApplicationController
include Roar::Rails::ControllerAdditions
respond_to :json
def index
respond_with Segment.new, represent_with: SegmentTypeRepresenter
end
end
Is there a simpler way to pass an array of objects to a presenter? Thanks!