- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathspec_helper.rb
133 lines (113 loc) · 3.04 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require'simplecov'
SimpleCov.startdo
add_filter'/spec/'
end
require'jsonapi/renderer'
classUserResource
attr_accessor:id,:name,:address,:posts
definitialize(id,name,address,posts)
@id=id
@name=name
@address=address
@posts=posts
end
defjsonapi_type
'users'
end
defjsonapi_id
@id.to_s
end
defjsonapi_related(included)
ifincluded.include?(:posts)
{posts: @posts.map{ |p| p}}
else
{}
end
end
defjsonapi_cache_key(options={})
"#{jsonapi_type} - #{jsonapi_id} - #{options[:include].to_a.sort} - #{(options[:fields] || Set.new).to_a.sort}"
end
defas_jsonapi(options={})
fields=options[:fields] || [:name,:address,:posts]
included=options[:include] || []
hash={id: jsonapi_id,type: jsonapi_type}
hash[:attributes]={name: @name,address: @address}
.select{ |k,_| fields.include?(k)}
iffields.include?(:posts)
hash[:relationships]={posts: {}}
hash[:relationships][:posts]={
links: {
self: "http://api.example.com/users/#{@id}/relationships/posts",
related: {
href: "http://api.example.com/users/#{@id}/posts",
meta: {
do_not_use: true
}
}
},
meta: {
deleted_posts: 5
}
}
ifincluded.include?(:posts)
hash[:relationships][:posts][:data]=@posts.mapdo |p|
{type: 'posts',id: p.id.to_s}
end
end
end
hash[:links]={
self: "http://api.example.com/users/#{@id}"
}
hash[:meta]={user_meta: 'is_meta'}
hash
end
end
classPostResource
attr_accessor:id,:title,:date,:author
definitialize(id,title,date,author)
@id=id
@title=title
@date=date
@author=author
end
defjsonapi_type
'posts'
end
defjsonapi_id
@id.to_s
end
defjsonapi_related(included)
included.include?(:author) ? {author: [@author]} : {}
end
defjsonapi_cache_key(options={})
"#{jsonapi_type} - #{jsonapi_id} - #{options[:include].to_a.sort} - #{(options[:fields] || Set.new).to_a.sort}"
end
defas_jsonapi(options={})
fields=options[:fields] || [:title,:date,:author]
included=options[:include] || []
hash={id: jsonapi_id,type: jsonapi_type}
hash[:attributes]={title: @title,date: @date}
.select{ |k,_| fields.include?(k)}
iffields.include?(:author)
hash[:relationships]={author: {}}
hash[:relationships][:author]={
links: {
self: "http://api.example.com/posts/#{@id}/relationships/author",
related: "http://api.example.com/posts/#{@id}/author"
},
meta: {
author_active: true
}
}
ifincluded.include?(:author)
hash[:relationships][:author][:data]=
if@author.nil?
nil
else
{type: 'users',id: @author.id.to_s}
end
end
end
hash
end
end