class Faraday::Response::Json
Parse response bodies as JSON.
Public Class Methods
new(app = nil, options = {})
click to toggle source
Calls superclass method
Faraday::Middleware::new
# File lib/faraday/response/json.rb, line 9 def initialize(app = nil, options = {}) super(app) @parser_options = options[:parser_options] @content_types = Array(options[:content_type] || /\bjson$/) @preserve_raw = options[:preserve_raw] end
Public Instance Methods
on_complete(env)
click to toggle source
# File lib/faraday/response/json.rb, line 16 def on_complete(env) process_response(env) if parse_response?(env) end
Private Instance Methods
parse(body)
click to toggle source
# File lib/faraday/response/json.rb, line 29 def parse(body) ::JSON.parse(body, @parser_options || {}) unless body.strip.empty? end
parse_response?(env)
click to toggle source
# File lib/faraday/response/json.rb, line 33 def parse_response?(env) process_response_type?(env) && env[:body].respond_to?(:to_str) end
process_response(env)
click to toggle source
# File lib/faraday/response/json.rb, line 22 def process_response(env) env[:raw_body] = env[:body] if @preserve_raw env[:body] = parse(env[:body]) rescue StandardError, SyntaxError => e raise Faraday::ParsingError.new(e, env[:response]) end
process_response_type?(env)
click to toggle source
# File lib/faraday/response/json.rb, line 38 def process_response_type?(env) type = response_type(env) @content_types.empty? || @content_types.any? do |pattern| pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern end end
response_type(env)
click to toggle source
# File lib/faraday/response/json.rb, line 45 def response_type(env) type = env[:response_headers][CONTENT_TYPE].to_s type = type.split(';', 2).first if type.index(';') type end