Representers for arrays or no object at all?

364 views
Skip to first unread message

Aaron McLeod

unread,
Dec 11, 2013, 11:10:10 AM12/11/13
to roar...@googlegroups.com
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!

Nick Sutterer

unread,
Dec 11, 2013, 6:44:37 PM12/11/13
to roar...@googlegroups.com
Hey Aaron,

it seems both questions refer to the same concept: Representers are designed to be stupid. They decorate or extend an object (even if it's an empty Object.new) and eventually call readers and writers on that to represent (render or parse) that very instance.

So far, there's no mechanism to skip the initialization without the represented object. You have to go the "ugly" Object.new way, to answer 1.)


Regarding 2.) we hit the same concept. If you just wanna represent a collection of items (without the :self link) you'd go with a lonely collection representer: https://github.com/apotonick/representable/#lonely-collections

Since you wanna have the collection and the self link, you need a proper representer (SegmentTypeRepresenter). I am not sure if I entirely understand what you wanna do. Is it something like

respond_with [list of SegmentType], represent_with: SegmentTypeRepresenter

Why the Segment.new?


Feel free to ask anything you need to know,

Nick


--
You received this message because you are subscribed to the Google Groups "Roar" group.
To unsubscribe from this group and stop receiving emails from it, send an email to roar-talk+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nick Sutterer

unread,
Dec 11, 2013, 6:49:43 PM12/11/13
to roar...@googlegroups.com
If you think the Object.new feels too ugly to render a link-only representation, we could think about a HypermediaDocument that deals with that. However, this introduces another edge-case technique that people would have to learn.

I'd rather handle that in roar-rails and keep going with the existing concept of requiring users to provide a represented object to a representer. This makes sure people understand that representers represent objects instead of replacing them.

What about something like the following, in roar-rails?

  respond_with HypermediaDocument, represent_with: RootRepresenter

Reply all
Reply to author
Forward
0 new messages