|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | | -class UserResource |
4 | | - attr_accessor :id, :name, :address, :posts |
5 | | - |
6 | | - def initialize(id, name, address, posts) |
7 | | - @id = id |
8 | | - @name = name |
9 | | - @address = address |
10 | | - @posts = posts |
11 | | - end |
12 | | - |
13 | | - def jsonapi_type |
14 | | - 'users' |
15 | | - end |
16 | | - |
17 | | - def jsonapi_id |
18 | | - @id.to_s |
19 | | - end |
20 | | - |
21 | | - def jsonapi_related(included) |
22 | | - if included.include?(:posts) |
23 | | - { posts: @posts.map { |p| p } } |
24 | | - else |
25 | | - {} |
26 | | - end |
27 | | - end |
28 | | - |
29 | | - def as_jsonapi(options = {}) |
30 | | - fields = options[:fields] || [:name, :address, :posts] |
31 | | - included = options[:include] || [] |
32 | | - |
33 | | - hash = { id: jsonapi_id, type: jsonapi_type } |
34 | | - hash[:attributes] = { name: @name, address: @address } |
35 | | - .select { |k, _| fields.include?(k) } |
36 | | - if fields.include?(:posts) |
37 | | - hash[:relationships] = { posts: {} } |
38 | | - hash[:relationships][:posts] = { |
39 | | - links: { |
40 | | - self: "http://api.example.com/users/#{@id}/relationships/posts", |
41 | | - related: { |
42 | | - href: "http://api.example.com/users/#{@id}/posts", |
43 | | - meta: { |
44 | | - do_not_use: true |
45 | | - } |
46 | | - } |
47 | | - }, |
48 | | - meta: { |
49 | | - deleted_posts: 5 |
50 | | - } |
51 | | - } |
52 | | - if included.include?(:posts) |
53 | | - hash[:relationships][:posts][:data] = @posts.map do |p| |
54 | | - { type: 'posts', id: p.id.to_s } |
55 | | - end |
56 | | - end |
57 | | - end |
58 | | - |
59 | | - hash[:links] = { |
60 | | - self: "http://api.example.com/users/#{@id}" |
61 | | - } |
62 | | - hash[:meta] = { user_meta: 'is_meta' } |
63 | | - |
64 | | - hash |
65 | | - end |
66 | | -end |
67 | | - |
68 | | -class PostResource |
69 | | - attr_accessor :id, :title, :date, :author |
70 | | - |
71 | | - def initialize(id, title, date, author) |
72 | | - @id = id |
73 | | - @title = title |
74 | | - @date = date |
75 | | - @author = author |
76 | | - end |
77 | | - |
78 | | - def jsonapi_type |
79 | | - 'posts' |
80 | | - end |
81 | | - |
82 | | - def jsonapi_id |
83 | | - @id.to_s |
84 | | - end |
85 | | - |
86 | | - def jsonapi_related(included) |
87 | | - included.include?(:author) ? { author: [@author] } : {} |
88 | | - end |
89 | | - |
90 | | - def as_jsonapi(options = {}) |
91 | | - fields = options[:fields] || [:title, :date, :author] |
92 | | - included = options[:include] || [] |
93 | | - hash = { id: jsonapi_id, type: jsonapi_type } |
94 | | - |
95 | | - hash[:attributes] = { title: @title, date: @date } |
96 | | - .select { |k, _| fields.include?(k) } |
97 | | - if fields.include?(:author) |
98 | | - hash[:relationships] = { author: {} } |
99 | | - hash[:relationships][:author] = { |
100 | | - links: { |
101 | | - self: "http://api.example.com/posts/#{@id}/relationships/author", |
102 | | - related: "http://api.example.com/posts/#{@id}/author" |
103 | | - }, |
104 | | - meta: { |
105 | | - author_active: true |
106 | | - } |
107 | | - } |
108 | | - if included.include?(:author) |
109 | | - hash[:relationships][:author][:data] = |
110 | | - if @author.nil? |
111 | | - nil |
112 | | - else |
113 | | - { type: 'users', id: @author.id.to_s } |
114 | | - end |
115 | | - end |
116 | | - end |
117 | | - |
118 | | - hash |
119 | | - end |
120 | | -end |
121 | | - |
122 | 3 | describe JSONAPI::Renderer, '#render' do |
123 | 4 | before(:all) do |
124 | 5 | @users = [ |
|
0 commit comments