Sha256: b837f502671dd334ea8cf17883d1c803a13a252bcd679a9ad377bc2263e4a5e2

Contents?: true

Size: 745 Bytes

Versions: 1

Compression:

Stored size: 745 Bytes

Contents

require 'yajl'

class Zeppelin
  # Middleware for Faraday that parses JSON response bodies. Based on code in
  # the FaradayMiddleware project.
  #
  # @private
  class JsonParserMiddleware < 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] = Yajl::Parser.parse(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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zeppelin-0.5.0 lib/zeppelin/json_parser_middleware.rb