Class: FaradayMiddleware::Jsons::Encoder

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_middleware/jsons/request/encoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Encoder

Returns a new instance of Encoder.


3
4
5
6
7
8
9
# File 'lib/faraday_middleware/jsons/request/encoder.rb', line 3

def initialize(app, options = {})
  super(app)

  @pretty       = options.delete(:pretty)       || false
  @content_type = options.delete(:content_type) || %r!^application/(.*\+)?json!
  @options      = options
end

Instance Method Details

#call(env) ⇒ Object


11
12
13
14
15
16
17
18
19
# File 'lib/faraday_middleware/jsons/request/encoder.rb', line 11

def call(env)
  if match_http_method?(env) && match_content_type?(env)
    # do not encode multiple times when the same middleware is decrared
    # unicode string is valid for json (but escaping is also skipped...)
    env[:body] = MultiJson.dump(env[:body], @options.merge(pretty: @pretty)) unless env[:body].is_a?(String)
  end

  @app.call(env)
end