jsonapi_serializer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ module JsonapiSerializer::Utils
2
+
3
+ def normalize_includes(includes)
4
+ case includes
5
+ when String
6
+ normalize_includes([includes.to_sym])
7
+ when Symbol
8
+ normalize_includes([includes])
9
+ when Hash
10
+ includes.each_with_object({}) do |(key, val), hash|
11
+ hash[key] = normalize_includes(val)
12
+ end
13
+ when Array
14
+ includes.each_with_object({}) do |entry, hash|
15
+ case entry
16
+ when Symbol
17
+ hash[entry] = {}
18
+ when Hash
19
+ hash.merge!(normalize_includes(entry))
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def apply_splat(item, &block)
26
+ if item.is_a? Array
27
+ item.map { |i| block.call(i) }
28
+ else
29
+ block.call(item)
30
+ end
31
+ end
32
+
33
+ def key_intersect(limited, full)
34
+ limited && limited & full || full
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module JsonapiSerializer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'fast_jsonapi'
2
+
3
+ module FastJsonapiTest
4
+
5
+ class AuthorSerializer
6
+ include FastJsonapi::ObjectSerializer
7
+ attributes :name, :age
8
+ end
9
+
10
+ class CharacterSerializer
11
+ include FastJsonapi::ObjectSerializer
12
+ attributes :name
13
+ end
14
+
15
+ class BookSerializer
16
+ include FastJsonapi::ObjectSerializer
17
+ attributes :name, :isbn, :year
18
+ belongs_to :author
19
+ has_many :characters
20
+ end
21
+
22
+ def self.base(records, type = :hash)
23
+ serializer = BookSerializer.new(records)
24
+ case type
25
+ when :hash
26
+ serializer.serializable_hash
27
+ when :json
28
+ serializer.serialized_json
29
+ end
30
+ end
31
+
32
+ def self.with_included(records, type = :hash)
33
+ serializer = BookSerializer.new(records, include: [:author])
34
+ case type
35
+ when :hash
36
+ serializer.serializable_hash
37
+ when :json
38
+ serializer.serialized_json
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ require 'jsonapi_serializer'
2
+
3
+ module JsonapiSerializerTest
4
+ JsonapiSerializer.set_type_namespace_separator :ignore
5
+
6
+ class AuthorSerializer
7
+ include JsonapiSerializer::Base
8
+ attributes :name, :age
9
+ end
10
+
11
+ class CharacterSerializer
12
+ include JsonapiSerializer::Base
13
+ attributes :name
14
+ end
15
+
16
+ class BookSerializer
17
+ include JsonapiSerializer::Base
18
+ attributes :name, :isbn, :year
19
+ belongs_to :author, serializer: AuthorSerializer
20
+ has_many :characters, serializer: CharacterSerializer
21
+ end
22
+
23
+ def self.base(records, type = :hash)
24
+ serializer = BookSerializer.new()
25
+ case type
26
+ when :hash
27
+ serializer.serializable_hash(records)
28
+ when :json
29
+ serializer.serialized_json(records)
30
+ end
31
+ end
32
+
33
+ def self.with_included(records, type = :hash)
34
+ serializer = BookSerializer.new(include: [:author])
35
+ case type
36
+ when :hash
37
+ serializer.serializable_hash(records)
38
+ when :json
39
+ serializer.serialized_json(records)
40
+ end
41
+ end
42
+ end
data/perf/models.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'ffaker'
2
+
3
+ class Book
4
+ attr_accessor :id, :name, :isbn, :year, :author, :author_id, :characters, :character_ids, :librarians, :librarian_ids
5
+
6
+ @characters = []
7
+ @character_ids = []
8
+
9
+ def initialize(id, opts = {})
10
+ @id = id
11
+ @name = FFaker::Book.title
12
+ @isbn = FFaker::Book.isbn
13
+ @year = 1990 + rand(30)
14
+ put_author(opts[:author_pool])
15
+ put_characters(opts[:character_pool])
16
+ end
17
+
18
+ def put_author(pool)
19
+ @author = pool.sample
20
+ @author_id = @author.id
21
+ end
22
+
23
+ def put_characters(pool)
24
+ @characters = (1..10).map { pool.sample }.uniq
25
+ @character_ids = @characters.map(&:id)
26
+ end
27
+ end
28
+
29
+ class Author
30
+ attr_accessor :id, :name, :age, :consultants, :consultant_ids
31
+ def initialize(id, consultant_pool)
32
+ @id = id
33
+ @name = FFaker::Name.name
34
+ @age = 20 + rand(40)
35
+ @consultants = (1..3).map { consultant_pool.sample }.uniq
36
+ @consultant_ids = @consultants.map(&:id)
37
+ end
38
+ end
39
+
40
+ class Consultant
41
+ attr_accessor :id, :name
42
+ def initialize(id)
43
+ @id = id
44
+ @name = FFaker::Name.name
45
+ end
46
+ end
47
+
48
+ class Character
49
+ attr_accessor :id, :name
50
+ def initialize(id)
51
+ @id = id
52
+ @name = FFaker::Name.name
53
+ end
54
+ end
55
+ #
56
+ # class Villain < Character
57
+ # attr_accessor :kills
58
+ # def initialize(id)
59
+ # super(id)
60
+ # @kills = rand(20)
61
+ # end
62
+ # end
63
+ #
64
+ # class Hero < Character
65
+ # attr_accessor :kills
66
+ # def initialize(id)
67
+ # super(id)
68
+ # @kills = rand(20)
69
+ # end
70
+ # end
71
+
72
+ class Models
73
+ def initialize(count)
74
+ consultant_pool = (1..50).map { |id| Consultant.new(id) }
75
+ author_pool = (1..50).map { |id| Author.new(id, consultant_pool) }
76
+ character_pool = (1..300).map { |id| Character.new(id) }
77
+
78
+ @storage = (1..count).map do |id|
79
+ Book.new(id, character_pool: character_pool, author_pool: author_pool)
80
+ end
81
+ end
82
+
83
+ def take(n)
84
+ @storage.take(n)
85
+ end
86
+ end
data/perf/runner.rb ADDED
@@ -0,0 +1,76 @@
1
+ require './perf/models'
2
+ require 'benchmark'
3
+ require 'digest'
4
+
5
+ class Runner
6
+
7
+ WIDTH = 24
8
+
9
+ def initialize(count)
10
+ @repo = Models.new(count)
11
+ end
12
+
13
+ def set_modules(*modules)
14
+ @modules = modules
15
+ end
16
+
17
+ def set_tests(*tests)
18
+ @tests = tests
19
+ end
20
+
21
+ def set_takes(*takes)
22
+ @takes = takes
23
+ end
24
+
25
+ def run
26
+ @results = {}
27
+ @tests.each do |test_case|
28
+ @results[test_case] = {takes: @takes, times: [], digests: {}}
29
+ @modules.each do |mod|
30
+ mod_results = [mod.to_s]
31
+ if mod.respond_to? test_case
32
+ @takes.each do |count|
33
+ records = @repo.take(count)
34
+ hash = '%.2f' % (Benchmark.measure { mod.send(test_case, records, :hash) }.real * 1000)
35
+ output = nil
36
+ json = '%.2f' % (Benchmark.measure { output = mod.send(test_case, records, :json) }.real * 1000)
37
+ verify_digest(count, @results[test_case][:digests], Digest::MD5.hexdigest(output))
38
+ mod_results << "#{hash} / #{json}"
39
+ end
40
+ else
41
+ mod_results += Array.new(@takes.length, "-")
42
+ end
43
+ @results[test_case][:times] << mod_results
44
+ end
45
+ end
46
+ end
47
+
48
+ def print_table(test_case)
49
+ print_header(@results[test_case][:takes])
50
+ print_body(@results[test_case][:times])
51
+ end
52
+
53
+ def print_header(takes)
54
+ col_titles = takes.map do |count|
55
+ "#{count} hash/json (ms)".center(WIDTH)
56
+ end
57
+ headers = ["Adapters".center(WIDTH)] + col_titles
58
+ print "| " + headers.join(" | ") + " |\n"
59
+ print "| " + Array.new(headers.length, "".rjust(WIDTH, "-")).join(" | ") + " |\n"
60
+ end
61
+
62
+ def print_body(times)
63
+ times.each do |ts|
64
+ print "| " + ts.map { |t| t.center(WIDTH) }.join(" | ") + " |\n"
65
+ end
66
+ end
67
+
68
+ def verify_digest(count, tracker, digest)
69
+ current = tracker[count]
70
+ if current.nil?
71
+ tracker[count] = digest
72
+ elsif current != digest
73
+ tracker[count] = false
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Yurov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-22 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: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ffaker
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.8.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.8.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: fast_jsonapi
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.0'
125
+ description: Alternative JSONApi serializer inspired by Netflix's fast_jsonapi serializer
126
+ email:
127
+ - ivan.youroff@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - CODE_OF_CONDUCT.md
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/benchmark
141
+ - bin/console
142
+ - bin/setup
143
+ - jsonapi_serializer.gemspec
144
+ - lib/jsonapi_serializer.rb
145
+ - lib/jsonapi_serializer/aux/converters.rb
146
+ - lib/jsonapi_serializer/base.rb
147
+ - lib/jsonapi_serializer/common.rb
148
+ - lib/jsonapi_serializer/dsl/common.rb
149
+ - lib/jsonapi_serializer/dsl/polymorphic.rb
150
+ - lib/jsonapi_serializer/polymorphic.rb
151
+ - lib/jsonapi_serializer/utils.rb
152
+ - lib/jsonapi_serializer/version.rb
153
+ - perf/fast_jsonapi_test.rb
154
+ - perf/jsonapi_serializer_test.rb
155
+ - perf/models.rb
156
+ - perf/runner.rb
157
+ homepage: https://github.com/youroff/jsonapi_serializer
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.5.1
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: Alternative JSONApi serializer
181
+ test_files: []