artsy/artsy-ruby-client

View on GitHub
lib/artsy/client/response/parse_json.rb

Summary

Maintainability
A
45 mins
Test Coverage
module Artsy
  module Client
    module Response
      class ParseJson < Faraday::Response::Middleware
        def parse(body)
          case body
          when /\A^\s*$\z/, nil
            nil
          else
            MultiJson.decode(body)
          end
        end

        def on_complete(env)
          if (env[:response_headers][:content_type] || "").start_with? 'application/json'
            if respond_to?(:parse)
              env[:body] = parse(env[:body]) unless [204, 301, 302, 304].include?(env[:status])
            end
          end
        end
      end
    end
  end
end