Class: FaradayJSON::ParseJsonMimeTypeFix
- Defined in:
- lib/faraday_json/parse_json.rb
Overview
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”.
Constant Summary collapse
- MIME_TYPE =
'application/json'.freeze
- BRACKETS =
%w- [ { -
- WHITESPACE =
[ " ", "\n", "\r", "\t" ]
Constants inherited from ParseJson
FaradayJSON::ParseJson::CONTENT_TYPE
Instance Method Summary collapse
Methods inherited from ParseJson
#call, #initialize, #preserve_raw?, #process_response_type?, #response_charset, #response_type
Methods included from Encoding
#bin_to_hex, #get_bom, #get_canonical_encoding, #get_dominant_encoding, #strip_bom, #to_utf8, #transcode
Constructor Details
This class inherits a constructor from FaradayJSON::ParseJson
Instance Method Details
#first_char(body) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/faraday_json/parse_json.rb', line 152 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) ⇒ Boolean
148 149 150 |
# File 'lib/faraday_json/parse_json.rb', line 148 def parse_response?(env) super and BRACKETS.include? first_char(env[:body]) end |
#process_response(env) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/faraday_json/parse_json.rb', line 138 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 |