jsonapi-serializers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +417 -0
- data/Rakefile +2 -0
- data/jsonapi-serializers.gemspec +26 -0
- data/lib/jsonapi-serializers/attributes.rb +58 -0
- data/lib/jsonapi-serializers/serializer.rb +435 -0
- data/lib/jsonapi-serializers/version.rb +5 -0
- data/lib/jsonapi-serializers.rb +11 -0
- data/spec/serializer_spec.rb +630 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/support/factory.rb +26 -0
- data/spec/support/serializers.rb +98 -0
- metadata +134 -0
@@ -0,0 +1,98 @@
|
|
1
|
+
module MyApp
|
2
|
+
class Post
|
3
|
+
attr_accessor :id
|
4
|
+
attr_accessor :title
|
5
|
+
attr_accessor :body
|
6
|
+
attr_accessor :author
|
7
|
+
attr_accessor :long_comments
|
8
|
+
end
|
9
|
+
|
10
|
+
class LongComment
|
11
|
+
attr_accessor :id
|
12
|
+
attr_accessor :body
|
13
|
+
attr_accessor :user
|
14
|
+
attr_accessor :post
|
15
|
+
end
|
16
|
+
|
17
|
+
class User
|
18
|
+
attr_accessor :id
|
19
|
+
attr_accessor :name
|
20
|
+
end
|
21
|
+
|
22
|
+
class PostSerializer
|
23
|
+
include JSONAPI::Serializer
|
24
|
+
|
25
|
+
attribute :title
|
26
|
+
attribute :long_content do
|
27
|
+
object.body
|
28
|
+
end
|
29
|
+
|
30
|
+
has_one :author
|
31
|
+
has_many :long_comments
|
32
|
+
end
|
33
|
+
|
34
|
+
class LongCommentSerializer
|
35
|
+
include JSONAPI::Serializer
|
36
|
+
|
37
|
+
attribute :body
|
38
|
+
has_one :user
|
39
|
+
|
40
|
+
# Circular-reference back to post.
|
41
|
+
has_one :post
|
42
|
+
end
|
43
|
+
|
44
|
+
class UserSerializer
|
45
|
+
include JSONAPI::Serializer
|
46
|
+
|
47
|
+
attribute :name
|
48
|
+
end
|
49
|
+
|
50
|
+
# More customized, one-off serializers to test particular behaviors:
|
51
|
+
|
52
|
+
class SimplestPostSerializer
|
53
|
+
include JSONAPI::Serializer
|
54
|
+
|
55
|
+
attribute :title
|
56
|
+
attribute :long_content do
|
57
|
+
object.body
|
58
|
+
end
|
59
|
+
|
60
|
+
def type
|
61
|
+
:posts
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class PostSerializerWithMetadata
|
66
|
+
include JSONAPI::Serializer
|
67
|
+
|
68
|
+
attribute :title
|
69
|
+
attribute :long_content do
|
70
|
+
object.body
|
71
|
+
end
|
72
|
+
|
73
|
+
def type
|
74
|
+
'posts' # Intentionally test string type.
|
75
|
+
end
|
76
|
+
|
77
|
+
def meta
|
78
|
+
{
|
79
|
+
'copyright' => 'Copyright 2015 Example Corp.',
|
80
|
+
'authors' => ['Aliens'],
|
81
|
+
}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class PostSerializerWithContextHandling < SimplestPostSerializer
|
86
|
+
include JSONAPI::Serializer
|
87
|
+
|
88
|
+
attribute :body, if: :show_body?, unless: :hide_body?
|
89
|
+
|
90
|
+
def show_body?
|
91
|
+
context.fetch(:show_body, true)
|
92
|
+
end
|
93
|
+
|
94
|
+
def hide_body?
|
95
|
+
context.fetch(:hide_body, false)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonapi-serializers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Fotinakis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: factory_girl
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.5'
|
83
|
+
description: ''
|
84
|
+
email:
|
85
|
+
- mike@fotinakis.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- jsonapi-serializers.gemspec
|
98
|
+
- lib/jsonapi-serializers.rb
|
99
|
+
- lib/jsonapi-serializers/attributes.rb
|
100
|
+
- lib/jsonapi-serializers/serializer.rb
|
101
|
+
- lib/jsonapi-serializers/version.rb
|
102
|
+
- spec/serializer_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/support/factory.rb
|
105
|
+
- spec/support/serializers.rb
|
106
|
+
homepage: ''
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.2.2
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Pure Ruby serializers conforming to the JSON:API spec.
|
130
|
+
test_files:
|
131
|
+
- spec/serializer_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
- spec/support/factory.rb
|
134
|
+
- spec/support/serializers.rb
|