Sha256: 9fed1bab1cd8208a0295066dd9dddc61448bb3cf4dfd49e9b379fa8046f364a8

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'faraday'

module Saddle
  module Middleware
    module Response

      # Public: Parse response bodies as JSON.
      class ParseJson < Faraday::Middleware

        CONTENT_TYPE = 'Content-Type'.freeze
        MIME_TYPE    = 'application/json'.freeze

        dependency do
          require 'json' unless defined?(::JSON)
        end if Faraday::VERSION < '2'

        def call(env)
          result = @app.call(env)

          if parse_response?(result)
            result.env[:body] = ::JSON.parse(result.env[:body])
          end
          result
        end


        private

        def parse_response?(result)
          has_body?(result) && (response_type(result) == MIME_TYPE)
        end

        def has_body?(result)
          result.env[:body] &&
            (!result.env[:body].respond_to?(:to_str) || # must already be in string format
             !result.env[:body].empty?) # or must be non-empty
        end

        def response_type(result)
          return nil unless result.headers
          type = result.headers[CONTENT_TYPE].to_s
          type = type.split(';', 2).first if type.index(';')
          type
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saddle-0.2.2 lib/saddle/middleware/response/parse_json.rb