wrapping in object but not in collection

40 views
Skip to first unread message

Marcel Augsburger

unread,
Dec 10, 2013, 10:36:02 AM12/10/13
to roar...@googlegroups.com
I'm working with an API that is not resource oriented, and wraps everything around a "result"

So when I go to the resource url for a person I get:
{
  result:{
    • name"Marcel Augsburger",
    • id"52581d2d097182ee7b000001"
    }
}

And for a collection I get:

{
  • result
    [
    • {
      • name"Marcel Augsburger",
      • id"52581d2d097182ee7b000001"
      },
    • {
      • name"Another Person",
      • id"52581d47097182ee7b000002"
      }
    ]
}

So for my a person representer I would fix the problem adding a wrapper:

self.representation_wrap= :result

So in the collection I would use:

collection :result, :extend => PersonRepresenter, :class => Person

but the wrapper around person makes it not work, since the persons aren't wrapped in the collection.

Any ideas?

Nick Sutterer

unread,
Dec 10, 2013, 6:07:47 PM12/10/13
to roar...@googlegroups.com
Hey Marcel, use a lonely collection with a wrap for your collection: https://github.com/apotonick/representable/#lonely-collections

We are working on a new feature to make lonely collections implicit, meaning, they can be created automatically from the single representer: https://github.com/apotonick/representable/pull/22

Please, don't hesitate to ask if there's more questions.

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.

Marcel Augsburger

unread,
Dec 11, 2013, 11:02:09 AM12/11/13
to roar...@googlegroups.com
Yes, more questions! I tried it out and it gives me the same error (undefined method `has_key?' for nil:NilClass in method 'read' in hash_bindings.rb). I guess if I use a lonely collection and a wrap for my collection, I still have the problem that a single object (Person) has a wrapper, but no wrapper when fetching a collection of them. The real problem, I think, is dealing with the "result" wrapper when fetching a single object, or somehow ignoring that wrapper in the collection.

Just to make everything clear here's the real code and real sources:

Example person:

Example person collection:

Person Representer:

Person Collection Representer:

Example classes:
class PopitPerson
include Popit::PersonRepresenter
end
class PopitPersonCollection
include Popit::PersonCollectionRepresenter
end

So, as it is, it works when I "get" a single person, but doesn't when I get a collection.

If I comment the wrapper in the person representer:

# self.representation_wrap= :result

then it works when I get a collection, but not when I get a single person.

I guess I could modify the person json to get rid of the "result" wrapper, and then trick the get method into using the new json, but there has to be a nicer way right?




2013/12/10 Nick Sutterer <apot...@gmail.com>
Reply all
Reply to author
Forward
0 new messages