sideload_serializer 0.0.1.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb927cee853f64a4c4f329cd3f762a64d9e7c818
4
+ data.tar.gz: bfd5de20b520857e9a3d0eedf73e358ed2d99c58
5
+ SHA512:
6
+ metadata.gz: 05f87ba01f4285e200ee7330092fd8116b0335a3c887516d6b1fe7dfc36f813da68cd6674142f24fdf15c930a049bf4441d00bb0d31d3595048ffd2fe20a8138
7
+ data.tar.gz: 570a90648054cf5a15e6dfe80184e3bb1bc066cca9d7386f9033a0e45e764a411f8aec3210d50da8499141c43011865f7a00184d72ab104dd408d6be7927bfed
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sideload_serializer.gemspec
4
+ gemspec
5
+
6
+
7
+ gem 'active_model_serializers', github: 'rails-api/active_model_serializers'
8
+ # gem 'pry'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Daniel Evans
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,165 @@
1
+ # SideloadSerializer
2
+
3
+ This is an adapter for [Active Model Serializers](https://github.com/rails-api/active_model_serializers) >= 0.10 intended to give a sideload JSON style similar to AMS 0.8.* embed approach with some alterations to conform to the [Rocketmade](http://rocketmade.com) API Standard.
4
+
5
+ It includes two forms of the adapter, the first serializes to the sideloaded format and the second alters all hash keys to camelCase.
6
+
7
+ Sideloaded json is extremely useful for offline sync situations where data is being loaded into coredata or other local storage. It helps reduce reparsing and transfer size by ensuring that each object is represented exactly once.
8
+
9
+ An authentication response from [Rockauth](https://github.com/rocketmade/rockauth) is used for the following examples.
10
+
11
+ Underscore Example:
12
+
13
+ ```json
14
+ {
15
+ "authentications": [
16
+ {
17
+ "id": 4,
18
+ "token": "<jwt>",
19
+ "token_id": "<jwt_id>",
20
+ "expiration": 1483647326,
21
+ "client_version": null,
22
+ "device_identifier": null,
23
+ "device_os": null,
24
+ "device_os_version": null,
25
+ "device_description": null,
26
+ "user_id": 3,
27
+ "provider_authentication_id": 1
28
+ }
29
+ ],
30
+ "users": [
31
+ {
32
+ "id": 3,
33
+ "email": null,
34
+ "first_name": null,
35
+ "last_name": null
36
+ }
37
+ ],
38
+ "provider_authentications": [
39
+ {
40
+ "id": 1,
41
+ "provider": "facebook",
42
+ "provider_user_id": "facebook_user_id"
43
+ }
44
+ ],
45
+ "meta": {
46
+ "primary_resource_collection": "authentications",
47
+ "primary_resource_id": 4
48
+ }
49
+ }
50
+ ```
51
+
52
+ Camelized Example:
53
+
54
+ ```json
55
+ {
56
+ "authentications": [
57
+ {
58
+ "id": 8,
59
+ "token": "<jwt>",
60
+ "expiration": 1483647222,
61
+ "tokenId": "<jwt_id>",
62
+ "clientVersion": null,
63
+ "deviceIdentifier": null,
64
+ "deviceOs": null,
65
+ "deviceOsVersion": null,
66
+ "deviceDescription": null,
67
+ "userId": 5,
68
+ "providerAuthenticationId": 1
69
+ }
70
+ ],
71
+ "users": [
72
+ {
73
+ "id": 5,
74
+ "email": null,
75
+ "firstName": null,
76
+ "lastName": null
77
+ }
78
+ ],
79
+ "providerAuthentications": [
80
+ {
81
+ "id": 1,
82
+ "provider": "facebook",
83
+ "providerUserId": "facebook_user_id"
84
+ }
85
+ ],
86
+ "meta": {
87
+ "primaryResourceCollection": "authentications",
88
+ "primaryResourceId": 8
89
+ }
90
+ }
91
+ ```
92
+
93
+ The same response with the `json` adapter:
94
+
95
+ ```json
96
+ {
97
+ "authentication": {
98
+ "id": 6,
99
+ "token": "<jwt>",
100
+ "token_id": "<jwt>",
101
+ "expiration": 1483647755,
102
+ "client_version": null,
103
+ "device_identifier": null,
104
+ "device_os": null,
105
+ "device_os_version": null,
106
+ "device_description": null,
107
+ "user": {
108
+ "id": 4,
109
+ "email": null,
110
+ "first_name": null,
111
+ "last_name": null
112
+ },
113
+ "provider_authentication": {
114
+ "id": 1,
115
+ "provider": "facebook",
116
+ "provider_user_id": "facebook_user_id"
117
+ }
118
+ }
119
+ }
120
+
121
+ ```
122
+ ## Installation
123
+
124
+ Add this line to your application's Gemfile:
125
+
126
+ ```ruby
127
+ gem 'sideload_serializer'
128
+ ```
129
+
130
+ And then execute:
131
+
132
+ $ bundle
133
+
134
+ Or install it yourself as:
135
+
136
+ $ gem install sideload_serializer
137
+
138
+ ## Usage
139
+
140
+ Add the following line to `$RAILS_ROOT/config/initializers/active_model_serializers`
141
+
142
+ ```
143
+ ActiveModelSerializers.config.adapter = "sideload_serializer/adapter"
144
+ ```
145
+
146
+ Or, to use the camelized keys form of the adapter:
147
+
148
+ ```
149
+ ActiveModelSerializers.config.adapter = "sideload_serializer/camelized_adapter"
150
+ ```
151
+
152
+ ## Development
153
+
154
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
155
+
156
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
157
+
158
+ ## Contributing
159
+
160
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rocketmade/sideload_serializer.
161
+
162
+
163
+ ## License
164
+
165
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sideload_serializer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require "sideload_serializer/version"
2
+
3
+ module SideloadSerializer
4
+ end
5
+
6
+ # Active Model Serializers registers the adapter on subclassing, so eagerload
7
+ require 'sideload_serializer/adapter'
8
+ require 'sideload_serializer/camelized_adapter'
@@ -0,0 +1,110 @@
1
+ require 'active_model_serializers'
2
+
3
+ module SideloadSerializer
4
+ class Adapter < ActiveModel::Serializer::Adapter::Base
5
+ def initialize(serializer, options = {})
6
+ super
7
+ @include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include] || '*')
8
+ end
9
+
10
+ def serializable_hash _options=nil
11
+ {}.tap do |document|
12
+ add_serializer_to_document serializer, document, Hash.new, @include_tree
13
+ end
14
+ end
15
+
16
+ def add_serializer_to_document serializer_instance, document, serialized_keys, include_tree
17
+ return unless serializer_instance && serializer_instance.object
18
+ if collection_serializer_given? serializer_instance
19
+ serializer_instance.each do |s|
20
+ add_serializer_to_document s, document, serialized_keys, include_tree
21
+ end
22
+
23
+ else
24
+ cache_key = serializer_instance.object.cache_key rescue serializer_instance.object.hash
25
+
26
+ return if serialized_keys.has_key? cache_key
27
+
28
+ attributes = cache_check serializer_instance do
29
+ serializer_instance.attributes
30
+ end
31
+
32
+ add_relationship_keys serializer_instance, attributes, include_tree
33
+
34
+ key = root serializer_instance
35
+
36
+ document[key] ||= []
37
+ document[key] << attributes
38
+
39
+ serialized_keys[cache_key] = true
40
+
41
+ serializer_instance.associations(include_tree).each do |association|
42
+ add_serializer_to_document association.serializer, document, serialized_keys, include_tree[association.key]
43
+ end
44
+ end
45
+
46
+ document
47
+ end
48
+
49
+ def root serializer_instance=serializer
50
+ serializer_instance.json_key.to_s.pluralize.to_sym
51
+ end
52
+
53
+ def meta
54
+ compiled_meta = {
55
+ primary_resource_collection: root
56
+ }
57
+
58
+ unless collection_serializer_given?
59
+ compiled_meta[:primary_resource_id] = resource_identifier_id
60
+ end
61
+
62
+ compiled_meta.merge(instance_options.fetch(:meta, Hash.new))
63
+ end
64
+
65
+ protected
66
+
67
+ def collection_serializer_given? serializer_instance=serializer
68
+ serializer_instance.respond_to? :each
69
+ end
70
+
71
+ def embed_key_for association
72
+ extension = '_id'
73
+ extension += 's' if collection_serializer_given? association.serializer
74
+ association_key = if association.options.has_key? :key
75
+ association.key
76
+ else
77
+ association.name.to_s.singularize
78
+ end
79
+
80
+ :"#{association_key}#{extension}" # this is hokey, refactor. Can we interrogate the serializer/association for this information?
81
+ end
82
+
83
+ def resource_identifier_id serializer_instance=serializer
84
+ if serializer_instance.respond_to?(:id)
85
+ serializer_instance.id
86
+ else
87
+ serializer_instance.object.id
88
+ end
89
+ end
90
+
91
+ def add_relationship_keys serializer_instance, attributes, include_tree
92
+ serializer_instance.associations(include_tree).each do |association|
93
+ attributes[embed_key_for(association)] = relationship_value_for association
94
+ end
95
+ end
96
+
97
+ def relationship_value_for association
98
+ return association.options[:virtual_value] if association.options[:virtual_value]
99
+ return unless association.serializer && association.serializer.object
100
+
101
+ if collection_serializer_given? association.serializer
102
+ association.serializer.map do |s|
103
+ resource_identifier_id s
104
+ end
105
+ else
106
+ resource_identifier_id association.serializer
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,50 @@
1
+ require 'sideload_serializer/adapter'
2
+
3
+ module SideloadSerializer
4
+ class CamelizedAdapter < Adapter
5
+ def self.deep_camelize_keys enumerable
6
+ if enumerable.is_a? Array
7
+ enumerable.each do |item|
8
+ deep_camelize_keys item if item.respond_to? :each
9
+ end
10
+ elsif
11
+ enumerable.keys.each do |key|
12
+ if key.is_a?(String) || key.is_a?(Symbol)
13
+ new_key = key.to_s.camelize(:lower)
14
+ new_key = new_key.to_sym if key.is_a? Symbol
15
+ if new_key != key
16
+ enumerable[new_key] = enumerable[key]
17
+ enumerable.delete key
18
+ end
19
+
20
+ if enumerable[new_key].respond_to? :each
21
+ deep_camelize_keys enumerable[new_key]
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ enumerable
28
+ end
29
+
30
+ def serializable_hash _options=nil
31
+ super.tap do |document|
32
+ self.class.deep_camelize_keys document
33
+ end
34
+ end
35
+
36
+ def meta
37
+ compiled_meta = {
38
+ primaryResourceCollection: root.to_s.camelize(:lower)
39
+ }
40
+
41
+ unless collection_serializer_given?
42
+ compiled_meta[:primaryResourceId] = resource_identifier_id
43
+ end
44
+
45
+ compiled_meta.merge!(self.class.deep_camelize_keys(instance_options.fetch(:meta, Hash.new).deep_dup))
46
+
47
+ compiled_meta
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module SideloadSerializer
2
+ VERSION = "0.0.1.pre1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sideload_serializer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sideload_serializer"
8
+ spec.version = SideloadSerializer::VERSION
9
+ spec.authors = ["Daniel Evans"]
10
+ spec.email = ["evans.daniel.n@gmail.com"]
11
+
12
+ spec.summary = %q{A simple Sideloaded JSON Adapter for Active Model Serializers}
13
+ spec.homepage = "https://github.com/rocketmade/sideload_serializer"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "active_model_serializers", ">= 0.10.0.rc3"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sideload_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Evans
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_model_serializers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0.rc3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0.rc3
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.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
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.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - evans.daniel.n@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/sideload_serializer.rb
86
+ - lib/sideload_serializer/adapter.rb
87
+ - lib/sideload_serializer/camelized_adapter.rb
88
+ - lib/sideload_serializer/version.rb
89
+ - sideload_serializer.gemspec
90
+ homepage: https://github.com/rocketmade/sideload_serializer
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">"
106
+ - !ruby/object:Gem::Version
107
+ version: 1.3.1
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A simple Sideloaded JSON Adapter for Active Model Serializers
114
+ test_files: []