gamache / hyperresource

A self-inflating Ruby client for hypermedia APIs. Not under active development.
http://hyperresource.com/doc
MIT License
304 stars 29 forks source link

Support embedded resources with no "_links" property or "self" link #29

Closed ghost closed 8 years ago

ghost commented 10 years ago

Presently HyperResource will fail trying to load an embedded resource object that has no "_links" property, or a "_links" property but no "self" link, with an error message like

NoMethodError: undefined method `[]' for nil:NilClass
    /.../lib/hyper_resource/adapter/hal_json.rb:50:in `block in apply_objects'
    /.../lib/hyper_resource/adapter/hal_json.rb:45:in `each'
    /.../lib/hyper_resource/adapter/hal_json.rb:45:in `apply_objects'
    /.../lib/hyper_resource/adapter/hal_json.rb:30:in `apply'
    ...

But according to the HAL spec,

The reserved "_links" property is OPTIONAL.

and,

Each Resource Object SHOULD [not MUST] contain a 'self' link...

This makes sense, as a resource corresponding to a weak or associative entity in a data model may have no identity of its own and thus no "self" link or any meaningful links at all. HyperResource should be able to work with resources of this type.

I've fixed this (I believe) locally and will submit a pull request soon.