Skip to content

Create a links object on each resource #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ gemspec

group :test do
gem 'rake'
gem 'rspec'
end
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/json-api-vanilla/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion lib/json-api-vanilla/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module JSON
module Api
module Vanilla
VERSION = '1.0.1'
VERSION = '1.1.0'
end
end
end
23 changes: 23 additions & 0 deletions spec/json-api-vanilla/diff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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