diff --git a/Gemfile b/Gemfile index 3506a0e..ad3f87e 100644 --- a/Gemfile +++ b/Gemfile @@ -3,4 +3,5 @@ gemspec group :test do gem 'rake' + gem 'rspec' end diff --git a/README.md b/README.md index 3183ef9..c948707 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# JSON API *VANILLA* +# JSON API _VANILLA_ -Deserialize JSON API formats into *vanilla* Ruby objects. +Deserialize JSON API formats into _vanilla_ Ruby objects. The simplest JSON API library at all altitudes above Earth centre. ```ruby @@ -38,13 +38,14 @@ fields: - `errors` is an array containing [errors](http://jsonapi.org/format/#error-objects). Each error is a Hash. - `links` is a Hash from objects (obtained from `data`) to their links, as a Hash. + - A `links` Hash is also included as an attribute on each `data` object (ie `data.links` or `data[0].links`). - `rel_links` is a Hash from objects' relationships (obtained from `data`) to the links defined in that relationship, as a Hash. - `meta` is a Hash from objects to their meta information (a Hash). - `find('type', 'id')` returns the object with that type and that id. - `find_all('type')` returns an Array of all objects with that type. - `keys` is a Hash from objects to a Hash from their original field names - (non-snake\_case'd) to the corresponding object. + (non-snake_case'd) to the corresponding object. # License diff --git a/lib/json-api-vanilla/parser.rb b/lib/json-api-vanilla/parser.rb index c16d3b8..64089e1 100644 --- a/lib/json-api-vanilla/parser.rb +++ b/lib/json-api-vanilla/parser.rb @@ -68,6 +68,7 @@ def self.build(hash) end if o_hash['links'] links[obj] = o_hash['links'] + set_key(obj, 'links', o_hash['links'], original_keys) end objects[[obj.type, obj.id]] = obj end @@ -122,6 +123,7 @@ def self.prepare_class(hash, superclass, container) end add_accessor(klass, 'id') add_accessor(klass, 'type') + add_accessor(klass, 'links') attr_keys = hash['attributes'] ? hash['attributes'].keys : [] rel_keys = hash['relationships'] ? hash['relationships'].keys : [] (attr_keys + rel_keys).each do |key| diff --git a/lib/json-api-vanilla/version.rb b/lib/json-api-vanilla/version.rb index f5543e5..7c6e8bc 100644 --- a/lib/json-api-vanilla/version.rb +++ b/lib/json-api-vanilla/version.rb @@ -2,7 +2,7 @@ module JSON module Api module Vanilla - VERSION = '1.0.1' + VERSION = '1.1.0' end end end diff --git a/spec/json-api-vanilla/diff_spec.rb b/spec/json-api-vanilla/diff_spec.rb index 25ea2ed..4c4bc33 100644 --- a/spec/json-api-vanilla/diff_spec.rb +++ b/spec/json-api-vanilla/diff_spec.rb @@ -97,4 +97,27 @@ JSON::Api::Vanilla.naive_validate(data: []) end.to_not raise_error end + + it "should include resources links on the data object" do + expect(doc.data[0].links["self"]).to eql("http://example.com/articles/1") + end + + it "should include resource links when the data object is not an array" do + json = <<-JSON + { + "data": { + "type": "articles", + "id": "1", + "attributes": { + "title": "JSON API paints my bikeshed!" + }, + "links": { + "self": "http://example.com/articles/1" + } + } + } + JSON + doc = JSON::Api::Vanilla.parse(json) + expect(doc.data.links["self"]).to eql("http://example.com/articles/1") + end end