media_types-deserialization 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6fee00006a57b04944e81dbf523c45f93dcce8bf3b4297eefbf19b4d390e3fa5
4
+ data.tar.gz: 119622b47be1ee5d10d0bc73fbd4319f079c8d65b2c405070bb74bd675abbaec
5
+ SHA512:
6
+ metadata.gz: 54df1ef93343f130c527d60d1ce21a06ff299a6859391cc4d8109aa69824703e6b334d4c3a6b27b52376c742b00303b8b13b90966293f8f4cb725d0cf0872b46
7
+ data.tar.gz: 378d743f205ae9ae6947118aa613450605a7292c79a147f4a546d9df618aa74c39d53073bd43f301b00cd570d8768919626e5f8ad207f6ccfcc1b5f95b43e725
data/.gitignore ADDED
@@ -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
data/.idea/.rakeTasks ADDED
@@ -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-deserialization-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-deserialization-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install media_types-deserialization-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-deserialization-0.1.0.gem to rubygems.org" 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,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,54 @@
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_type-deserialization" />
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-content_type (v0.1.1, ruby-2.5.3-p105) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="http_headers-utils (v0.2.0, ruby-2.5.3-p105) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="i18n (v1.5.3, ruby-2.5.3-p105) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="loofah (v2.2.3, ruby-2.5.3-p105) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="media_types (v0.6.0, ruby-2.5.3-p105) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="mini_portile2 (v2.4.0, ruby-2.5.3-p105) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="minitest (v5.11.3, ruby-2.5.3-p105) [gem]" level="application" />
44
+ <orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.10.1, ruby-2.5.3-p105) [gem]" level="application" />
45
+ <orderEntry type="library" scope="PROVIDED" name="oj (v3.7.8, ruby-2.5.3-p105) [gem]" level="application" />
46
+ <orderEntry type="library" scope="PROVIDED" name="rack (v2.0.6, ruby-2.5.3-p105) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="rack-test (v1.1.0, ruby-2.5.3-p105) [gem]" level="application" />
48
+ <orderEntry type="library" scope="PROVIDED" name="rails-dom-testing (v2.0.3, ruby-2.5.3-p105) [gem]" level="application" />
49
+ <orderEntry type="library" scope="PROVIDED" name="rails-html-sanitizer (v1.0.4, ruby-2.5.3-p105) [gem]" level="application" />
50
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.5.3-p105) [gem]" level="application" />
51
+ <orderEntry type="library" scope="PROVIDED" name="thread_safe (v0.3.6, ruby-2.5.3-p105) [gem]" level="application" />
52
+ <orderEntry type="library" scope="PROVIDED" name="tzinfo (v1.2.5, ruby-2.5.3-p105) [gem]" level="application" />
53
+ </component>
54
+ </module>
data/.idea/misc.xml ADDED
@@ -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>
data/.idea/modules.xml ADDED
@@ -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_type-deserialization.iml" filepath="$PROJECT_DIR$/.idea/media_type-deserialization.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_type-deserialization" />
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>
data/.idea/vcs.xml ADDED
@@ -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>
data/.travis.yml ADDED
@@ -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}
data/CHANGELOG.md ADDED
@@ -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_type-deserialization.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ media_types-deserialization (0.1.0)
5
+ activesupport (>= 4.0.0)
6
+ http_headers-content_type (>= 0.1.1, < 1.0.0)
7
+ media_types (>= 0.6.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionpack (5.2.2)
13
+ actionview (= 5.2.2)
14
+ activesupport (= 5.2.2)
15
+ rack (~> 2.0)
16
+ rack-test (>= 0.6.3)
17
+ rails-dom-testing (~> 2.0)
18
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
19
+ actionview (5.2.2)
20
+ activesupport (= 5.2.2)
21
+ builder (~> 3.1)
22
+ erubi (~> 1.4)
23
+ rails-dom-testing (~> 2.0)
24
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
25
+ activesupport (5.2.2)
26
+ concurrent-ruby (~> 1.0, >= 1.0.2)
27
+ i18n (>= 0.7, < 2)
28
+ minitest (~> 5.1)
29
+ tzinfo (~> 1.1)
30
+ builder (3.2.3)
31
+ concurrent-ruby (1.1.4)
32
+ crass (1.0.4)
33
+ erubi (1.8.0)
34
+ http_headers-content_type (0.1.1)
35
+ http_headers-utils (>= 0.2.0, < 1.0.0)
36
+ http_headers-utils (0.2.0)
37
+ i18n (1.5.3)
38
+ concurrent-ruby (~> 1.0)
39
+ loofah (2.2.3)
40
+ crass (~> 1.0.2)
41
+ nokogiri (>= 1.5.9)
42
+ media_types (0.6.0)
43
+ mini_portile2 (2.4.0)
44
+ minitest (5.11.3)
45
+ nokogiri (1.10.1-x64-mingw32)
46
+ mini_portile2 (~> 2.4.0)
47
+ oj (3.7.8)
48
+ rack (2.0.6)
49
+ rack-test (1.1.0)
50
+ rack (>= 1.0, < 3)
51
+ rails-dom-testing (2.0.3)
52
+ activesupport (>= 4.2.0)
53
+ nokogiri (>= 1.6)
54
+ rails-html-sanitizer (1.0.4)
55
+ loofah (~> 2.2, >= 2.2.2)
56
+ rake (10.5.0)
57
+ thread_safe (0.3.6)
58
+ tzinfo (1.2.5)
59
+ thread_safe (~> 0.1)
60
+
61
+ PLATFORMS
62
+ x64-mingw32
63
+
64
+ DEPENDENCIES
65
+ actionpack (>= 4.0.0)
66
+ bundler (~> 2.0)
67
+ media_types-deserialization!
68
+ minitest (~> 5.0)
69
+ oj (>= 3.5.0)
70
+ rake (~> 10.0)
71
+
72
+ BUNDLED WITH
73
+ 2.0.1
data/LICENSE.txt ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,156 @@
1
+ # MediaTypes::Deserialization
2
+
3
+ [![Build Status: master](https://travis-ci.com/XPBytes/media_types-deserialization.svg)](https://travis-ci.com/XPBytes/media_types-deserialization)
4
+ [![Gem Version](https://badge.fury.io/rb/media_types-deserialization.svg)](https://badge.fury.io/rb/media_types-deserialization)
5
+ [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
6
+
7
+ Add media types supported deserialization using your favourite deserializer, and (when supported and provided) media
8
+ type validation.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'media_types-deserialization'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install media_types-deserialization
25
+
26
+ ## Usage
27
+
28
+ All logic lives in the `MediaTypes::Deserialization` and the main method you'll use is `media_type_params`. This works
29
+ very similarly as `params` in `Rack` (and therefore `Rails`) applications, but only gives back what the implicit or
30
+ explicit deserializer gives back.
31
+
32
+ ```ruby
33
+ require 'media_type/deserialization'
34
+
35
+ class BaseController
36
+ include MediaTypes::Deserialization
37
+
38
+ rescue_from ContentFormatError, with: :bad_request # 400
39
+ rescue_from ContentTypeNotRecognised, with: :unsupported_media_type # 415
40
+ rescue_from ContentDoesNotMatchContentType, with: :unprocessable_entity # 422
41
+ end
42
+ ```
43
+
44
+ ### Content Type lookup (symbol)
45
+
46
+ If you _don't_ provide the `lookup_content_type_symbol` configuration, it requires `'action_dispatch/http/mime_type'` to
47
+ be present in order to look-up the content type symbol. This is true for `Rails` applications by default.
48
+
49
+ ```ruby
50
+ MediaTypes::Deserialization.configure do
51
+ self.lookup_content_type_symbol = lambda do |content_type|
52
+ # For example use a lookup map
53
+ KNOWN_CONTENT_TYPES_TO_SYMBOL[content_type]
54
+
55
+ # Or alternatively use matching
56
+ content_type.include?('json') ? :json : nil
57
+ end
58
+ end
59
+ ```
60
+
61
+ See below if you're using the `media_types` gem.
62
+
63
+ ### Deserializer lookup
64
+
65
+ If you _don't_ provide the `lookup_deserializer_by_symbol` configuration, it currently can only deserialize `json` and
66
+ will only do so if the symbol is `:json`, requiring `oj`. If you don't want this behaviour, define it.
67
+
68
+ ```ruby
69
+ MediaTypes::Deserialization.configure do
70
+ self.lookup_deserializer_by_symbol = lambda do |symbol|
71
+ case symbol
72
+ when :json
73
+ return CustomJsonDeserializer
74
+ when :xml
75
+ when :html
76
+ when :xhtml
77
+ return CustomXmlDeserializer
78
+ else
79
+ nil
80
+ end
81
+ end
82
+ end
83
+ ```
84
+
85
+ ### Media Type lookup and validation
86
+
87
+ If you *want* media type validation, for example via the `media_types` gem, provide the `lookup_media_type_by_symbol`
88
+ option and return the media types. The easiest way to accomplish this is tracking your registerables when you register
89
+ the media types, and creating a Lookup Map like so:
90
+
91
+ ```ruby
92
+ # In some initializer that defines the media types,
93
+ # given a module MyDomain::MediaTypes which holds many media types
94
+
95
+ require 'my_domain/media_types'
96
+ require 'media_types/integrations/actionpack'
97
+
98
+ registerables = []
99
+ MyDomain::MediaTypes.module_exec do
100
+ registerables.concat self::Author.register
101
+ registerables.concat self::Book.register
102
+ registerables.concat self::Configuration.register
103
+ registerables.concat self::Errors.register
104
+ registerables.concat self::Signature.register
105
+
106
+ # Create lookup table by string (content-type) => media type
107
+ lookup = registerables.flatten.each_with_object({}) do |registerable, hash|
108
+ [registerable.media_type, *registerable.aliases].each do |type|
109
+ hash[String(type)] = registerable
110
+ end
111
+ end.freeze
112
+
113
+ # Create lookup table by symbol => media_type
114
+ lookup_by_symbol = registerables.flatten.each_with_object({}) do |registerable, hash|
115
+ hash[String(registerable.symbol).to_sym] = registerable
116
+ end.freeze
117
+
118
+ const_set(:LOOKUP, lookup)
119
+ const_set(:LOOKUP_BY_SYMBOL, lookup_by_symbol)
120
+ end
121
+ ```
122
+
123
+ At this point you can re-use those lookup tables for both the media type lookup and the symbol lookup:
124
+
125
+ ```ruby
126
+ MediaTypes::Deserialization.configure do
127
+ self.lookup_content_type_symbol = lambda do |content_type|
128
+ registerable = MyDomain::MediaTypes.const_get(:Lookup).fetch(content_type) { nil }
129
+ registerable&.symbol
130
+ end
131
+
132
+ self.lookup_media_type_by_symbol = lambda do |symbol|
133
+ registerable = MyDomain::MediaTypes.const_get(:LOOKUP_BY_SYMBOL).fetch(symbol) { nil }
134
+ registerable&.media_type
135
+ end
136
+ end
137
+ ```
138
+
139
+ ### Related
140
+
141
+ - [`MediaTypes`](https://github.com/SleeplessByte/media-types-ruby): :gem: Library to create media type definitions, schemes and validations
142
+ - [`MediaTypes::Serialization`](https://github.com/XPBytes/media_types-serialization): :cyclone: Add media types supported serialization using your favourite serializer
143
+ - [`MediaTypes::Validation`](https://github.com/XPBytes/media_types-validation): :heavy_exclamation_mark: Response validations according to a media-type
144
+
145
+ ## Development
146
+
147
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
148
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
149
+
150
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
151
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
152
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
153
+
154
+ ## Contributing
155
+
156
+ Bug reports and pull requests are welcome on GitHub at [XPBytes/media_types-deserialization](https://github.com/XPBytes/media_types-deserialization).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "media_types/deserialization"
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(__FILE__)
data/bin/setup ADDED
@@ -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,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "media_types/deserialization/version"
4
+
5
+ require 'active_support/concern'
6
+ require 'active_support/core_ext/module/attribute_accessors'
7
+
8
+ require 'media_types/scheme/errors'
9
+
10
+ require 'media_types/deserialization/error'
11
+ require 'media_types/deserialization/content_does_not_match_content_type'
12
+ require 'media_types/deserialization/content_format_error'
13
+ require 'media_types/deserialization/content_type_not_recognised'
14
+
15
+ require 'http_headers/content_type'
16
+
17
+ module MediaTypes
18
+ module Deserialization
19
+ extend ActiveSupport::Concern
20
+
21
+ PARAMETERS_KEY = 'api.media_type_deserializer.request.parameters'
22
+
23
+ DEFAULT_JSON_DESERIALIZER = lambda do |raw_post|
24
+ require 'oj'
25
+ data = Oj.load(raw_post, Oj.default_options) || {}
26
+ data.is_a?(::Hash) ? data : { _json: data }
27
+ end
28
+
29
+ DEFAULT_LOOKUP_TABLE = {
30
+ 'application/json' => :json,
31
+ 'text/xml' => :xml,
32
+ 'text/html' => :html
33
+ }
34
+
35
+ mattr_accessor :lookup_content_type_symbol, :lookup_deserializer_by_symbol, :lookup_media_type_by_symbol
36
+
37
+ def self.configure(&block)
38
+ block_given? ? instance_exec(self, &block) : self
39
+ end
40
+
41
+ # Returns both GET and POST \parameters in a single hash.
42
+ def media_type_params
43
+ # If this has been parsed before, e.g. in a middleware, return directly
44
+ previous_params = request.get_header(PARAMETERS_KEY)
45
+ return previous_params if previous_params
46
+
47
+ deserialize_content.tap do |params|
48
+ params.merge!(request.path_parameters)
49
+ request.set_header(PARAMETERS_KEY, params)
50
+ end
51
+ rescue StandardError => ex
52
+ if defined?(::Oj::Error) && ex.is_a?(::Oj::Error)
53
+ raise ContentFormatError, 'Body is not valid JSON: ' + ex.message
54
+ end
55
+
56
+ raise ex
57
+ end
58
+
59
+ private
60
+
61
+ def request_content_type?
62
+ request.content_type && request.content_length.positive?
63
+ end
64
+
65
+ def request_content_type
66
+ @request_content_type ||= HttpHeaders::ContentType.new(request.content_type)&.content_type
67
+ end
68
+
69
+ def request_content_type_symbol
70
+ content_type = request_content_type
71
+ result = lookup_content_type_symbol.respond_to?(:call) ?
72
+ instance_exec(content_type, &lookup_content_type_symbol) :
73
+ lookup_content_type_via_mime_type(content_type)
74
+
75
+ raise ContentTypeNotRecognised(content_type) unless result
76
+ return result if result.is_a?(Symbol)
77
+ (result.respond_to?(:symbol) && result.symbol) || result.to_sym || DEFAULT_LOOKUP_TABLE[result.to_s]
78
+ end
79
+
80
+ def lookup_content_type_via_mime_type(content_type)
81
+ require 'action_dispatch/http/mime_type'
82
+ Mime::Type.lookup(content_type)
83
+ end
84
+
85
+ def deserializer_for_content_type(symbol = request_content_type_symbol)
86
+ if lookup_deserializer_by_symbol.respond_to?(:call)
87
+ deserializer = instance_exec(symbol, &lookup_deserializer_by_symbol)
88
+ return deserializer if deserializer
89
+ end
90
+
91
+ next_symbol = symbol
92
+
93
+ if lookup_media_type_by_symbol.respond_to?(:call)
94
+ next_symbol = instance_exec(symbol, &lookup_media_type_by_symbol)&.suffix&.to_sym
95
+ return deserializer_for_content_type(next_symbol) if next_symbol != symbol && next_symbol
96
+ end
97
+
98
+ return nil unless symbol == :json || next_symbol == :json
99
+ DEFAULT_JSON_DESERIALIZER
100
+ end
101
+
102
+ def deserialize_content(body = request.body)
103
+ symbol = request_content_type_symbol
104
+ deserializer = deserializer_for_content_type(symbol)
105
+ return {} unless deserializer
106
+
107
+ deserializer.call(body).tap do |deserialized|
108
+ if lookup_media_type_by_symbol.respond_to?(:call)
109
+ media_type = instance_exec(symbol, &lookup_media_type_by_symbol)
110
+ media_type&.validate!(deserialized)
111
+ end
112
+ end
113
+ rescue ::MediaTypes::Scheme::ValidationError => ex
114
+ raise ContentDoesNotMatchContentType, ex
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,12 @@
1
+ require 'media_types/deserialization/error'
2
+
3
+ module MediaTypes
4
+ module Deserialization
5
+ class ContentDoesNotMatchContentType < Error
6
+ def initialize(source)
7
+ set_backtrace(source.backtrace)
8
+ super source.message
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'media_types/deserialization/error'
2
+
3
+ module MediaTypes
4
+ module Deserialization
5
+ class ContentFormatError < defined?(::ActionController) ? ::ActionController::BadRequest : Error
6
+ def initialize(message)
7
+ super message
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require 'media_types/deserialization/error'
2
+
3
+ module MediaTypes
4
+ module Deserialization
5
+ class ContentTypeNotRecognised < Error
6
+ def initialize(content_type)
7
+ set_backtrace(source.backtrace)
8
+ super format(
9
+ 'The Content-Type: %<content_type>s is not recognised or supported',
10
+ content_type: content_type
11
+ )
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module MediaTypes
2
+ module Deserialization
3
+ class Error < RuntimeError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module MediaTypes
2
+ module Deserialization
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "media_types/deserialization/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "media_types-deserialization"
8
+ spec.version = MediaTypes::Deserialization::VERSION
9
+ spec.authors = ["Derk-Jan Karrenbeld"]
10
+ spec.email = ["derk-jan@xpbytes.com"]
11
+
12
+ spec.summary = 'Add media types supported deserialization using your favourite parser'
13
+ spec.homepage = 'https://github.com/XPBytes/media_types-deserialization'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = spec.homepage
23
+ spec.metadata["changelog_uri"] = spec.homepage + '/CHANGELOG.md'
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency 'activesupport', '>= 4.0.0'
39
+ spec.add_dependency 'media_types', '>= 0.6.0'
40
+ spec.add_dependency 'http_headers-content_type', '>= 0.1.1', '< 1.0.0'
41
+
42
+ spec.add_development_dependency 'actionpack', '>= 4.0.0'
43
+ spec.add_development_dependency 'oj', '>= 3.5.0'
44
+ spec.add_development_dependency "bundler", "~> 2.0"
45
+ spec.add_development_dependency "rake", "~> 10.0"
46
+ spec.add_development_dependency "minitest", "~> 5.0"
47
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: media_types-deserialization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Derk-Jan Karrenbeld
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-17 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.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: media_types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: http_headers-content_type
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.1
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: 1.0.0
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 0.1.1
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: 1.0.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: actionpack
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 4.0.0
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 4.0.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: oj
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 3.5.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.5.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: bundler
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '10.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '10.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: minitest
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '5.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '5.0'
131
+ description:
132
+ email:
133
+ - derk-jan@xpbytes.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".gitignore"
139
+ - ".idea/.rakeTasks"
140
+ - ".idea/inspectionProfiles/Project_Default.xml"
141
+ - ".idea/media_type-deserialization.iml"
142
+ - ".idea/misc.xml"
143
+ - ".idea/modules.xml"
144
+ - ".idea/runConfigurations/test.xml"
145
+ - ".idea/vcs.xml"
146
+ - ".travis.yml"
147
+ - CHANGELOG.md
148
+ - CODE_OF_CONDUCT.md
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - lib/media_types/deserialization.rb
157
+ - lib/media_types/deserialization/content_does_not_match_content_type.rb
158
+ - lib/media_types/deserialization/content_format_error.rb
159
+ - lib/media_types/deserialization/content_type_not_recognised.rb
160
+ - lib/media_types/deserialization/error.rb
161
+ - lib/media_types/deserialization/version.rb
162
+ - media_types-deserialization.gemspec
163
+ homepage: https://github.com/XPBytes/media_types-deserialization
164
+ licenses:
165
+ - MIT
166
+ metadata:
167
+ homepage_uri: https://github.com/XPBytes/media_types-deserialization
168
+ source_code_uri: https://github.com/XPBytes/media_types-deserialization
169
+ changelog_uri: https://github.com/XPBytes/media_types-deserialization/CHANGELOG.md
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubyforge_project:
186
+ rubygems_version: 2.7.6
187
+ signing_key:
188
+ specification_version: 4
189
+ summary: Add media types supported deserialization using your favourite parser
190
+ test_files: []