Class: Shark::Middleware::ComposeRequest
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Shark::Middleware::ComposeRequest
- Defined in:
- lib/shark/middleware/compose_request.rb
Constant Summary collapse
- HTTP_METHODS_WITH_BODY =
%i[post patch put].freeze
- CONTENT_TYPE =
'Content-Type'
- JSON_MIME_TYPE =
'application/json'
- JSON_MIME_TYPE_REGEX =
%r{^application/(vnd\..+\+)?json$}.freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
- #compose_request_body(env) ⇒ Object
- #request_type(env) ⇒ Object
- #request_with_body?(env) ⇒ Boolean
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 |
# File 'lib/shark/middleware/compose_request.rb', line 11 def call(env) compose_request_body(env) if request_with_body?(env) @app.call(env) end |
#compose_request_body(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/shark/middleware/compose_request.rb', line 21 def compose_request_body(env) type = request_type(env) params = env[:body] || {} params = if JSON_MIME_TYPE_REGEX =~ type ::JSON.dump(params) else params.to_param end env[:body] = params end |
#request_type(env) ⇒ Object
34 35 36 37 38 |
# File 'lib/shark/middleware/compose_request.rb', line 34 def request_type(env) type = env[:request_headers][CONTENT_TYPE].to_s type = type.split(';', 2).first if type.index(';') type end |
#request_with_body?(env) ⇒ Boolean
17 18 19 |
# File 'lib/shark/middleware/compose_request.rb', line 17 def request_with_body?(env) HTTP_METHODS_WITH_BODY.include?(env[:method]) end |