Parent

FaradayMiddleware::ParseJson::MimeTypeFix

Public: Override the content-type of the response with "application/json" if the response body looks like it might be JSON, i.e. starts with an open bracket.

This is to fix responses from certain API providers that insist on serving JSON with wrong MIME-types such as "text/javascript".

Public Instance Methods

first_char(body) click to toggle source
# File lib/faraday_middleware/response/parse_json.rb, line 37
def first_char(body)
  idx = -1
  begin
    char = body[idx += 1]
    char = char.chr if char
  end while char and WHITESPACE.include? char
  char
end
parse_response?(env) click to toggle source
# File lib/faraday_middleware/response/parse_json.rb, line 33
def parse_response?(env)
  super and BRACKETS.include? first_char(env[:body])
end
process_response(env) click to toggle source
# File lib/faraday_middleware/response/parse_json.rb, line 23
def process_response(env)
  old_type = env[:response_headers][CONTENT_TYPE].to_s
  new_type = MIME_TYPE.dup
  new_type << ';' << old_type.split(';', 2).last if old_type.index(';')
  env[:response_headers][CONTENT_TYPE] = new_type
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.