Class: FaradayStack::ResponseJSON::MimeTypeFix

Inherits:
FaradayStack::ResponseMiddleware show all
Defined in:
lib/faraday_stack/response_json.rb

Overview

writes the correct MIME-type if the payload looks like JSON

Constant Summary collapse

BRACKETS =
%w- [ { -

Constants inherited from FaradayStack::ResponseMiddleware

FaradayStack::ResponseMiddleware::CONTENT_TYPE

Instance Method Summary collapse

Methods inherited from FaradayStack::ResponseMiddleware

define_parser, inherited, #initialize, #parse, #parse_response?, #process_response_type?, #response_type

Constructor Details

This class inherits a constructor from FaradayStack::ResponseMiddleware

Instance Method Details

#looks_like_json?(env) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/faraday_stack/response_json.rb', line 45

def looks_like_json?(env)
  parse_response?(env) and BRACKETS.include? env[:body][0,1]
end

#on_complete(env) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/faraday_stack/response_json.rb', line 34

def on_complete(env)
  if process_response_type?(response_type(env)) and looks_like_json?(env)
    old_type = env[:response_headers][CONTENT_TYPE]
    new_type = 'application/json'
    new_type << ';' << old_type.split(';', 2).last if old_type.index(';')
    env[:response_headers][CONTENT_TYPE] = new_type
  end
end