class ElastomerClient::Middleware::ParseJson

Parse response bodies as JSON.

Constants

CONTENT_TYPE
MIME_TYPE

Public Instance Methods

call(environment) click to toggle source
# File lib/elastomer_client/middleware/parse_json.rb, line 11
def call(environment)
  @app.call(environment).on_complete do |env|
    if process_response?(env)
      env[:body] = parse env[:body]
    end
  end
end
parse(body) click to toggle source

Parse the response body.

# File lib/elastomer_client/middleware/parse_json.rb, line 20
def parse(body)
  MultiJson.load(body) if body.respond_to?(:to_str) && !body.strip.empty?
rescue StandardError, SyntaxError => e
  raise Faraday::ParsingError, e
end
process_response?(env) click to toggle source
# File lib/elastomer_client/middleware/parse_json.rb, line 26
def process_response?(env)
  type = response_type(env)
  type.empty? || type == MIME_TYPE
end
response_type(env) click to toggle source
# File lib/elastomer_client/middleware/parse_json.rb, line 31
def response_type(env)
  type = env[:response_headers][CONTENT_TYPE].to_s
  type = type.split(";", 2).first if type.index(";")
  type
end