Parsing collections from HAL API's

74 views
Skip to first unread message

Steve Hoeksema

unread,
Sep 2, 2014, 2:31:51 AM9/2/14
to roar...@googlegroups.com
Hi all,

I'm trying to save some time when querying a third party API that claims to be HAL.

https://gist.github.com/steveh/ab975e70333d6aabad8f

A (truncated) API response is in the heredoc.

How can I parse that JSON as a collection of objects? I can't get more than an empty array.

cheers!

Nick Sutterer

unread,
Sep 2, 2014, 4:57:35 AM9/2/14
to roar...@googlegroups.com
Hi Steve,

check out the docs! https://github.com/apotonick/roar#nesting-1




--
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/d/optout.

349.gif

Steve Hoeksema

unread,
Sep 2, 2014, 5:41:37 AM9/2/14
to roar...@googlegroups.com
Hi Nick,

I've read through the docs and done some experimenting. That section seems to be about output, I'm talking about input.

The only examples I've seen are about parsing collections that are attached to an entity (like a has_many), I can't find out how to parse a collection with no parent - e.g. a list, like my attached example.




--
You received this message because you are subscribed to a topic in the Google Groups "Roar" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/roar-talk/dUlRfUUQ3fo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to roar-talk+...@googlegroups.com.
349.gif

Nick Sutterer

unread,
Sep 2, 2014, 6:06:03 PM9/2/14
to roar...@googlegroups.com
Ah, I missed this. You have to use a lonely collection representer that uses the actual representer you wrote for items: https://github.com/apotonick/representable#lonely-collections

You should also be able to use for_collection with your ServiceRepresenter itself: https://github.com/apotonick/representable#representing-singular-models-and-collections

ServiceRepresenter.for_collection.prepare([]).from_json ".."
349.gif

Nick Sutterer

unread,
Sep 2, 2014, 6:07:21 PM9/2/14
to roar...@googlegroups.com
For a better understanding, the following two calls are identical:

ServiceRepresenter.for_collection.prepare([]).from_json ".."
[].extend(ServiceRepresenter.for_collection).from_json ".."
349.gif

Nick Sutterer

unread,
Sep 2, 2014, 6:21:10 PM9/2/14
to roar...@googlegroups.com
Anyway, what you really wanna do (and that is documented in the README) is this.

module ServiceRepresenter
include Roar::Representer::JSON::HAL
collection :services, embedded: true do
property :id
end
end

The incoming document is a valid HAL document, so you need a HAL representer! One requirement here, though. The represented object needs a services accessor, though.

OpenStruct.new.extend(ServiceRepresenter).from_json "..".services #=> [..]

This is the generic way using the HAL representer. I can't think of a way to tell Roar to parse the embedded services keys as a lonely collection. Does the above stuff work of for you?

349.gif
360.gif
Reply all
Reply to author
Forward
0 new messages