media_types-serialization 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.idea/.rakeTasks +7 -0
  4. data/.idea/dictionaries/Derk_Jan.xml +7 -0
  5. data/.idea/inspectionProfiles/Project_Default.xml +6 -0
  6. data/.idea/media_types-serialization.iml +55 -0
  7. data/.idea/misc.xml +7 -0
  8. data/.idea/modules.xml +8 -0
  9. data/.idea/runConfigurations/test.xml +20 -0
  10. data/.idea/vcs.xml +6 -0
  11. data/.travis.yml +17 -0
  12. data/CHANGELOG.md +5 -0
  13. data/CODE_OF_CONDUCT.md +74 -0
  14. data/Gemfile +4 -0
  15. data/Gemfile.lock +76 -0
  16. data/LICENSE.txt +21 -0
  17. data/README.md +209 -0
  18. data/Rakefile +10 -0
  19. data/bin/console +14 -0
  20. data/bin/setup +8 -0
  21. data/lib/media_types/serialization.rb +194 -0
  22. data/lib/media_types/serialization/base.rb +138 -0
  23. data/lib/media_types/serialization/error.rb +7 -0
  24. data/lib/media_types/serialization/migrations_command.rb +38 -0
  25. data/lib/media_types/serialization/migrations_support.rb +41 -0
  26. data/lib/media_types/serialization/mime_type_support.rb +55 -0
  27. data/lib/media_types/serialization/no_content_type_given.rb +11 -0
  28. data/lib/media_types/serialization/no_media_type_serializers.rb +11 -0
  29. data/lib/media_types/serialization/no_serializer_for_content_type.rb +15 -0
  30. data/lib/media_types/serialization/renderer.rb +31 -0
  31. data/lib/media_types/serialization/renderer/register.rb +4 -0
  32. data/lib/media_types/serialization/version.rb +5 -0
  33. data/lib/media_types/serialization/wrapper.rb +15 -0
  34. data/lib/media_types/serialization/wrapper/html_wrapper.rb +42 -0
  35. data/lib/media_types/serialization/wrapper/media_collection_wrapper.rb +48 -0
  36. data/lib/media_types/serialization/wrapper/media_index_wrapper.rb +48 -0
  37. data/lib/media_types/serialization/wrapper/media_object_wrapper.rb +45 -0
  38. data/lib/media_types/serialization/wrapper/media_wrapper.rb +32 -0
  39. data/lib/media_types/serialization/wrapper/root_key.rb +20 -0
  40. data/media_types-serialization.gemspec +48 -0
  41. metadata +212 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f81671e23c69f1315a0bd9d07ad55ddf0d5e9f2a2c46f1597a905525bcb19bef
4
+ data.tar.gz: 3dc428be52dafa3a00538b947f4126cb884dd1a6ae1613fe0e6e2dd1e307177e
5
+ SHA512:
6
+ metadata.gz: 276c8eed325348f3fd7b270e363349698d1ca94fd68b3804e74ff0c1f52791dc6215f92be8837588eca805fe7437ebac312e636df6e8c014bd86739334054572
7
+ data.tar.gz: 1cd405531bba571c8f78626444bfb239f5d112ce2f19726319c8eda7ce87b5865043096a3146be5ed045dc0287ca038ebe5b86f1b9b9029833133d3eb59165ac
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .idea/workspace.xml
11
+ .idea/usage.statistics.xml
12
+ .idea/tasks.xml
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build media_types-serialization-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install media_types-serialization-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install media_types-serialization-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push media_types-serialization-0.1.0.gem to TODO: Set to 'http://mygemserver.com'" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run tests" fullCmd="test" taksId="test" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
@@ -0,0 +1,7 @@
1
+ <component name="ProjectDictionaryState">
2
+ <dictionary name="Derk-Jan">
3
+ <words>
4
+ <w>migrator</w>
5
+ </words>
6
+ </dictionary>
7
+ </component>
@@ -0,0 +1,6 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="Rubocop" enabled="false" level="WARNING" enabled_by_default="false" />
5
+ </profile>
6
+ </component>
@@ -0,0 +1,55 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared>
5
+ <configuration default="false" name="test" type="RakeRunConfigurationType" factoryName="Rake">
6
+ <module name="media_types-serialization" />
7
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="" />
8
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$" />
9
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
10
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
11
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
12
+ <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="true" />
13
+ <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
14
+ <EXTENSION ID="RubyCoverageRunConfigurationExtension" track_test_folders="true" runner="rcov" />
15
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_NAME" VALUE="test" />
16
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ARGS" VALUE="" />
17
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ATTACHED_TEST_FRAMEWORKS" VALUE=":test_unit " />
18
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_TRACE" VALUE="false" />
19
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_DRYRUN" VALUE="false" />
20
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_PREREQS" VALUE="false" />
21
+ <method v="2" />
22
+ </configuration>
23
+ </shared>
24
+ </component>
25
+ <component name="NewModuleRootManager">
26
+ <content url="file://$MODULE_DIR$" />
27
+ <orderEntry type="jdk" jdkName="ruby-2.5.3-p105" jdkType="RUBY_SDK" />
28
+ <orderEntry type="sourceFolder" forTests="false" />
29
+ <orderEntry type="library" scope="PROVIDED" name="actionpack (v5.2.2, ruby-2.5.3-p105) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="actionview (v5.2.2, ruby-2.5.3-p105) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="activesupport (v5.2.2, ruby-2.5.3-p105) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="builder (v3.2.3, ruby-2.5.3-p105) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.0.1, ruby-2.5.3-p105) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.4, ruby-2.5.3-p105) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="crass (v1.0.4, ruby-2.5.3-p105) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="erubi (v1.8.0, ruby-2.5.3-p105) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="http_headers-accept (v0.1.0, ruby-2.5.3-p105) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="http_headers-link (v0.1.0, ruby-2.5.3-p105) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="http_headers-utils (v0.1.1, ruby-2.5.3-p105) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="i18n (v1.5.3, ruby-2.5.3-p105) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="loofah (v2.2.3, ruby-2.5.3-p105) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="media_types (v0.6.0, ruby-2.5.3-p105) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="mini_portile2 (v2.4.0, ruby-2.5.3-p105) [gem]" level="application" />
44
+ <orderEntry type="library" scope="PROVIDED" name="minitest (v5.11.3, ruby-2.5.3-p105) [gem]" level="application" />
45
+ <orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.10.1, ruby-2.5.3-p105) [gem]" level="application" />
46
+ <orderEntry type="library" scope="PROVIDED" name="oj (v3.7.8, ruby-2.5.3-p105) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="rack (v2.0.6, ruby-2.5.3-p105) [gem]" level="application" />
48
+ <orderEntry type="library" scope="PROVIDED" name="rack-test (v1.1.0, ruby-2.5.3-p105) [gem]" level="application" />
49
+ <orderEntry type="library" scope="PROVIDED" name="rails-dom-testing (v2.0.3, ruby-2.5.3-p105) [gem]" level="application" />
50
+ <orderEntry type="library" scope="PROVIDED" name="rails-html-sanitizer (v1.0.4, ruby-2.5.3-p105) [gem]" level="application" />
51
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.5.3-p105) [gem]" level="application" />
52
+ <orderEntry type="library" scope="PROVIDED" name="thread_safe (v0.3.6, ruby-2.5.3-p105) [gem]" level="application" />
53
+ <orderEntry type="library" scope="PROVIDED" name="tzinfo (v1.2.5, ruby-2.5.3-p105) [gem]" level="application" />
54
+ </component>
55
+ </module>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.4.4-p296" project-jdk-type="RUBY_SDK" />
7
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/media_types-serialization.iml" filepath="$PROJECT_DIR$/.idea/media_types-serialization.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,20 @@
1
+ <component name="ProjectRunConfigurationManager">
2
+ <configuration default="false" name="test" type="RakeRunConfigurationType" factoryName="Rake">
3
+ <module name="media_types-serialization" />
4
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="" />
5
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$" />
6
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
7
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
8
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
9
+ <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="true" />
10
+ <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
11
+ <EXTENSION ID="RubyCoverageRunConfigurationExtension" track_test_folders="true" runner="rcov" />
12
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_NAME" VALUE="test" />
13
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ARGS" VALUE="" />
14
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ATTACHED_TEST_FRAMEWORKS" VALUE=":test_unit " />
15
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_TRACE" VALUE="false" />
16
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_DRYRUN" VALUE="false" />
17
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_PREREQS" VALUE="false" />
18
+ <method v="2" />
19
+ </configuration>
20
+ </component>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,17 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5
7
+ - 2.6
8
+ - ruby-head
9
+ before_install:
10
+ - gem install bundler -v 2.0.1
11
+ - gem update --system
12
+ - gem --version
13
+ matrix:
14
+ allow_failures:
15
+ - rvm: ruby-head
16
+ install:
17
+ - bundle install --with development --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle}
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ :baby: initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at derk-jan+github@karrenbeld.info. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in media_types-serialization.gemspec
4
+ gemspec
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ media_types-serialization (0.1.0)
5
+ actionpack (>= 4.0.0)
6
+ activesupport (>= 4.0.0)
7
+ http_headers-accept (< 1.0.0)
8
+ http_headers-link (< 1.0.0)
9
+ media_types (>= 0.6.0)
10
+ oj (>= 3.5.0)
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ actionpack (5.2.2)
16
+ actionview (= 5.2.2)
17
+ activesupport (= 5.2.2)
18
+ rack (~> 2.0)
19
+ rack-test (>= 0.6.3)
20
+ rails-dom-testing (~> 2.0)
21
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
22
+ actionview (5.2.2)
23
+ activesupport (= 5.2.2)
24
+ builder (~> 3.1)
25
+ erubi (~> 1.4)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
28
+ activesupport (5.2.2)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 0.7, < 2)
31
+ minitest (~> 5.1)
32
+ tzinfo (~> 1.1)
33
+ builder (3.2.3)
34
+ concurrent-ruby (1.1.4)
35
+ crass (1.0.4)
36
+ erubi (1.8.0)
37
+ http_headers-accept (0.1.0)
38
+ http_headers-utils (< 1.0.0)
39
+ http_headers-link (0.1.0)
40
+ http_headers-utils (< 1.0.0)
41
+ http_headers-utils (0.1.1)
42
+ i18n (1.5.3)
43
+ concurrent-ruby (~> 1.0)
44
+ loofah (2.2.3)
45
+ crass (~> 1.0.2)
46
+ nokogiri (>= 1.5.9)
47
+ media_types (0.6.0)
48
+ mini_portile2 (2.4.0)
49
+ minitest (5.11.3)
50
+ nokogiri (1.10.1-x64-mingw32)
51
+ mini_portile2 (~> 2.4.0)
52
+ oj (3.7.8)
53
+ rack (2.0.6)
54
+ rack-test (1.1.0)
55
+ rack (>= 1.0, < 3)
56
+ rails-dom-testing (2.0.3)
57
+ activesupport (>= 4.2.0)
58
+ nokogiri (>= 1.6)
59
+ rails-html-sanitizer (1.0.4)
60
+ loofah (~> 2.2, >= 2.2.2)
61
+ rake (10.5.0)
62
+ thread_safe (0.3.6)
63
+ tzinfo (1.2.5)
64
+ thread_safe (~> 0.1)
65
+
66
+ PLATFORMS
67
+ x64-mingw32
68
+
69
+ DEPENDENCIES
70
+ bundler (~> 2.0)
71
+ media_types-serialization!
72
+ minitest (~> 5.0)
73
+ rake (~> 10.0)
74
+
75
+ BUNDLED WITH
76
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Derk-Jan Karrenbeld
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,209 @@
1
+ # MediaTypes::Serialization
2
+
3
+ [![Build Status: master](https://travis-ci.com/XPBytes/media_types-serialization.svg)](https://travis-ci.com/XPBytes/media_types-serialization)
4
+ [![Gem Version](https://badge.fury.io/rb/media_types-serialization.svg)](https://badge.fury.io/rb/media_types-serialization)
5
+ [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
6
+
7
+ Add media types supported serialization using your favourite serializer
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'media_types-serialization'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install media_types-serialization
24
+
25
+ ## Usage
26
+
27
+ In order to use media type serialization you only need to do 2 things:
28
+
29
+ ### Serializer
30
+
31
+ Add a serializer that can serialize a certain media type. The `to_hash` function will be called _explicitely_ in your
32
+ controller, so you can always use your own, favourite serializer here to do the hefty work. This gem does provide some
33
+ easy tools, usually enough to do most serialization.
34
+
35
+ ```ruby
36
+ class Book < ApplicationRecord
37
+ class Serializer < MediaTypes::Serialization::Base
38
+ serializes_media_type MyNamespace::MediaTypes::Book
39
+
40
+ def fields
41
+ if current_media_type.create?
42
+ return %i[name author]
43
+ end
44
+
45
+ %i[name author updated_at views]
46
+ end
47
+
48
+ def to_hash
49
+ extract(serializable, fields).tap do |result|
50
+ result[:_links] = extract_links unless current_media_type.create?
51
+ end
52
+ end
53
+
54
+ alias to_h to_hash
55
+
56
+ protected
57
+
58
+ def extract_self
59
+ # A serializer gets the controller as context
60
+ { href: context.api_book_url(serializable) }
61
+ end
62
+
63
+ private
64
+
65
+ def extract_links
66
+ {
67
+ 'self': extract_self,
68
+ 'signatures': { href: context.api_book_signatures_url(serializable) }
69
+ }
70
+ end
71
+ end
72
+ end
73
+ ```
74
+ By default, The passed in `MediaType` gets converted into a constructable (via `to_constructable`) and invoked with the
75
+ current `view` (e.g. `create`, `index`, `collection` or ` `). This means that by default it will be able to serialize
76
+ the latest version you `MediaType` is reporting. The best way to supply your media type is via the [`media_types`](https://github.com/SleeplessByte/media-types-ruby) gem.
77
+
78
+ #### Multiple suffixes, one serializer
79
+ By default, the media renderer will automatically detect and inject the following:
80
+ - suffix `+json` if you define `to_json`
81
+ - suffix `+xml` if you define `to_xml`
82
+ - type `text/html` if you define `to_html`
83
+
84
+ The only limitation is that these methods need to be defined on the serializer class directly, and not in a super class.
85
+
86
+ If you do _not_ define these methods, only the `default` suffix / type will be used, `accepts_html` for the `text/html`
87
+ content-type.
88
+
89
+ #### Migrations (versions)
90
+ If the serializer can serialize multiple _versions_, you can supply them through `additional_versions: [2, 3]`. A way to
91
+ handle this is via backward migrations, meaning you'll migrate from the current version back to an older version.
92
+
93
+ ```ruby
94
+ class Book < ApplicationRecord
95
+ class BasicSerializer < MediaTypes::Serialization::Base
96
+
97
+ # Maybe it's currently at version 2, so tell the base that this also serializes version 1
98
+ # You can also use a range to_a: (1...4).to_a
99
+ #
100
+ serializes_media_type MyNamespace::MediaTypes::Book, additional_versions: [1]
101
+
102
+ def to_hash
103
+ # This enables migrations right when it's being serialized
104
+ #
105
+ migrate do
106
+ extract(serializable, fields).tap do |result|
107
+ result[:_links] = extract_links unless current_media_type.create?
108
+ end
109
+ end
110
+ end
111
+
112
+ # This defines migrations. You can use classes, commands or anything else to execute this code
113
+ # but inline migrations work fine if you don't have a lot of them.
114
+ backward_migrations do
115
+
116
+ # This is called if the version requested is 1 _or_ lower. This means you can compose your migrations. The
117
+ # migrations with a _lower_ version than the requested version are NOT executed.
118
+ version 1 do |result|
119
+ result.tap do |r|
120
+ if r.key?(:views)
121
+ r[:views_count] = r.delete(:views)
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ ```
129
+
130
+ ### Controller
131
+
132
+ In your base controller, or wherever you'd like, include the `MediaTypes::Serialization` concern. In the controller that
133
+ uses the serialization, you need to explicitely `accept` it if you want to use the built-in lookups.
134
+
135
+ ```ruby
136
+ require 'media_types/serialization'
137
+ require 'media_types/serialization/renderer/register'
138
+
139
+ class ApiController < ActionController::API
140
+ include MediaTypes::Serialization
141
+ end
142
+
143
+ class BookController < ApiController
144
+
145
+ accept_serialization(Book::BasicSerializer, accept_html: false, only: %i[show])
146
+ accept_html(Book::CoverHtmlSerializer, only: %i[show])
147
+ freeze_accepted_media!
148
+
149
+ def show
150
+ render media: serialize_media(@book)
151
+ end
152
+ end
153
+ ```
154
+
155
+ ### HTML output
156
+
157
+ You can define HTML outputs for example by creating a serializer that accepts `text/html`. At this moment, there may
158
+ only be one (1) active HTML serializer for each action; a single controller can have multiple registered, but never for
159
+ the same preconditions in `before_action` (because how else would it know which one to pick?).
160
+
161
+ Use the `render` method to generate your HTML:
162
+ ```ruby
163
+ class Book::CoverHtmlSerializer < MediaTypes::Serialization::Base
164
+ # Tell the serializer that this accepts HTML, but this is also signaled by `to_html`
165
+ serializes_html
166
+
167
+ def to_html
168
+ ApplicationController.render(
169
+ 'serializers/book/cover',
170
+ assigns: {
171
+ title: extract_title,
172
+ image: resolve_file_url(covers.first&.version_url('small')),
173
+ description: extract_description,
174
+ language_links: language_links,
175
+ },
176
+ layout: false
177
+ )
178
+ end
179
+
180
+ # Naturally you have to define extract_title, etc etc
181
+ end
182
+ ```
183
+
184
+ ### Wrapping output
185
+
186
+ By convention, `index` views are wrapped in `_index: [items]`, `collection` views are wrapped in `_embedded: [items]`
187
+ and `create` / no views are wrapped in `[ROOT_KEY]: item`. This is currently only enabled for `to_json` serialization
188
+ but planned for `xml` as well.
189
+
190
+ This behaviour can not be turned of as of writing.
191
+
192
+ ### Related
193
+
194
+ - [`MediaTypes`](https://github.com/SleeplessByte/media-types-ruby): :gem: Library to create media type definitions, schemes and validations
195
+ - [`MediaTypes::Deserialization`](https://github.com/XPBytes/media_types-deserialization): :cyclone: Add media types supported deserialization using your favourite parser, and media type validation.
196
+ - [`MediaTypes::Validation`](https://github.com/XPBytes/media_types-validation): :heavy_exclamation_mark: Response validations according to a media-type
197
+
198
+ ## Development
199
+
200
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
201
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
202
+
203
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
204
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
205
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
206
+
207
+ ## Contributing
208
+
209
+ Bug reports and pull requests are welcome on GitHub at [XPBytes/media_types-serialization](https://github.com/XPBytes/media_types-serialization).