easy-jsonapi 1.0.1 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/codecov.yml +30 -0
- data/.github/workflows/publish.yml +57 -0
- data/.rspec_status +486 -483
- data/.rubocop.yml +2 -2
- data/.travis.yml +8 -0
- data/CHANGELOG.md +22 -1
- data/Gemfile.lock +8 -7
- data/README.md +20 -16
- data/docs/UsingTheRequestObject.md +1 -1
- data/docs/UsingUserConfigurations.md +1 -1
- data/easy-jsonapi.gemspec +1 -1
- 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 +19 -17
- 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/middleware.rb +25 -27
- data/lib/easy/jsonapi/parser/document_parser.rb +2 -2
- data/lib/easy/jsonapi/parser/json_parser.rb +29 -0
- data/lib/easy/jsonapi/request/query_param_collection.rb +9 -3
- metadata +7 -3
- data/.github/workflows/publish-gem.yml +0 -74
data/.rubocop.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
AllCops:
|
2
2
|
NewCops: enable
|
3
|
-
TargetRubyVersion: '
|
3
|
+
TargetRubyVersion: '3.0.0'
|
4
4
|
SuggestExtensions: false
|
5
5
|
|
6
6
|
Metrics/AbcSize:
|
@@ -61,4 +61,4 @@ Metrics/ModuleLength:
|
|
61
61
|
Enabled: false
|
62
62
|
|
63
63
|
Layout/ArgumentAlignment:
|
64
|
-
Enabled: false
|
64
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,31 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 1.0.
|
3
|
+
## 1.0.6 - 2020-03-20
|
4
|
+
|
5
|
+
- Fixed bug in JSONAPI::Middleware that was not checking for environment variables properly
|
6
|
+
|
7
|
+
## 1.0.5 - 2020-03-20
|
8
|
+
|
9
|
+
- Fixed bug in JSONAPI::Exceptions::HeadersExceptions that didn't check for user required headers requirements
|
10
|
+
- Fixed bug in JSONAPI::Exceptions::QueryParamExceptions that didn't check for user required query param requirements
|
11
|
+
- Added more tests to the middleware
|
12
|
+
- Updated Documentation
|
13
|
+
|
14
|
+
## 1.0.4 - 2020-03-28
|
15
|
+
|
16
|
+
- Fixed JSONAPI::ExceptionsHeadersExceptions bug
|
17
|
+
- Updated README files
|
18
|
+
|
19
|
+
## 1.0.3 - 2020-03-25
|
20
|
+
|
21
|
+
- Updated JSONAPI::Exceptions::HeadersExceptions to allow wildcard matching for Accept header
|
22
|
+
|
23
|
+
## 1.0.2 - 2020-03-25
|
4
24
|
|
5
25
|
- Updated README and fix READE broken links
|
6
26
|
- Reorganization of README files into docs file.
|
7
27
|
- Make easy-jsonapi compatible with ruby versions >= 2.5
|
28
|
+
- Added wrapper around Oj usage so all raised errors are found in the JSONAPI::Exceptions module
|
8
29
|
|
9
30
|
## 1.0.0 - 2020-03-24
|
10
31
|
|
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.6)
|
5
5
|
oj (~> 3.10)
|
6
6
|
|
7
7
|
GEM
|
@@ -20,9 +20,9 @@ GEM
|
|
20
20
|
rexml
|
21
21
|
kramdown-parser-gfm (1.1.0)
|
22
22
|
kramdown (~> 2.0)
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
nokogiri (1.11.2-x86_64-darwin)
|
24
|
+
racc (~> 1.4)
|
25
|
+
nokogiri (1.11.2-x86_64-linux)
|
26
26
|
racc (~> 1.4)
|
27
27
|
oj (3.11.3)
|
28
28
|
parallel (1.20.1)
|
@@ -50,7 +50,7 @@ GEM
|
|
50
50
|
diff-lcs (>= 1.2.0, < 2.0)
|
51
51
|
rspec-support (~> 3.10.0)
|
52
52
|
rspec-support (3.10.2)
|
53
|
-
rubocop (1.
|
53
|
+
rubocop (1.12.0)
|
54
54
|
parallel (~> 1.10)
|
55
55
|
parser (>= 3.0.0.0)
|
56
56
|
rainbow (>= 2.2.2, < 4.0)
|
@@ -82,13 +82,14 @@ GEM
|
|
82
82
|
thor (~> 1.0)
|
83
83
|
tilt (~> 2.0)
|
84
84
|
yard (~> 0.9, >= 0.9.24)
|
85
|
-
thor (1.0
|
85
|
+
thor (1.1.0)
|
86
86
|
tilt (2.0.10)
|
87
87
|
unicode-display_width (2.0.0)
|
88
88
|
yard (0.9.26)
|
89
89
|
|
90
90
|
PLATFORMS
|
91
|
-
|
91
|
+
x86_64-darwin-19
|
92
|
+
x86_64-linux
|
92
93
|
|
93
94
|
DEPENDENCIES
|
94
95
|
codecov (~> 0.4)
|
data/README.md
CHANGED
@@ -5,11 +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
|
-
|
11
|
-
|
12
|
-
|
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
|
+
|
13
15
|
|
14
16
|
The gem that makes using [JSON:API](https://jsonapi.org/) ***EASY***!
|
15
17
|
|
@@ -19,9 +21,9 @@ Ever wanted the benefits of [JSONAPI](https://jsonapi.org/) without the learning
|
|
19
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.
|
20
22
|
3. A `validator` to check your serialized responses for [JSONAPI](https://jsonapi.org/) compliance.
|
21
23
|
|
22
|
-
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.
|
23
25
|
|
24
|
-
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.
|
25
27
|
|
26
28
|
## Links
|
27
29
|
|
@@ -54,13 +56,15 @@ $ gem install easy-jsonapi
|
|
54
56
|
|
55
57
|
## Quick Start
|
56
58
|
|
57
|
-
|
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
|
58
62
|
|
59
63
|
```ruby
|
60
64
|
use JSONAPI::Middleware
|
61
65
|
```
|
62
66
|
|
63
|
-
|
67
|
+
- Parse the rack environment variable and get access to request components (assumes valid JSON:API requests -- use with middleware to ensure valid requests)
|
64
68
|
|
65
69
|
```ruby
|
66
70
|
j_req = JSONAPI::Parser.parse_request(env)
|
@@ -70,7 +74,7 @@ $ gem install easy-jsonapi
|
|
70
74
|
j_req.body.data.type # => "person"
|
71
75
|
```
|
72
76
|
|
73
|
-
|
77
|
+
- Validate your serialized JSON:API before returning it to your clients.
|
74
78
|
|
75
79
|
```ruby
|
76
80
|
begin
|
@@ -120,24 +124,24 @@ use JSONAPI::Middleware
|
|
120
124
|
|
121
125
|
### Functionality
|
122
126
|
|
123
|
-
The easy-jsonapi middleware can
|
127
|
+
The easy-jsonapi middleware can operate in development or production mode.
|
124
128
|
|
125
|
-
If `ENV['RACK_ENV']` is set to
|
129
|
+
If `ENV['RACK_ENV']` is set to `'development'` or not set at all, the middleware will be operating in development mode.
|
126
130
|
|
127
131
|
When the middleware is in development mode it will raise an exception wherever it finds the http request to be non JSONAPI compliant.
|
128
132
|
|
129
133
|
The types of exceptions it will raise are:
|
130
134
|
|
131
|
-
- `
|
135
|
+
- `JSONAPI::Exceptions::JSONParseError` when an included body is not valid JSON
|
132
136
|
- `JSONAPI::Exceptions::HeaderExceptions::InvalidHeader` when an included header is non-compliant
|
133
137
|
- `JSONAPI::Exceptions::QueryParamExceptions::InvalidQueryParam` when an included query parameter is non-compliant
|
134
138
|
- `JSONAPI::Exceptions::DocumentExceptions::InvalidDocument` when the body is included and non-compliant
|
135
139
|
|
136
|
-
If `ENV['RACK_ENV']` is set to something other than
|
140
|
+
If `ENV['RACK_ENV']` is set to something other than `'development'`, then the middleware will return the appropriate status code error given the JSON:API clause the headers, query params, or document violates.
|
137
141
|
|
138
142
|
### User Configurations
|
139
143
|
|
140
|
-
***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).
|
141
145
|
|
142
146
|
## Using the Request Parser
|
143
147
|
|
@@ -196,7 +200,7 @@ See the [rubydocs](https://rubydoc.info/github/Curatess/easy-jsonapi/proudction)
|
|
196
200
|
|
197
201
|
## Acknowledgements
|
198
202
|
|
199
|
-
The exception checking strategy for `JSONAPI::Exceptions::DocumentExceptions` and some
|
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!
|
200
204
|
|
201
205
|
## Releases
|
202
206
|
|
@@ -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
@@ -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,11 @@ module JSONAPI
|
|
25
25
|
|
26
26
|
# Check http verb vs included headers
|
27
27
|
# @param env [Hash] The rack environment variable
|
28
|
+
# @param config_manager [JSONAPI::ConfigManager] The manager of user configurations
|
29
|
+
# @param opts [Hash] Includes http_method, path, and contains_body values
|
28
30
|
def self.check_request(env, config_manager = nil, opts = {})
|
29
31
|
check_compliance(env, config_manager, opts)
|
30
|
-
check_http_method_against_headers(env)
|
32
|
+
check_http_method_against_headers(env, opts[:contains_body])
|
31
33
|
end
|
32
34
|
|
33
35
|
# Check jsonapi compliance
|
@@ -39,7 +41,7 @@ module JSONAPI
|
|
39
41
|
hdrs = JSONAPI::Parser::HeadersParser.parse(env)
|
40
42
|
usr_opts = { http_method: opts[:http_method], path: opts[:path] }
|
41
43
|
err_msg = JSONAPI::Exceptions::UserDefinedExceptions.check_user_header_requirements(hdrs, config_manager, usr_opts)
|
42
|
-
|
44
|
+
raise err_msg unless err_msg.nil?
|
43
45
|
end
|
44
46
|
|
45
47
|
class << self
|
@@ -78,30 +80,30 @@ module JSONAPI
|
|
78
80
|
# error if the combination doesn't make sense
|
79
81
|
# @param (see #compliant?)
|
80
82
|
# @raise InvalidHeader the invalid header incombination with the http verb
|
81
|
-
def check_http_method_against_headers(env)
|
83
|
+
def check_http_method_against_headers(env, contains_body)
|
82
84
|
case env['REQUEST_METHOD']
|
83
85
|
when 'GET'
|
84
|
-
check_get_against_hdrs(env)
|
86
|
+
check_get_against_hdrs(env, contains_body)
|
85
87
|
when 'POST' || 'PATCH' || 'PUT'
|
86
|
-
check_post_against_hdrs(env)
|
88
|
+
check_post_against_hdrs(env, contains_body)
|
87
89
|
when 'DELETE'
|
88
|
-
check_delete_against_hdrs(env)
|
90
|
+
check_delete_against_hdrs(env, contains_body)
|
89
91
|
end
|
90
92
|
end
|
91
93
|
|
92
94
|
# Raise error if a GET request has a body or a content type header
|
93
95
|
# @param (see #compliant?)
|
94
|
-
def check_get_against_hdrs(env)
|
95
|
-
raise_error('GET requests cannot have a body.')
|
96
|
+
def check_get_against_hdrs(env, contains_body)
|
97
|
+
raise_error('GET requests cannot have a body.') if contains_body
|
96
98
|
raise_error("GET request cannot have a 'CONTENT_TYPE' http header.") unless env['CONTENT_TYPE'].nil?
|
97
99
|
end
|
98
100
|
|
99
101
|
# POST, PUT, and PATCH request must have a content type header,
|
100
102
|
# a body, and a content-type and accept header that accepts jsonapi
|
101
103
|
# @param (see #compliant?)
|
102
|
-
def check_post_against_hdrs(env)
|
104
|
+
def check_post_against_hdrs(env, contains_body)
|
103
105
|
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.') unless
|
106
|
+
raise_error('POST, PUT, and PATCH requests must have a body.') unless contains_body
|
105
107
|
|
106
108
|
return if env['CONTENT_TYPE'] == 'application/vnd.api+json' && accepts_jsonapi?(env)
|
107
109
|
|
@@ -109,6 +111,12 @@ module JSONAPI
|
|
109
111
|
"JSON:API media type, if they include a JSON:API 'CONTENT_TYPE' header")
|
110
112
|
end
|
111
113
|
|
114
|
+
# Raise error if DELETE hdr has a body or a content type header
|
115
|
+
def check_delete_against_hdrs(env, contains_body)
|
116
|
+
raise_error('DELETE requests cannot have a body.') if contains_body
|
117
|
+
raise_error("DELETE request cannot have a 'CONTENT_TYPE' http header.") unless env['CONTENT_TYPE'].nil?
|
118
|
+
end
|
119
|
+
|
112
120
|
# Check the accept header to see if any of the provided media types indicate that
|
113
121
|
# jsonapi is accepted
|
114
122
|
# @param (see #compliant?)
|
@@ -121,16 +129,10 @@ module JSONAPI
|
|
121
129
|
false
|
122
130
|
end
|
123
131
|
|
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
132
|
# @param accept_hdr [String] The value of the http accept header
|
131
133
|
def contains_at_least_one_jsonapi_media_type_without_params?(accept_hdr)
|
132
134
|
accept_hdr.split(',').each do |mt|
|
133
|
-
if mt
|
135
|
+
if JSONAPI_MEDIA_TYPES.include? mt
|
134
136
|
return true
|
135
137
|
end
|
136
138
|
end
|
@@ -62,8 +62,10 @@ module JSONAPI
|
|
62
62
|
# with underscores instead of dashes.
|
63
63
|
# @param config (see #check_user_document_requirements)
|
64
64
|
def check_user_header_requirements(headers, config_manager, opts)
|
65
|
-
return if config_manager.nil?
|
65
|
+
return if config_manager.nil?
|
66
|
+
|
66
67
|
config = get_config(config_manager, opts[:http_method], opts[:path])
|
68
|
+
return if config.default? && config_manager.size.positive?
|
67
69
|
|
68
70
|
err =
|
69
71
|
check_for_required_headers(headers, config.required_headers)
|
@@ -77,8 +79,10 @@ module JSONAPI
|
|
77
79
|
# @param rack_req_params [Hash] The hash of the query parameters given by Rack::Request
|
78
80
|
# @param config (see #check_user_document_requirements)
|
79
81
|
def check_user_query_param_requirements(rack_req_params, config_manager, opts)
|
80
|
-
return if config_manager.nil?
|
82
|
+
return if config_manager.nil?
|
83
|
+
|
81
84
|
config = get_config(config_manager, opts[:http_method], opts[:path])
|
85
|
+
return if config.default? && config_manager.size.positive?
|
82
86
|
|
83
87
|
err =
|
84
88
|
check_for_required_params(rack_req_params, config.required_query_params)
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'easy/jsonapi/exceptions'
|
4
4
|
require 'easy/jsonapi/config_manager'
|
5
|
-
require '
|
5
|
+
require 'easy/jsonapi/parser/json_parser'
|
6
6
|
|
7
7
|
module JSONAPI
|
8
8
|
|
@@ -23,8 +23,8 @@ module JSONAPI
|
|
23
23
|
# and error if any section is found to be non-compliant.
|
24
24
|
# @param env The rack envirornment hash
|
25
25
|
def call(env)
|
26
|
-
if in_maintenance_mode?
|
27
|
-
return maintenance_response
|
26
|
+
if in_maintenance_mode?
|
27
|
+
return maintenance_response
|
28
28
|
end
|
29
29
|
|
30
30
|
if jsonapi_request?(env)
|
@@ -38,17 +38,15 @@ module JSONAPI
|
|
38
38
|
private
|
39
39
|
|
40
40
|
# Checks the 'MAINTENANCE' environment variable
|
41
|
-
# @param (see #call)
|
42
41
|
# @return [TrueClass | FalseClass]
|
43
|
-
def in_maintenance_mode?
|
44
|
-
!
|
42
|
+
def in_maintenance_mode?
|
43
|
+
!ENV['MAINTENANCE'].nil?
|
45
44
|
end
|
46
45
|
|
47
46
|
# Return 503 with or without msg depending on environment
|
48
|
-
# @param (see #call)
|
49
47
|
# @return [Array] Http Error Responses
|
50
|
-
def maintenance_response
|
51
|
-
if environment_development?
|
48
|
+
def maintenance_response
|
49
|
+
if environment_development?
|
52
50
|
[503, {}, ['MAINTENANCE envirornment variable set']]
|
53
51
|
else
|
54
52
|
[503, {}, []]
|
@@ -88,18 +86,21 @@ module JSONAPI
|
|
88
86
|
# @param config_manager [JSONAPI::ConfigManager::Config] The config object to use modify compliance checking
|
89
87
|
# @return [NilClass | Array] Nil meaning no error or a 400 level http response
|
90
88
|
def check_compliance(env, config_manager)
|
91
|
-
|
89
|
+
# Store separately so you can rewind for next middleware or app
|
90
|
+
body = env['rack.input'].read
|
91
|
+
env['rack.input'].rewind
|
92
|
+
opts = { http_method: env['REQUEST_METHOD'], path: env['PATH_INFO'], contains_body: body != "" }
|
92
93
|
|
93
94
|
header_error = check_headers_compliance(env, config_manager, opts)
|
94
95
|
return header_error unless header_error.nil?
|
95
96
|
|
96
97
|
req = Rack::Request.new(env)
|
97
|
-
param_error = check_query_param_compliance(
|
98
|
+
param_error = check_query_param_compliance(req.GET, config_manager, opts)
|
98
99
|
return param_error unless param_error.nil?
|
99
100
|
|
100
101
|
return unless env['CONTENT_TYPE']
|
101
102
|
|
102
|
-
body_error =
|
103
|
+
body_error = check_body_compliance(body, config_manager, opts)
|
103
104
|
return body_error unless body_error.nil?
|
104
105
|
end
|
105
106
|
|
@@ -108,8 +109,8 @@ module JSONAPI
|
|
108
109
|
# @return [NilClass | Array] Nil meaning no error or a 400 level http response
|
109
110
|
def check_headers_compliance(env, config_manager, opts)
|
110
111
|
JSONAPI::Exceptions::HeadersExceptions.check_request(env, config_manager, opts)
|
111
|
-
rescue JSONAPI::Exceptions::HeadersExceptions::InvalidHeader
|
112
|
-
raise if environment_development?
|
112
|
+
rescue JSONAPI::Exceptions::HeadersExceptions::InvalidHeader, JSONAPI::Exceptions::UserDefinedExceptions::InvalidHeader => e
|
113
|
+
raise if environment_development?
|
113
114
|
|
114
115
|
[e.status_code, {}, []]
|
115
116
|
end
|
@@ -117,10 +118,10 @@ module JSONAPI
|
|
117
118
|
# @param query_params [Hash] The rack request query_param hash
|
118
119
|
# @raise If the query parameters are not JSONAPI compliant
|
119
120
|
# @return [NilClass | Array] Nil meaning no error or a 400 level http response
|
120
|
-
def check_query_param_compliance(
|
121
|
+
def check_query_param_compliance(query_params, config_manager, opts)
|
121
122
|
JSONAPI::Exceptions::QueryParamsExceptions.check_compliance(query_params, config_manager, opts)
|
122
|
-
rescue JSONAPI::Exceptions::QueryParamsExceptions::InvalidQueryParameter
|
123
|
-
raise if environment_development?
|
123
|
+
rescue JSONAPI::Exceptions::QueryParamsExceptions::InvalidQueryParameter, JSONAPI::Exceptions::UserDefinedExceptions::InvalidQueryParam => e
|
124
|
+
raise if environment_development?
|
124
125
|
|
125
126
|
[e.status_code, {}, []]
|
126
127
|
end
|
@@ -128,17 +129,14 @@ module JSONAPI
|
|
128
129
|
# @param env (see #call)
|
129
130
|
# @param req (see #check_query_param_compliance)
|
130
131
|
# @raise If the document body is not JSONAPI compliant
|
131
|
-
def
|
132
|
-
# Store separately so you can rewind for next middleware or app
|
133
|
-
body = env['rack.input'].read
|
134
|
-
env['rack.input'].rewind
|
132
|
+
def check_body_compliance(body, config_manager, opts)
|
135
133
|
JSONAPI::Exceptions::DocumentExceptions.check_compliance(body, config_manager, opts)
|
136
|
-
rescue JSONAPI::Exceptions::DocumentExceptions::InvalidDocument
|
137
|
-
raise if environment_development?
|
134
|
+
rescue JSONAPI::Exceptions::DocumentExceptions::InvalidDocument, JSONAPI::Exceptions::UserDefinedExceptions::InvalidDocument => e
|
135
|
+
raise if environment_development?
|
138
136
|
|
139
137
|
[e.status_code, {}, []]
|
140
|
-
rescue
|
141
|
-
raise if environment_development?
|
138
|
+
rescue JSONAPI::Exceptions::JSONParseError
|
139
|
+
raise if environment_development?
|
142
140
|
|
143
141
|
[400, {}, []]
|
144
142
|
end
|
@@ -151,8 +149,8 @@ module JSONAPI
|
|
151
149
|
end
|
152
150
|
|
153
151
|
# @param (see #call)
|
154
|
-
def environment_development?
|
155
|
-
|
152
|
+
def environment_development?
|
153
|
+
ENV['RACK_ENV'].to_s.downcase == 'development' || ENV['RACK_ENV'].nil?
|
156
154
|
end
|
157
155
|
end
|
158
156
|
end
|