JA2R

Gem Version Depfu

JASON-API to Ruby Object

Converts a JSON-API payload into a ruby object which supports navigation over relationships.

Usage

json = "{\n   \"data\":{\n      \"id\":\"1001\",\n      \"type\":\"persons\",\n      \"attributes\":{\n         \"name\":\"Bart\"\n      },\n      \"relationships\":{\n         \"sister\":{\n            \"data\":{\n               \"id\":\"1002\",\n               \"type\":\"persons\"\n            }\n         }\n      }\n   },\n   \"included\":[\n      {\n         \"id\":\"1002\",\n         \"type\":\"persons\",\n         \"attributes\":{\n            \"name\":\"Lisa\"\n         }\n      }\n   ]\n}\n"
bart = JA2R.parse(JSON.parse(json))
bart.sister.name # Lisa

Options

You can pass options to JA2R#parse as second argument. Currently the only supported options are:

Name Type Description
safe_traverse boolean When setting this option to true, unknown relationships or attributes will return nil instead of raising NoMethodError

Example:

bart = JA2R.parse(hash, safe_traverse: true)
bart.uncle&.name # nil