Sha256: d57b583946993733dcd0ac65d6875860a8798b8e6bd9cf6fdb6634381afe443f

Contents?: true

Size: 814 Bytes

Versions: 10

Compression:

Stored size: 814 Bytes

Contents

module Redd
  # The module that contains middleware that alters the Faraday response.
  module Response
    # Faraday Middleware that parses JSON using Oj.
    class ParseJson < Faraday::Middleware
      dependency do
        require "multi_json" unless defined?(::MultiJson)
      end

      # Call the middleware.
      # @param faraday
      def call(faraday)
        @app.call(faraday).on_complete do |env|
          env[:body] = parse(env[:body])
        end
      end

      private

      # Parse a JSON string and return a symbolized hash.
      #
      # @param [String] body The JSON string to parse.
      # @return [Hash] A symbolized parsed JSON hash.
      def parse(body)
        MultiJson.load(body, symbolize_keys: true)
      rescue MultiJson::ParseError
        body
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
redd-0.6.0 lib/redd/response/parse_json.rb
redd-0.5.0 lib/redd/response/parse_json.rb
redd-0.4.3 lib/redd/response/parse_json.rb
redd-0.4.2 lib/redd/response/parse_json.rb
redd-0.4.1 lib/redd/response/parse_json.rb
redd-0.4.0 lib/redd/response/parse_json.rb
redd-0.3.0 lib/redd/response/parse_json.rb
redd-0.2.0 lib/redd/response/parse_json.rb
redd-0.1.6 lib/redd/response/parse_json.rb
redd-0.1.5 lib/redd/response/parse_json.rb