Skip to content
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Ruby gem for rendering [JSON API](http://jsonapi.org) documents.

[![Gem Version](https://badge.fury.io/rb/jsonapi-renderer.svg)](https://badge.fury.io/rb/jsonapi-renderer)
[![Build Status](https://secure.travis-ci.org/jsonapi-rb/renderer.svg?branch=master)](http://travis-ci.org/jsonapi-rb/renderer?branch=master)
[![codecov](https://codecov.io/gh/jsonapi-rb/renderer/branch/master/graph/badge.svg)](https://codecov.io/gh/jsonapi-rb/renderer)

## Installation
```ruby
Expand Down
1 change: 1 addition & 0 deletions jsonapi-renderer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'rake', '~> 11.3'
spec.add_development_dependency 'rspec', '~> 3.5'
spec.add_development_dependency 'codecov', '~> 0.1'
end
2 changes: 2 additions & 0 deletions spec/include_directive/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spec_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing frozen string literal comment.


require 'jsonapi/include_directive'

describe JSONAPI::IncludeDirective::Parser, '.parse_include_args' do
Expand Down
2 changes: 2 additions & 0 deletions spec/include_directive_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spec_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing frozen string literal comment.


require 'jsonapi/include_directive'

describe JSONAPI::IncludeDirective, '.key?' do
Expand Down
84 changes: 40 additions & 44 deletions spec/renderer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
require 'jsonapi/renderer'
require 'spec_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing frozen string literal comment.


class Model
def initialize(params)
params.each { |k, v| instance_variable_set("@#{k}", v) }
end
end

class User < Model
class UserResource
attr_accessor :id, :name, :address, :posts
end

class Post < Model
attr_accessor :id, :title, :date, :author
end

class UserResource
def initialize(user)
@user = user
def initialize(id, name, address, posts)
@id = id
@name = name
@address = address
@posts = posts
end

def jsonapi_type
'users'
end

def jsonapi_id
@user.id.to_s
@id.to_s
end

def jsonapi_related(included)
if included.include?(:posts)
{ posts: @user.posts.map { |p| PostResource.new(p) } }
{ posts: @posts.map { |p| p } }
else
{}
end
Expand All @@ -40,15 +31,15 @@ def as_jsonapi(options = {})
included = options[:include] || []

hash = { id: jsonapi_id, type: jsonapi_type }
hash[:attributes] = { name: @user.name, address: @user.address }
hash[:attributes] = { name: @name, address: @address }
.select { |k, _| fields.include?(k) }
if fields.include?(:posts)
hash[:relationships] = { posts: {} }
hash[:relationships][:posts] = {
links: {
self: "http://api.example.com/users/#{@user.id}/relationships/posts",
self: "http://api.example.com/users/#{@id}/relationships/posts",
related: {
href: "http://api.example.com/users/#{@user.id}/posts",
href: "http://api.example.com/users/#{@id}/posts",
meta: {
do_not_use: true
}
Expand All @@ -59,14 +50,14 @@ def as_jsonapi(options = {})
}
}
if included.include?(:posts)
hash[:relationships][:posts][:data] = @user.posts.map do |p|
hash[:relationships][:posts][:data] = @posts.map do |p|
{ type: 'posts', id: p.id.to_s }
end
end
end

hash[:links] = {
self: "http://api.example.com/users/#{@user.id}"
self: "http://api.example.com/users/#{@id}"
}
hash[:meta] = { user_meta: 'is_meta' }

Expand All @@ -75,46 +66,51 @@ def as_jsonapi(options = {})
end

class PostResource
def initialize(post)
@post = post
attr_accessor :id, :title, :date, :author

def initialize(id, title, date, author)
@id = id
@title = title
@date = date
@author = author
end

def jsonapi_type
'posts'
end

def jsonapi_id
@post.id.to_s
@id.to_s
end

def jsonapi_related(included)
included.include?(:author) ? { author: UserResource.new(@post.author) } : {}
included.include?(:author) ? { author: @author } : {}
end

def as_jsonapi(options = {})
fields = options[:fields] || [:title, :date, :author]
included = options[:include] || []
hash = { id: jsonapi_id, type: jsonapi_type }

hash[:attributes] = { title: @post.title, date: @post.date }
hash[:attributes] = { title: @title, date: @date }
.select { |k, _| fields.include?(k) }
if fields.include?(:author)
hash[:relationships] = { author: {} }
hash[:relationships][:author] = {
links: {
self: "http://api.example.com/posts/#{@post.id}/relationships/author",
related: "http://api.example.com/posts/#{@post.id}/author"
self: "http://api.example.com/posts/#{@id}/relationships/author",
related: "http://api.example.com/posts/#{@id}/author"
},
meta: {
author_active: true
}
}
if included.include?(:author)
hash[:relationships][:author][:data] =
if @post.author.nil?
if @author.nil?
nil
else
{ type: 'users', id: @post.author.id.to_s }
{ type: 'users', id: @author.id.to_s }
end
end
end
Expand All @@ -126,15 +122,15 @@ def as_jsonapi(options = {})
describe JSONAPI, '#render' do
before(:all) do
@users = [
User.new(id: 1, name: 'User 1', address: '123 Example st.', posts: []),
User.new(id: 2, name: 'User 2', address: '234 Example st.', posts: []),
User.new(id: 3, name: 'User 3', address: '345 Example st.', posts: []),
User.new(id: 4, name: 'User 4', address: '456 Example st.', posts: [])
UserResource.new(1, 'User 1', '123 Example st.', []),
UserResource.new(2, 'User 2', '234 Example st.', []),
UserResource.new(3, 'User 3', '345 Example st.', []),
UserResource.new(4, 'User 4', '456 Example st.', [])
]
@posts = [
Post.new(id: 1, title: 'Post 1', date: 'yesterday', author: @users[1]),
Post.new(id: 2, title: 'Post 2', date: 'today', author: @users[0]),
Post.new(id: 3, title: 'Post 3', date: 'tomorrow', author: @users[1])
PostResource.new(1, 'Post 1', 'yesterday', @users[1]),
PostResource.new(2, 'Post 2', 'today', @users[0]),
PostResource.new(3, 'Post 3', 'tomorrow', @users[1])
]
@users[0].posts = [@posts[1]]
@users[1].posts = [@posts[0], @posts[2]]
Expand All @@ -159,7 +155,7 @@ def as_jsonapi(options = {})
end

it 'renders a single resource' do
actual = JSONAPI.render(data: UserResource.new(@users[0]))
actual = JSONAPI.render(data: @users[0])
expected = {
data: {
type: 'users',
Expand Down Expand Up @@ -197,8 +193,8 @@ def as_jsonapi(options = {})
end

it 'renders a collection of resources' do
actual = JSONAPI.render(data: [UserResource.new(@users[0]),
UserResource.new(@users[1])])
actual = JSONAPI.render(data: [@users[0],
@users[1]])
expected = {
data: [
{
Expand Down Expand Up @@ -268,7 +264,7 @@ def as_jsonapi(options = {})
end

it 'renders included relationships' do
actual = JSONAPI.render(data: UserResource.new(@users[0]),
actual = JSONAPI.render(data: @users[0],
include: 'posts')
expected = {
data: {
Expand Down Expand Up @@ -329,7 +325,7 @@ def as_jsonapi(options = {})
end

it 'filters out fields' do
actual = JSONAPI.render(data: UserResource.new(@users[0]),
actual = JSONAPI.render(data: @users[0],
fields: { users: [:name] })
expected = {
data: {
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'simplecov'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing frozen string literal comment.

SimpleCov.start

require 'codecov'
SimpleCov.formatter = SimpleCov::Formatter::Codecov

require 'jsonapi/renderer'