cp-sparrow 0.0.12 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.ruby-version +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +96 -13
- data/lib/sparrow.rb +21 -4
- data/lib/sparrow/configuration.rb +23 -2
- data/lib/sparrow/core_ext/hash.rb +2 -0
- data/lib/sparrow/dependencies.rb +15 -0
- data/lib/sparrow/http_message.rb +103 -0
- data/lib/sparrow/logger.rb +53 -0
- data/lib/sparrow/middleware.rb +44 -63
- data/lib/sparrow/request_http_message.rb +22 -0
- data/lib/sparrow/request_middleware.rb +8 -10
- data/lib/sparrow/response_http_message.rb +49 -0
- data/lib/sparrow/response_middleware.rb +32 -28
- data/lib/sparrow/response_steward.rb +13 -0
- data/lib/sparrow/route_parser.rb +28 -5
- data/lib/sparrow/steward.rb +125 -0
- data/lib/sparrow/strategies.rb +6 -0
- data/lib/sparrow/strategies/form_hash.rb +27 -11
- data/lib/sparrow/strategies/ignore.rb +33 -5
- data/lib/sparrow/strategies/json_format_strategies.rb +4 -0
- data/lib/sparrow/strategies/json_format_strategies/array_json_format_strategy.rb +23 -0
- data/lib/sparrow/strategies/json_format_strategies/default_json_format_strategy.rb +11 -2
- data/lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb +21 -6
- data/lib/sparrow/strategies/json_format_strategies/rack_body_json_format_strategy.rb +22 -0
- data/lib/sparrow/strategies/key_transformation.rb +2 -0
- data/lib/sparrow/strategies/key_transformation/camelize_key.rb +45 -17
- data/lib/sparrow/strategies/key_transformation/underscore_key.rb +17 -6
- data/lib/sparrow/strategies/raw_input.rb +29 -7
- data/lib/sparrow/strategies/transform_params.rb +46 -21
- data/lib/sparrow/transformable.rb +28 -20
- data/lib/sparrow/version.rb +1 -1
- data/sparrow.gemspec +1 -1
- data/spec/integration/apps/rails_app/config/boot.rb +1 -1
- data/spec/integration/rack/camel_caser_spec.rb +2 -2
- data/spec/unit/camelize_key_spec.rb +9 -7
- data/spec/unit/configuration_spec.rb +3 -0
- data/spec/unit/http_message.rb +66 -0
- data/spec/unit/logger_spec.rb +6 -0
- data/spec/unit/{camel_caser_spec.rb → sparrow_spec.rb} +8 -2
- data/spec/unit/steward_spec.rb +31 -0
- metadata +24 -13
- data/lib/sparrow/path_normalizer.rb +0 -10
- data/lib/sparrow/strategies/json_format_strategies/array_strategy.rb +0 -17
- data/lib/sparrow/strategies/json_format_strategies/rack_body.rb +0 -17
- data/lib/sparrow/strategies/key_transformation/key_normalizer.rb +0 -9
- data/spec/unit/path_normalizer_spec.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d21e1f781a7f5f4a27d6d8c286cd46d9af31cba4
|
4
|
+
data.tar.gz: 838ed5f0629f44eb18c8041c1dcaf3d5048b1630
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99f670f75b1a3dc078130af3d946738f80705f1c79e26922afbbcba0921aecaa1c38bc7edd1253fb3f0a07fbad8c9635f5d6a7e332a407b4109d8b122a59c301
|
7
|
+
data.tar.gz: cd0a34026665ddfd21c075a05af4839b5f1995cc0d45d70009a205329a2862dd8eb752b243f5fa6c1937b2e973245a3130a2d2c33d25ced0bccbef71761b41df
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.2
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -25,7 +25,8 @@ If you're using Rails, that's it. You haven't to do anything more. If you're not
|
|
25
25
|
```rb
|
26
26
|
require 'sparrow'
|
27
27
|
|
28
|
-
use Sparrow::
|
28
|
+
use Sparrow::RequestMiddleware
|
29
|
+
use Sparrow::ResponseMiddleware
|
29
30
|
```
|
30
31
|
|
31
32
|
## Configuration
|
@@ -43,18 +44,100 @@ Sparrow.configure do |config|
|
|
43
44
|
end
|
44
45
|
```
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
47
|
+
Options may be:
|
48
|
+
|
49
|
+
### excluded_routes
|
50
|
+
|
51
|
+
> `Array(String|Regexp)`. Default: `[]`
|
52
|
+
|
53
|
+
An Array of Strings and/or Regexps defining which paths should not be touched by the middleware. The entries should matchs paths for your application. They should *not* start with a leading slash.
|
54
|
+
|
55
|
+
Example:
|
56
|
+
`config.excluded_routes = ['api/model/certificates']`
|
57
|
+
|
58
|
+
|
59
|
+
### default_json_request_key_transformation_strategy
|
60
|
+
|
61
|
+
> `String`. Default: `camelize`
|
62
|
+
|
63
|
+
Defines how the middleware should treat incoming parameters via Request. Which means how they get tranformed, i.e. defining _underscore_ here means that incoming parameters get underscore. Possible values are _underscore_ and _camelize_
|
64
|
+
|
65
|
+
Example:
|
66
|
+
`config.default_json_request_key_transformation_strategy = "underscore"`
|
67
|
+
|
68
|
+
### default_json_response_key_transformation_strategy
|
69
|
+
|
70
|
+
> `String`. Default: `camelize`
|
71
|
+
|
72
|
+
Same as **default_json_request_key_transformation_strategy**, but for responses. I.e. this defines to which format the keys get transformed when the response gets sent.
|
73
|
+
|
74
|
+
Example:
|
75
|
+
`config.default_json_response_key_transformation_strategy = "camelize"`
|
76
|
+
|
77
|
+
### json_request_format_header
|
78
|
+
|
79
|
+
> `String`. Default: `request-json-format`
|
80
|
+
|
81
|
+
Defines the HTTP Header key which sets the request transformation strategy as in default_json_request_key_transformation_strategy*. This definition has higher priority than the default definition. Any valid HTTP Header String value is possible. Defaults to `request-json-format`.
|
82
|
+
|
83
|
+
Example:
|
84
|
+
> `config.json_request_format_header = "request-json-format"`
|
85
|
+
|
86
|
+
|
87
|
+
### json_response_format_header
|
88
|
+
|
89
|
+
> `String`. Default: `response-json-format`
|
90
|
+
|
91
|
+
Same as **json_request_format_header**, but for the response handling. Defaults to `response-json-format`
|
92
|
+
|
93
|
+
Example:
|
94
|
+
`config.json_response_format_header = "response-json-format"`
|
95
|
+
|
96
|
+
### camelize_ignore_uppercase_keys
|
97
|
+
|
98
|
+
> `Boolean`. Default: `true`
|
99
|
+
|
100
|
+
A boolean that indicates to not camelize keys that are all Uppercase, like CountryCodes "EN", etc.
|
101
|
+
|
102
|
+
Example:
|
103
|
+
`config.camelize_ignore_uppercase_keys = true `
|
104
|
+
|
105
|
+
### allowed_content_types
|
106
|
+
|
107
|
+
> `Array(String)`. Default: `%w[application/json application/x-www-form-urlencoded text/x-json]`
|
108
|
+
|
109
|
+
A list of HTTP content types upon which the middleware shall trigger and possibly start conversion. Defaults to `['application/json', 'application/x-www-form-urlencoded', 'text/x-json']`. If `nil` is present in the list, requests/responses with *no* Content-Type header will be processed as well. Possible values can also be the start of the content-type-header like ```application/``` which matches everything that starts with ```application/``` like ```application/json```
|
110
|
+
|
111
|
+
Example:
|
112
|
+
`config.allowed_content_types = ['application/json', 'text/x-json']`
|
113
|
+
|
114
|
+
### allowed_accepts
|
115
|
+
|
116
|
+
> `Array(String)`. Default: `['application/json', 'application/x-www-form-urlencoded', 'text/x-json', nil]`
|
117
|
+
|
118
|
+
Same as [allowed_content_types], but reacts to the HTTP Accept Header. Applies to the same possible options, behavior. Defaults to the same set of MIME types, but also includes `nil` by default, which ignores checking the Accept header in default configuration
|
119
|
+
|
120
|
+
Example:
|
121
|
+
`config.allowed_accepts = ['application/json', 'text/x-json']`
|
122
|
+
|
123
|
+
### enable_logging
|
124
|
+
|
125
|
+
> `Boolean`. Default: `false`
|
126
|
+
|
127
|
+
Determines logging while the middleware is active. Primary output is which conversion strategy gets chosen upon which request/response.
|
128
|
+
|
129
|
+
Example:
|
130
|
+
`config.enable_logging = true`
|
131
|
+
|
132
|
+
### camelize_strategy
|
133
|
+
|
134
|
+
> `Symbol`. Default: `:lower`
|
135
|
+
|
136
|
+
Defines which strategy to use when camelizing keys. Possible options are `:lower` and `:upper`, which tells the middleware to
|
137
|
+
start camelized keys with an uppercased character or with a lowercased character.
|
138
|
+
|
139
|
+
Example:
|
140
|
+
`config.camelize_strategy = :lower`
|
58
141
|
|
59
142
|
## Tests
|
60
143
|
|
data/lib/sparrow.rb
CHANGED
@@ -1,25 +1,42 @@
|
|
1
|
-
require '
|
1
|
+
require 'sparrow/dependencies'
|
2
2
|
require 'sparrow/version'
|
3
3
|
require 'sparrow/configuration'
|
4
4
|
require 'sparrow/route_parser'
|
5
|
+
require 'sparrow/transformable'
|
6
|
+
require 'sparrow/strategies'
|
7
|
+
require 'sparrow/http_message'
|
8
|
+
require 'sparrow/request_http_message'
|
9
|
+
require 'sparrow/response_http_message'
|
10
|
+
require 'sparrow/steward'
|
11
|
+
require 'sparrow/response_steward'
|
12
|
+
require 'sparrow/middleware'
|
5
13
|
require 'sparrow/request_middleware'
|
6
14
|
require 'sparrow/response_middleware'
|
7
|
-
require 'sparrow/
|
8
|
-
require 'sparrow/strategies/json_format_strategies/array_strategy'
|
15
|
+
require 'sparrow/logger'
|
9
16
|
require 'sparrow/railtie' if defined?(Rails)
|
10
17
|
|
11
18
|
module Sparrow
|
12
19
|
class << self
|
20
|
+
# @yield [Configuration] configuration
|
13
21
|
def configure
|
14
22
|
yield configuration
|
15
23
|
end
|
16
24
|
|
25
|
+
# @return [Configuration] the configuration
|
17
26
|
def configuration
|
18
27
|
@configuration ||= Configuration.new
|
19
28
|
end
|
20
29
|
|
30
|
+
# resets the configuration values to their defaults, i.e.
|
31
|
+
# reinitializes the Configuration object without any arguments
|
32
|
+
# @return [Configuration] the (new initial) configuration
|
21
33
|
def reset_configuration
|
22
|
-
@configuration =
|
34
|
+
@configuration = Configuration.new
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Logger] the middleware's logger
|
38
|
+
def logger
|
39
|
+
@logger ||= Logger.new(configuration.enable_logging)
|
23
40
|
end
|
24
41
|
end
|
25
42
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
module Sparrow
|
2
|
+
# Middleware configuration store
|
3
|
+
# see {https://github.com/GateprotectGmbH/sparrow#configuration}
|
2
4
|
class Configuration
|
3
5
|
attr_accessor :json_request_format_header,
|
4
6
|
:json_response_format_header,
|
@@ -7,9 +9,15 @@ module Sparrow
|
|
7
9
|
:default_json_response_key_transformation_strategy,
|
8
10
|
:camelize_ignore_uppercase_keys,
|
9
11
|
:allowed_content_types,
|
10
|
-
:allowed_accepts
|
12
|
+
:allowed_accepts,
|
13
|
+
:enable_logging,
|
14
|
+
:ignored_response_codes,
|
15
|
+
:camelize_strategy
|
11
16
|
|
17
|
+
##
|
18
|
+
# Initializes a new Configuration with default parameters
|
12
19
|
def initialize
|
20
|
+
@enable_logging = false
|
13
21
|
@json_request_format_header = 'request-json-format'
|
14
22
|
@json_response_format_header = 'response-json-format'
|
15
23
|
@excluded_routes = []
|
@@ -22,13 +30,26 @@ module Sparrow
|
|
22
30
|
text/x-json
|
23
31
|
]
|
24
32
|
|
25
|
-
@allowed_accepts
|
33
|
+
@allowed_accepts = @allowed_content_types + [nil]
|
34
|
+
@ignored_response_codes = [404] + (500..511).to_a
|
35
|
+
@camelize_strategy = :lower
|
26
36
|
end
|
27
37
|
|
38
|
+
##
|
39
|
+
# @param type [String] the http message type.
|
40
|
+
# Must be either 'request' or 'response'.
|
41
|
+
# @return [String] the configuration value for the json_format_header for
|
42
|
+
# the given http message type
|
28
43
|
def json_format_header(type)
|
29
44
|
public_send("json_#{type}_format_header")
|
30
45
|
end
|
31
46
|
|
47
|
+
##
|
48
|
+
# the default json_key_transformation_strategy option for the given
|
49
|
+
# http message type
|
50
|
+
# @param type [String] http message type. Must be either 'request' or
|
51
|
+
# 'response'
|
52
|
+
# @return [String] the configuration option value
|
32
53
|
def default_json_key_transformation_strategy(type)
|
33
54
|
public_send("default_json_#{type}_key_transformation_strategy")
|
34
55
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_support/core_ext/object'
|
2
|
+
require 'active_support/core_ext/string'
|
3
|
+
require 'active_support/version'
|
4
|
+
|
5
|
+
if ActiveSupport::VERSION::STRING.match(/3\.\d+\.\d+/)
|
6
|
+
require 'sparrow/core_ext/hash'
|
7
|
+
require 'active_support/core_ext/object/to_param'
|
8
|
+
require 'active_support/core_ext/object/to_query'
|
9
|
+
end
|
10
|
+
|
11
|
+
unless defined?(Rails)
|
12
|
+
require 'active_support/core_ext/logger'
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'singleton'
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Sparrow
|
2
|
+
##
|
3
|
+
# Wrapper class for either a ::ActiveDispatch::Request or ::Rack::Request
|
4
|
+
# instance for the given rack environment.
|
5
|
+
# The wrapped class is determined based on the presence of the Rails constant.
|
6
|
+
# @abstract RequestHttpMessage and ResponseHttpMessage should be used in
|
7
|
+
# practice
|
8
|
+
class HttpMessage
|
9
|
+
# The rack environment hash key that determines if it is a form hash request
|
10
|
+
FORM_HASH_KEY = 'rack.request.form_hash'
|
11
|
+
# The rack environment hash key to access the input/output
|
12
|
+
RACK_INPUT_KEY = 'rack.input'
|
13
|
+
|
14
|
+
##
|
15
|
+
# @return [Hash] the Rack environment
|
16
|
+
# @see #initialize
|
17
|
+
attr_reader :env
|
18
|
+
|
19
|
+
##
|
20
|
+
# Initializes the HttpMessage
|
21
|
+
# @param [Hash] env The Rack environment
|
22
|
+
def initialize(env)
|
23
|
+
@env = env
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Depending on the environment this attribute may either be a
|
28
|
+
# [::ActionDispatch::Request], when running in a Rails environment,
|
29
|
+
# or a [::Rack::Request] otherwise
|
30
|
+
# Encapsulates the Rack env.
|
31
|
+
# @see #env
|
32
|
+
# @return [Object]
|
33
|
+
def request
|
34
|
+
@request ||= request_class.new(env)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Boolean] true any values is insides the FORM_HASH_KEY of the
|
38
|
+
# rack environment
|
39
|
+
# @see ::FORM_HASH_KEY
|
40
|
+
# @see #env
|
41
|
+
def form_hash?
|
42
|
+
env[FORM_HASH_KEY].present?
|
43
|
+
end
|
44
|
+
|
45
|
+
# Requested path within this HTTP message
|
46
|
+
# @return [String] the path
|
47
|
+
def path
|
48
|
+
http_header(:path_info)
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# The HTTP Accept Header field
|
53
|
+
# @return String the HTTP Accept Header value
|
54
|
+
def accept
|
55
|
+
http_header(:accept)
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# The HTTP Content Type Field
|
60
|
+
# @return String the HTTP Content-Type Header value
|
61
|
+
def content_type
|
62
|
+
http_header(:content_type)
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# Delegates all unknown method calls to the wrapped request
|
67
|
+
# @see #request
|
68
|
+
def method_missing(method_name, *args)
|
69
|
+
request.public_send(method_name, *args)
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def request_class
|
75
|
+
if defined?(Rails)
|
76
|
+
::ActionDispatch::Request
|
77
|
+
else
|
78
|
+
::Rack::Request
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Make sure to use any appropriate format of common HTTP Header key syntax
|
84
|
+
# for the given key
|
85
|
+
def http_header(key)
|
86
|
+
key = key.to_s
|
87
|
+
header_key = [
|
88
|
+
key,
|
89
|
+
key.upcase,
|
90
|
+
key.upcase.dasherize,
|
91
|
+
key.humanize,
|
92
|
+
key.dasherize,
|
93
|
+
key.parameterize,
|
94
|
+
key.underscore.split('_').map(&:humanize).join('-')
|
95
|
+
].detect do |transformed_key|
|
96
|
+
headers_hash[transformed_key]
|
97
|
+
end
|
98
|
+
|
99
|
+
return nil unless header_key
|
100
|
+
headers_hash[header_key].to_s.split(';').first
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Sparrow
|
2
|
+
##
|
3
|
+
# Simple Logger class to handle behavior's in Rails and Rack environment
|
4
|
+
# without the need to depend on an a "real" Logger to be available under the
|
5
|
+
# hood.
|
6
|
+
#
|
7
|
+
# If the middleware is running in a Rails environment
|
8
|
+
# (i.e. the Rails-constant is defined), is will delegate all its method calls
|
9
|
+
# to the Rails logger.
|
10
|
+
# Otherwise a simple log to STDOUT will get triggered using the method name as
|
11
|
+
# log level and the argument as message.
|
12
|
+
#
|
13
|
+
# Examples:
|
14
|
+
#
|
15
|
+
# Sparrow::Logger.debug('this is a debug message')
|
16
|
+
#
|
17
|
+
# when in a Rails env equals
|
18
|
+
#
|
19
|
+
# Rails.logger.debug('this is a debug message')
|
20
|
+
#
|
21
|
+
# when not in a Rails environment the same call equals
|
22
|
+
#
|
23
|
+
# ActiveSupport::Logger.debug("this is a debug message")
|
24
|
+
class Logger
|
25
|
+
##
|
26
|
+
# @return [Boolean] logging enabled
|
27
|
+
attr_accessor :enabled
|
28
|
+
alias_method :enabled?, :enabled
|
29
|
+
|
30
|
+
##
|
31
|
+
# Wrapped Logger class
|
32
|
+
# the Rails logger or a plain ActiveSupport::Logger instance using STDOUT
|
33
|
+
attr_reader :logger
|
34
|
+
|
35
|
+
# Initialize the Logger
|
36
|
+
# Enables the logging only if +enabled+ is truthy.
|
37
|
+
# Otherwise the logger will do nothing at all.
|
38
|
+
#
|
39
|
+
# @param [Boolean] enabled logging enabled
|
40
|
+
def initialize(enabled)
|
41
|
+
self.enabled = enabled
|
42
|
+
@logger = if defined?(Rails) then
|
43
|
+
Rails.logger
|
44
|
+
else
|
45
|
+
::Logger.new(STDOUT)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def method_missing(method_name, *args)
|
50
|
+
logger.public_send(method_name, *args) if enabled?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/sparrow/middleware.rb
CHANGED
@@ -1,88 +1,69 @@
|
|
1
|
-
require 'active_support/core_ext/object/blank'
|
2
|
-
require 'sparrow/strategies/form_hash'
|
3
|
-
require 'sparrow/strategies/raw_input'
|
4
|
-
require 'sparrow/strategies/ignore'
|
5
|
-
|
6
1
|
module Sparrow
|
2
|
+
##
|
3
|
+
# Parent middleware class for both Requests and Responses.
|
4
|
+
# Provides common accessors, but does not modify the http message.
|
7
5
|
class Middleware
|
8
|
-
|
9
|
-
|
6
|
+
# @return [Object] the parent Rack application in the stack.
|
7
|
+
attr_reader :app
|
8
|
+
# @return [Array<String>] the rack body after the middleware call
|
9
|
+
attr_reader :body
|
10
|
+
# @return [Hash<String, String>] the HTTP Headers after the middleware call
|
11
|
+
attr_reader :headers
|
12
|
+
# @return [Integer] the HTTP status after the middleware call
|
13
|
+
attr_reader :status
|
14
|
+
# @return [Array<Integer>] List of ignored HTTP response codes
|
15
|
+
# @see Configuration#ignored_response_codes
|
16
|
+
attr_reader :ignored_response_codes
|
17
|
+
|
18
|
+
|
19
|
+
##
|
20
|
+
# Creates a new Middleware object
|
21
|
+
# @param [#call] app the Rack application as defined by. Any object that
|
22
|
+
# responds to #call as defined in {https://rack.github.io/}.
|
10
23
|
def initialize(app)
|
11
|
-
@app
|
24
|
+
@app = app
|
25
|
+
@ignored_response_codes = ignored_response_codes
|
12
26
|
end
|
13
27
|
|
28
|
+
# @return [Array] The Rack tuple of status, headers and body
|
29
|
+
# as defined by {https://rack.github.io/}
|
30
|
+
# Does nothing by default.
|
14
31
|
def call(env)
|
15
32
|
@last_env = env
|
16
33
|
@status, @headers, @body = @app.call(convert(env))
|
17
34
|
end
|
18
35
|
|
36
|
+
# Converts the Rack environment
|
37
|
+
# Does nothing.
|
38
|
+
# @param [Hash] env the rack env
|
39
|
+
# @return [Hash] the rack env
|
19
40
|
def convert(env)
|
20
41
|
env
|
21
42
|
end
|
22
43
|
|
23
44
|
private
|
24
45
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
Strategies::Ignore
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def is_processable?
|
36
|
-
accepted_content_type? && accepted_accept_header? && includes_route?
|
46
|
+
def steward
|
47
|
+
Steward.new(http_message,
|
48
|
+
allowed_content_types: Sparrow.configuration.allowed_content_types,
|
49
|
+
allowed_accepts: Sparrow.configuration.allowed_accepts,
|
50
|
+
excluded_routes: Sparrow.configuration.excluded_routes,
|
51
|
+
ignored_response_codes: Sparrow.configuration.ignored_response_codes)
|
37
52
|
end
|
38
53
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
def accepted_accept_header?
|
49
|
-
allowed_accepts = Sparrow.configuration.allowed_accepts
|
50
|
-
accept_header = last_env['ACCEPT'] || last_env['Accept']
|
51
|
-
|
52
|
-
allowed_accepts.include?(nil) || accept_type_matches?(allowed_accepts, accept_header)
|
54
|
+
def strategy
|
55
|
+
strategy = if steward.has_processable_http_message?
|
56
|
+
Strategies::RawInput
|
57
|
+
else
|
58
|
+
Strategies::Ignore
|
59
|
+
end
|
60
|
+
|
61
|
+
Sparrow.logger.debug("#{self.class.name} choosing strategy #{strategy.name}")
|
62
|
+
strategy
|
53
63
|
end
|
54
64
|
|
55
65
|
def last_env
|
56
66
|
@last_env || {}
|
57
67
|
end
|
58
|
-
|
59
|
-
def request
|
60
|
-
request_class = if defined?(Rails) then
|
61
|
-
ActionDispatch::Request
|
62
|
-
else
|
63
|
-
Rack::Request
|
64
|
-
end
|
65
|
-
|
66
|
-
request_class.new(last_env)
|
67
|
-
end
|
68
|
-
|
69
|
-
def content_type_equals?(type)
|
70
|
-
Sparrow.configuration.allowed_content_types.include?(type)
|
71
|
-
end
|
72
|
-
|
73
|
-
def content_type_matches?(type)
|
74
|
-
matches = Sparrow.configuration.allowed_content_types.map do |acceptable_content_type|
|
75
|
-
(acceptable_content_type && type.to_s.starts_with?(acceptable_content_type.to_s))
|
76
|
-
end
|
77
|
-
|
78
|
-
matches.any?
|
79
|
-
end
|
80
|
-
|
81
|
-
def accept_type_matches?(accepted_headers, type)
|
82
|
-
accepted_headers.detect do |accept|
|
83
|
-
type.include?(accept)
|
84
|
-
end
|
85
|
-
end
|
86
68
|
end
|
87
69
|
end
|
88
|
-
|