Sha256: ba633689c63e05f51f52c90b8a362bbdd10f899c1228a250bbac52ec475c792b

Contents?: true

Size: 815 Bytes

Versions: 1

Compression:

Stored size: 815 Bytes

Contents

require 'multi_json'

class Zeppelin
  module Middleware
    # Middleware for Faraday that parses JSON response bodies. Based on code in
    # the FaradayMiddleware project.
    #
    # @private
    class JsonParser < Faraday::Middleware
      CONTENT_TYPE = 'Content-Type'

      def initialize(app=nil)
        @app = app
      end

      def call(env)
        @app.call(env).on_complete do
          parse_response(env) if process_content_type?(env) && parse_response?(env)
        end
      end

      private

      def parse_response(env)
        env[:body] = MultiJson.decode(env[:body])
      end

      def process_content_type?(env)
        env[:response_headers][CONTENT_TYPE].to_s =~ /\bjson$/
      end

      def parse_response?(env)
        env[:body].respond_to? :to_str
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zeppelin-0.6.0 lib/zeppelin/middleware/json_parser.rb