Class: Restforce::Middleware::JsonRequest

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/restforce/middleware/json_request.rb

Overview

Request middleware that encodes the body as JSON.

Processes only requests with matching Content-type or those without a type. If a request doesn’t have a type but has a body, it sets the Content-type to JSON MIME-type.

Doesn’t try to encode bodies that already are in string form. rubocop:disable Style/ClassAndModuleChildren

Constant Summary collapse

CONTENT_TYPE =

This is the only line that differs substantively from the version in Faraday. In Faraday, this refers to a Faraday constant.

'Content-Type'
MIME_TYPE =
'application/json'
MIME_TYPE_REGEX =
%r{^application/(vnd\..+\+)?json$}

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

Taken from ‘lib/faraday/middleware.rb` in the `faraday` gem (<github.com/lostisland/faraday/blob/08b7d4d/lib/faraday/middleware.rb>), with a tiny adaptation to refer to the `@app` instance variable rather than expecting an `attr_reader` to exist.

In Faraday versions before v1.2.0, ‘#call` is missing.

Copyright © 2009-2022 Rick Olson, Zack Hobson Licensed under the MIT License.



47
48
49
50
51
52
# File 'lib/restforce/middleware/json_request.rb', line 47

def call(env)
  on_request(env) if respond_to?(:on_request)
  @app.call(env).on_complete do |environment|
    on_complete(environment) if respond_to?(:on_complete)
  end
end

#on_request(env) ⇒ Object



54
55
56
57
58
# File 'lib/restforce/middleware/json_request.rb', line 54

def on_request(env)
  match_content_type(env) do |data|
    env[:body] = encode(data)
  end
end