easy-jsonapi 1.0.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.dockerignore +7 -0
- data/.github/workflows/codecov.yml +30 -0
- data/.github/workflows/{publish-gem.yml → publish.yml} +28 -31
- data/.gitignore +37 -0
- data/.rspec_status +523 -0
- data/.rubocop.yml +64 -0
- data/.ruby-gemset +1 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +23 -0
- data/Gemfile.lock +19 -20
- data/README.md +31 -21
- data/{UsingTheRequestObject.md → docs/UsingTheRequestObject.md} +1 -1
- data/{UsingUserConfigurations.md → docs/UsingUserConfigurations.md} +1 -1
- data/easy-jsonapi.gemspec +3 -3
- data/lib/easy/jsonapi/config_manager/config.rb +2 -2
- data/lib/easy/jsonapi/exceptions.rb +6 -0
- data/lib/easy/jsonapi/exceptions/document_exceptions.rb +2 -1
- data/lib/easy/jsonapi/exceptions/headers_exceptions.rb +18 -18
- data/lib/easy/jsonapi/exceptions/json_parse_error.rb +10 -0
- data/lib/easy/jsonapi/exceptions/query_params_exceptions.rb +1 -1
- data/lib/easy/jsonapi/exceptions/user_defined_exceptions.rb +6 -2
- data/lib/easy/jsonapi/item.rb +2 -2
- data/lib/easy/jsonapi/middleware.rb +13 -13
- data/lib/easy/jsonapi/parser/document_parser.rb +2 -2
- data/lib/easy/jsonapi/parser/headers_parser.rb +1 -1
- data/lib/easy/jsonapi/parser/json_parser.rb +29 -0
- data/lib/easy/jsonapi/request/query_param_collection.rb +9 -3
- data/lib/easy/jsonapi/request/query_param_collection/include_param.rb +1 -1
- data/lib/easy/jsonapi/utility.rb +1 -1
- metadata +29 -21
- data/.github/workflows/rake.yml +0 -35
data/.rubocop.yml
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
TargetRubyVersion: '3.0.0'
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
Metrics/AbcSize:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Layout/TrailingWhitespace:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Layout/EmptyLinesAroundModuleBody:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Layout/EmptyLines:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Layout/EmptyLinesAroundClassBody:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/BlockLength:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Metrics/MethodLength:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/ParameterLists:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Bundler/OrderedGems:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Layout/EmptyLinesAroundBlockBody:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Layout/EmptyLineAfterGuardClause:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Style/WordArray:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Style/StringConcatenation:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Layout/EmptyLinesAroundMethodBody:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/StringLiterals:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/IfUnlessModifier:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Layout/LineLength:
|
55
|
+
Enabled: false
|
56
|
+
|
57
|
+
Gemspec/OrderedDependencies:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
Metrics/ModuleLength:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
Layout/ArgumentAlignment:
|
64
|
+
Enabled: false
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
easy-jsonapi
|
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,28 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.0.5 - 2020-03-30
|
4
|
+
|
5
|
+
- Fixed bug in JSONAPI::Exceptions::HeadersExceptions that didn't check for user required headers requirements
|
6
|
+
- Fixed bug in JSONAPI::Exceptions::QueryParamExceptions that didn't check for user required query param requirements
|
7
|
+
- Added more tests to the middleware
|
8
|
+
- Updated Documentation
|
9
|
+
|
10
|
+
## 1.0.4 - 2020-03-28
|
11
|
+
|
12
|
+
- Fixed JSONAPI::ExceptionsHeadersExceptions bug
|
13
|
+
- Updated README files
|
14
|
+
|
15
|
+
## 1.0.3 - 2020-03-25
|
16
|
+
|
17
|
+
- Updated JSONAPI::Exceptions::HeadersExceptions to allow wildcard matching for Accept header
|
18
|
+
|
19
|
+
## 1.0.2 - 2020-03-25
|
20
|
+
|
21
|
+
- Updated README and fix READE broken links
|
22
|
+
- Reorganization of README files into docs file.
|
23
|
+
- Make easy-jsonapi compatible with ruby versions >= 2.5
|
24
|
+
- Added wrapper around Oj usage so all raised errors are found in the JSONAPI::Exceptions module
|
25
|
+
|
3
26
|
## 1.0.0 - 2020-03-24
|
4
27
|
|
5
28
|
- This is the first release with a version of 1.0.0. All main features supported, but user configurations can be developed to provide greater adherence to the spec and more developer features.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
easy-jsonapi (1.0.
|
4
|
+
easy-jsonapi (1.0.5)
|
5
5
|
oj (~> 3.10)
|
6
6
|
|
7
7
|
GEM
|
@@ -10,23 +10,19 @@ GEM
|
|
10
10
|
ast (2.4.2)
|
11
11
|
backport (1.1.2)
|
12
12
|
benchmark (0.1.1)
|
13
|
+
codecov (0.5.1)
|
14
|
+
simplecov (>= 0.15, < 0.22)
|
13
15
|
diff-lcs (1.4.4)
|
16
|
+
docile (1.3.5)
|
14
17
|
e2mmap (0.1.0)
|
15
18
|
jaro_winkler (1.5.4)
|
16
19
|
kramdown (2.3.1)
|
17
20
|
rexml
|
18
21
|
kramdown-parser-gfm (1.1.0)
|
19
22
|
kramdown (~> 2.0)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
thor (~> 1.0.1)
|
24
|
-
tomlrb (>= 1.3, < 2.1)
|
25
|
-
with_env (= 1.1.0)
|
26
|
-
xml-simple (~> 1.1.5)
|
27
|
-
mini_portile2 (2.5.0)
|
28
|
-
nokogiri (1.11.2)
|
29
|
-
mini_portile2 (~> 2.5.0)
|
23
|
+
nokogiri (1.11.2-x86_64-darwin)
|
24
|
+
racc (~> 1.4)
|
25
|
+
nokogiri (1.11.2-x86_64-linux)
|
30
26
|
racc (~> 1.4)
|
31
27
|
oj (3.11.3)
|
32
28
|
parallel (1.20.1)
|
@@ -54,7 +50,7 @@ GEM
|
|
54
50
|
diff-lcs (>= 1.2.0, < 2.0)
|
55
51
|
rspec-support (~> 3.10.0)
|
56
52
|
rspec-support (3.10.2)
|
57
|
-
rubocop (1.
|
53
|
+
rubocop (1.12.0)
|
58
54
|
parallel (~> 1.10)
|
59
55
|
parser (>= 3.0.0.0)
|
60
56
|
rainbow (>= 2.2.2, < 4.0)
|
@@ -66,7 +62,12 @@ GEM
|
|
66
62
|
rubocop-ast (1.4.1)
|
67
63
|
parser (>= 2.7.1.5)
|
68
64
|
ruby-progressbar (1.11.0)
|
69
|
-
|
65
|
+
simplecov (0.21.2)
|
66
|
+
docile (~> 1.1)
|
67
|
+
simplecov-html (~> 0.11)
|
68
|
+
simplecov_json_formatter (~> 0.1)
|
69
|
+
simplecov-html (0.12.3)
|
70
|
+
simplecov_json_formatter (0.1.2)
|
70
71
|
solargraph (0.40.4)
|
71
72
|
backport (~> 1.1)
|
72
73
|
benchmark
|
@@ -81,20 +82,18 @@ GEM
|
|
81
82
|
thor (~> 1.0)
|
82
83
|
tilt (~> 2.0)
|
83
84
|
yard (~> 0.9, >= 0.9.24)
|
84
|
-
thor (1.0
|
85
|
+
thor (1.1.0)
|
85
86
|
tilt (2.0.10)
|
86
|
-
tomlrb (2.0.1)
|
87
87
|
unicode-display_width (2.0.0)
|
88
|
-
with_env (1.1.0)
|
89
|
-
xml-simple (1.1.8)
|
90
88
|
yard (0.9.26)
|
91
89
|
|
92
90
|
PLATFORMS
|
93
|
-
|
91
|
+
x86_64-darwin-19
|
92
|
+
x86_64-linux
|
94
93
|
|
95
94
|
DEPENDENCIES
|
95
|
+
codecov (~> 0.4)
|
96
96
|
easy-jsonapi!
|
97
|
-
license_finder (~> 6.10)
|
98
97
|
rack (~> 2.2)
|
99
98
|
rake (~> 13.0)
|
100
99
|
redcarpet (~> 3.5)
|
@@ -103,4 +102,4 @@ DEPENDENCIES
|
|
103
102
|
solargraph (~> 0.39)
|
104
103
|
|
105
104
|
BUNDLED WITH
|
106
|
-
2.2.
|
105
|
+
2.2.15
|
data/README.md
CHANGED
@@ -5,9 +5,13 @@
|
|
5
5
|
|
6
6
|
# easy-jsonapi
|
7
7
|
|
8
|
-
|
9
|
-
![](https://ruby-gem-downloads-badge.herokuapp.com/easy-jsonapi?type=total&color=brightgreen)
|
10
|
-
|
8
|
+
![Gem Version](https://badge.fury.io/rb/easy-jsonapi.svg)
|
9
|
+
![Downloads](https://ruby-gem-downloads-badge.herokuapp.com/easy-jsonapi?type=total&color=brightgreen)
|
10
|
+
![Build Status](https://img.shields.io/travis/Curatess/easy-jsonapi/production)
|
11
|
+
![Code Coverage](https://img.shields.io/codecov/c/github/curatess/easy-jsonapi)
|
12
|
+
![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)
|
13
|
+
![License](https://img.shields.io/github/license/Curatess/easy-jsonapi)
|
14
|
+
|
11
15
|
|
12
16
|
The gem that makes using [JSON:API](https://jsonapi.org/) ***EASY***!
|
13
17
|
|
@@ -17,19 +21,19 @@ Ever wanted the benefits of [JSONAPI](https://jsonapi.org/) without the learning
|
|
17
21
|
2. A `parser` to interact with requests in a typical Object-Oriented Fashion, providing convenient and efficient access to headers, query parameters, and document members.
|
18
22
|
3. A `validator` to check your serialized responses for [JSONAPI](https://jsonapi.org/) compliance.
|
19
23
|
|
20
|
-
With its only gem dependency being [Oj](https://github.com/ohler55/oj), ***easy-jsonapi*** is a lightweight, dependable tool, featuring comprehensive error messages and over
|
24
|
+
With its only gem dependency being [Oj](https://github.com/ohler55/oj), ***easy-jsonapi*** is a lightweight, dependable tool, featuring comprehensive error messages and over ***525 unit tests*** allowing developers to spend less time debugging and more time creating.
|
21
25
|
|
22
|
-
As a bonus, flexible user configurations can be added to the middleware providing custom screening on all requests or individual requests depending on the resource type of the endpoint and the
|
26
|
+
As a bonus, flexible user configurations can be added to the middleware providing custom screening on all requests or individual requests depending on the resource type of the endpoint and the user-defined document, header, or query param restrictions.
|
23
27
|
|
24
28
|
## Links
|
25
29
|
|
26
|
-
- [*Documentation*](
|
30
|
+
- [*Documentation*](https://rubydoc.info/github/Curatess/easy-jsonapi/production)
|
27
31
|
|
28
|
-
- [*RubyGems* *
|
32
|
+
- [*RubyGems* *Repository*](https://rubygems.org/search?query=easy-jsonapi)
|
29
33
|
|
30
34
|
## Installation
|
31
35
|
|
32
|
-
Add this
|
36
|
+
Add this line to your applications' Gemfile:
|
33
37
|
|
34
38
|
```bash
|
35
39
|
# Gemfile
|
@@ -52,13 +56,15 @@ $ gem install easy-jsonapi
|
|
52
56
|
|
53
57
|
## Quick Start
|
54
58
|
|
55
|
-
|
59
|
+
***easy-jsonapi's*** 3 main use cases can be used together or separately, so to get started quickly, do any of the 3 below to begin enjoying the convenience of the library.
|
60
|
+
|
61
|
+
- Set up the middleware
|
56
62
|
|
57
63
|
```ruby
|
58
64
|
use JSONAPI::Middleware
|
59
65
|
```
|
60
66
|
|
61
|
-
|
67
|
+
- Parse the rack environment variable and get access to request components (assumes valid JSON:API requests -- use with middleware to ensure valid requests)
|
62
68
|
|
63
69
|
```ruby
|
64
70
|
j_req = JSONAPI::Parser.parse_request(env)
|
@@ -68,7 +74,7 @@ $ gem install easy-jsonapi
|
|
68
74
|
j_req.body.data.type # => "person"
|
69
75
|
```
|
70
76
|
|
71
|
-
|
77
|
+
- Validate your serialized JSON:API before returning it to your clients.
|
72
78
|
|
73
79
|
```ruby
|
74
80
|
begin
|
@@ -84,7 +90,7 @@ $ gem install easy-jsonapi
|
|
84
90
|
|
85
91
|
Add the middleware to the stack in order to activate it.
|
86
92
|
|
87
|
-
|
93
|
+
Sinatra:
|
88
94
|
|
89
95
|
```ruby
|
90
96
|
# app.rb
|
@@ -118,15 +124,15 @@ use JSONAPI::Middleware
|
|
118
124
|
|
119
125
|
### Functionality
|
120
126
|
|
121
|
-
The easy-jsonapi middleware can
|
127
|
+
The easy-jsonapi middleware can operate in development or production mode.
|
122
128
|
|
123
|
-
If `ENV['RACK_ENV']` is set to `:development` or not set at all, the middleware will be
|
129
|
+
If `ENV['RACK_ENV']` is set to `:development` or not set at all, the middleware will be operating in development mode.
|
124
130
|
|
125
131
|
When the middleware is in development mode it will raise an exception wherever it finds the http request to be non JSONAPI compliant.
|
126
132
|
|
127
133
|
The types of exceptions it will raise are:
|
128
134
|
|
129
|
-
- `
|
135
|
+
- `JSONAPI::Exceptions::JSONParseError` when an included body is not valid JSON
|
130
136
|
- `JSONAPI::Exceptions::HeaderExceptions::InvalidHeader` when an included header is non-compliant
|
131
137
|
- `JSONAPI::Exceptions::QueryParamExceptions::InvalidQueryParam` when an included query parameter is non-compliant
|
132
138
|
- `JSONAPI::Exceptions::DocumentExceptions::InvalidDocument` when the body is included and non-compliant
|
@@ -135,7 +141,7 @@ If `ENV['RACK_ENV']` is set to something other than `:development`, then the mi
|
|
135
141
|
|
136
142
|
### User Configurations
|
137
143
|
|
138
|
-
***easy-jsonapi*** has a fair amount of flexibility when it comes to user configurations
|
144
|
+
***easy-jsonapi*** has a fair amount of flexibility when it comes to user configurations but also plenty of room for to add more features. To see the currently available configurations see [UsingUserConfigurations](https://github.com/Curatess/easy-jsonapi/blob/production/docs/UsingUserConfigurations.md) and to propose a new feature create a pull request or ticket on the [dev repository](https://github.com/Curatess/easy-jsonapi/tree/dev).
|
139
145
|
|
140
146
|
## Using the Request Parser
|
141
147
|
|
@@ -167,7 +173,7 @@ require 'easy/jsonapi'
|
|
167
173
|
jsonapi_req = JSONAPI::Parser.parse_request(env)
|
168
174
|
```
|
169
175
|
|
170
|
-
This returns a `JSONAPI::Request` object that can be used to access the collection of query params, collection of headers, and the body of the request. To see example usage, see [Using the Request Object](https://github.com/Curatess/easy-jsonapi/UsingTheRequestObject.md).
|
176
|
+
This returns a `JSONAPI::Request` object that can be used to access the collection of query params, collection of headers, and the body of the request. To see example usage, see [Using the Request Object](https://github.com/Curatess/easy-jsonapi/blob/production/docs/UsingTheRequestObject.md).
|
171
177
|
|
172
178
|
## Using the Serialized Response Validator
|
173
179
|
|
@@ -190,11 +196,15 @@ end
|
|
190
196
|
The `headers` param is a hash of `String => String` or `Symbol => String` of the header keys and values.
|
191
197
|
The `body` param is either the JSON body or a ruby hash representation of the body.
|
192
198
|
|
193
|
-
See the [rubydocs](
|
199
|
+
See the [rubydocs](https://rubydoc.info/github/Curatess/easy-jsonapi/proudction) for more on the Serialized Response Validator.
|
200
|
+
|
201
|
+
## Acknowledgements
|
202
|
+
|
203
|
+
The exception checking strategy for `JSONAPI::Exceptions::DocumentExceptions` and some initial compliance checks are based off [jsonapi-parser](https://github.com/jsonapi-rb/jsonapi-parser). We would like to thank the [jsonapi-rb](https://github.com/jsonapi-rb) team for the document validation work and to all our contributors and users for the continuous support!
|
194
204
|
|
195
205
|
## Releases
|
196
206
|
|
197
|
-
See [{file:CHANGELOG.md}](https://github.com/Curatess/easy-jsonapi/CHANGELOG.md)
|
207
|
+
See [{file:CHANGELOG.md}](https://github.com/Curatess/easy-jsonapi/blob/production/CHANGELOG.md).
|
198
208
|
|
199
209
|
## License
|
200
210
|
|
@@ -202,8 +212,8 @@ See [{file:CHANGELOG.md}](https://github.com/Curatess/easy-jsonapi/CHANGELOG.md)
|
|
202
212
|
|
203
213
|
## Contributing
|
204
214
|
|
205
|
-
Bug reports and pull requests are welcome on
|
215
|
+
Bug reports and pull requests are welcome [here on Github](https://github.com/Curatess/easy-jsonapi). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Code of Conduct](https://github.com/Curatess/easy-jsonapi/blob/production/CODE_OF_CONDUCT.md).
|
206
216
|
|
207
217
|
## Code of Conduct
|
208
218
|
|
209
|
-
Everyone interacting in the easy-jsonapi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [
|
219
|
+
Everyone interacting in the easy-jsonapi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [Code of Conduct](https://github.com/Curatess/easy-jsonapi/blob/production/CODE_OF_CONDUCT.md).
|
@@ -69,6 +69,6 @@ j_req.body.jsonapi # The JSONAPI jsonapi member
|
|
69
69
|
j_req.body.to_s # serialized JSONAPI
|
70
70
|
j_req.body.to_h # ruby hash representation of JSONAPI
|
71
71
|
|
72
|
-
# NOTE: j_req.body.data returns a
|
72
|
+
# NOTE: j_req.body.data returns a resource or an array of resources depending on the request
|
73
73
|
j_req.body.data # JSONAPI::Document::Resource or [JSONAPI::Document::Resource]
|
74
74
|
```
|
@@ -17,7 +17,7 @@ use JSONAPI::Middleware do |config_manager|
|
|
17
17
|
end
|
18
18
|
```
|
19
19
|
|
20
|
-
To add restrictions to ALL requests use default global config included with the Config Manager:
|
20
|
+
To add restrictions to ALL requests use the default global config included with the Config Manager:
|
21
21
|
|
22
22
|
```ruby
|
23
23
|
use JSONAPI::Middleware do |config_manager|
|
data/easy-jsonapi.gemspec
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = 'easy-jsonapi'
|
5
|
-
spec.version = '1.0.
|
5
|
+
spec.version = '1.0.5'
|
6
6
|
spec.authors = ['Joshua DeMoss, Joe Viscomi']
|
7
7
|
spec.email = ['demoss.joshua@gmail.com']
|
8
8
|
|
9
9
|
spec.summary = 'Middleware, Parser, and Validator for JSONAPI requests and serialized resopnses'
|
10
10
|
spec.description = 'Middleware to screen non-JSONAPI-compliant requests, a parser to provide Object Oriented access to requests, and a validator for validating JSONAPI Serialized responses.'
|
11
11
|
spec.homepage = 'https://rubygems.org/gems/easy-jsonapi'
|
12
|
-
spec.required_ruby_version = '>= 2.
|
12
|
+
spec.required_ruby_version = '>= 2.5'
|
13
13
|
|
14
14
|
spec.metadata["source_code_uri"] = "https://github.com/Curatess/easy-jsonapi"
|
15
15
|
spec.metadata["changelog_uri"] = "https://github.com/Curatess/easy-jsonapi/CHANGELOG.mg"
|
@@ -24,13 +24,13 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.require_paths = ['lib']
|
25
25
|
|
26
26
|
# Dev Dependencies
|
27
|
-
spec.add_development_dependency 'license_finder', '~> 6.10'
|
28
27
|
spec.add_development_dependency 'rack', '~> 2.2'
|
29
28
|
spec.add_development_dependency 'rake', '~> 13.0'
|
30
29
|
spec.add_development_dependency 'redcarpet', '~> 3.5'
|
31
30
|
spec.add_development_dependency 'rspec', '~> 3.9'
|
32
31
|
spec.add_development_dependency 'rubocop', '~> 1.11'
|
33
32
|
spec.add_development_dependency 'solargraph', '~> 0.39'
|
33
|
+
spec.add_development_dependency 'codecov', '~> 0.4'
|
34
34
|
|
35
35
|
# Dependencies
|
36
36
|
spec.add_dependency 'oj', '~> 3.10'
|
@@ -35,8 +35,8 @@ module JSONAPI
|
|
35
35
|
# dynamically creates accessor methods for instance variables
|
36
36
|
# created in the initialize
|
37
37
|
def method_missing(method_name, *args, &block)
|
38
|
-
super unless READER_METHODS.include?(method_name.to_s[..-2].to_sym)
|
39
|
-
instance_variable_set("@#{method_name}"[..-2].to_sym, args[0])
|
38
|
+
super unless READER_METHODS.include?(method_name.to_s[0..-2].to_sym)
|
39
|
+
instance_variable_set("@#{method_name}"[0..-2].to_sym, args[0])
|
40
40
|
@default = false
|
41
41
|
end
|
42
42
|
|
@@ -4,6 +4,8 @@ require 'easy/jsonapi/exceptions/document_exceptions'
|
|
4
4
|
require 'easy/jsonapi/exceptions/headers_exceptions'
|
5
5
|
require 'easy/jsonapi/exceptions/naming_exceptions'
|
6
6
|
require 'easy/jsonapi/exceptions/query_params_exceptions'
|
7
|
+
require 'easy/jsonapi/exceptions/user_defined_exceptions'
|
8
|
+
require 'easy/jsonapi/exceptions/json_parse_error'
|
7
9
|
|
8
10
|
module JSONAPI
|
9
11
|
# Namespace for the gem's Exceptions
|
@@ -23,5 +25,9 @@ module JSONAPI
|
|
23
25
|
# Checking for JSONAPI naming rules compliance
|
24
26
|
module NamingExceptions
|
25
27
|
end
|
28
|
+
|
29
|
+
# Checking for User Defined Exceptions
|
30
|
+
module UserDefinedExceptions
|
31
|
+
end
|
26
32
|
end
|
27
33
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'easy/jsonapi/parser/json_parser'
|
3
4
|
require 'easy/jsonapi/exceptions/naming_exceptions'
|
4
5
|
require 'easy/jsonapi/exceptions/user_defined_exceptions'
|
5
6
|
|
@@ -57,7 +58,7 @@ module JSONAPI
|
|
57
58
|
# @param opts [Hash] Includes path, http_method, sparse_fieldsets
|
58
59
|
# @raise InvalidDocument if any part of the spec is not observed
|
59
60
|
def self.check_compliance(document, config_manager = nil, opts = {})
|
60
|
-
document =
|
61
|
+
document = JSONAPI::Parser::JSONParser.parse(document) if document.is_a? String
|
61
62
|
ensure!(!document.nil?, 'A document cannot be nil')
|
62
63
|
|
63
64
|
check_essentials(document, opts[:http_method])
|
@@ -25,9 +25,9 @@ module JSONAPI
|
|
25
25
|
|
26
26
|
# Check http verb vs included headers
|
27
27
|
# @param env [Hash] The rack environment variable
|
28
|
-
def self.check_request(env, config_manager = nil, opts = {})
|
28
|
+
def self.check_request(env, body, config_manager = nil, opts = {})
|
29
29
|
check_compliance(env, config_manager, opts)
|
30
|
-
check_http_method_against_headers(env)
|
30
|
+
check_http_method_against_headers(env, body)
|
31
31
|
end
|
32
32
|
|
33
33
|
# Check jsonapi compliance
|
@@ -39,7 +39,7 @@ module JSONAPI
|
|
39
39
|
hdrs = JSONAPI::Parser::HeadersParser.parse(env)
|
40
40
|
usr_opts = { http_method: opts[:http_method], path: opts[:path] }
|
41
41
|
err_msg = JSONAPI::Exceptions::UserDefinedExceptions.check_user_header_requirements(hdrs, config_manager, usr_opts)
|
42
|
-
|
42
|
+
raise err_msg unless err_msg.nil?
|
43
43
|
end
|
44
44
|
|
45
45
|
class << self
|
@@ -78,30 +78,30 @@ module JSONAPI
|
|
78
78
|
# error if the combination doesn't make sense
|
79
79
|
# @param (see #compliant?)
|
80
80
|
# @raise InvalidHeader the invalid header incombination with the http verb
|
81
|
-
def check_http_method_against_headers(env)
|
81
|
+
def check_http_method_against_headers(env, body)
|
82
82
|
case env['REQUEST_METHOD']
|
83
83
|
when 'GET'
|
84
|
-
check_get_against_hdrs(env)
|
84
|
+
check_get_against_hdrs(env, body)
|
85
85
|
when 'POST' || 'PATCH' || 'PUT'
|
86
|
-
check_post_against_hdrs(env)
|
86
|
+
check_post_against_hdrs(env, body)
|
87
87
|
when 'DELETE'
|
88
|
-
check_delete_against_hdrs(env)
|
88
|
+
check_delete_against_hdrs(env, body)
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
92
|
# Raise error if a GET request has a body or a content type header
|
93
93
|
# @param (see #compliant?)
|
94
|
-
def check_get_against_hdrs(env)
|
95
|
-
raise_error('GET requests cannot have a body.') unless
|
94
|
+
def check_get_against_hdrs(env, body)
|
95
|
+
raise_error('GET requests cannot have a body.') unless body == ""
|
96
96
|
raise_error("GET request cannot have a 'CONTENT_TYPE' http header.") unless env['CONTENT_TYPE'].nil?
|
97
97
|
end
|
98
98
|
|
99
99
|
# POST, PUT, and PATCH request must have a content type header,
|
100
100
|
# a body, and a content-type and accept header that accepts jsonapi
|
101
101
|
# @param (see #compliant?)
|
102
|
-
def check_post_against_hdrs(env)
|
102
|
+
def check_post_against_hdrs(env, body)
|
103
103
|
raise_error("POST, PUT, and PATCH requests must have a 'CONTENT_TYPE' header.") unless env['CONTENT_TYPE']
|
104
|
-
raise_error('POST, PUT, and PATCH requests must have a body.')
|
104
|
+
raise_error('POST, PUT, and PATCH requests must have a body.') if body == ""
|
105
105
|
|
106
106
|
return if env['CONTENT_TYPE'] == 'application/vnd.api+json' && accepts_jsonapi?(env)
|
107
107
|
|
@@ -109,6 +109,12 @@ module JSONAPI
|
|
109
109
|
"JSON:API media type, if they include a JSON:API 'CONTENT_TYPE' header")
|
110
110
|
end
|
111
111
|
|
112
|
+
# Raise error if DELETE hdr has a body or a content type header
|
113
|
+
def check_delete_against_hdrs(env, body)
|
114
|
+
raise_error('DELETE requests cannot have a body.') unless body == ""
|
115
|
+
raise_error("DELETE request cannot have a 'CONTENT_TYPE' http header.") unless env['CONTENT_TYPE'].nil?
|
116
|
+
end
|
117
|
+
|
112
118
|
# Check the accept header to see if any of the provided media types indicate that
|
113
119
|
# jsonapi is accepted
|
114
120
|
# @param (see #compliant?)
|
@@ -121,16 +127,10 @@ module JSONAPI
|
|
121
127
|
false
|
122
128
|
end
|
123
129
|
|
124
|
-
# Raise error if DELETE hdr has a body or a content type header
|
125
|
-
def check_delete_against_hdrs(env)
|
126
|
-
raise_error('DELETE requests cannot have a body.') unless env['rack.input'].nil?
|
127
|
-
raise_error("DELETE request cannot have a 'CONTENT_TYPE' http header.") unless env['CONTENT_TYPE'].nil?
|
128
|
-
end
|
129
|
-
|
130
130
|
# @param accept_hdr [String] The value of the http accept header
|
131
131
|
def contains_at_least_one_jsonapi_media_type_without_params?(accept_hdr)
|
132
132
|
accept_hdr.split(',').each do |mt|
|
133
|
-
if mt
|
133
|
+
if JSONAPI_MEDIA_TYPES.include? mt
|
134
134
|
return true
|
135
135
|
end
|
136
136
|
end
|