Sha256: 57968e3fcaab43520ccb9d7f36fba60e76697d66094d9df82c6a1342bb9e07bb

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

require 'faraday'
require 'json'

module CMIS
  class Connection
    class ResponseParser < Faraday::Middleware
      JSON_CONTENT_TYPE = /\/(x-)?json(;.+?)?$/i.freeze

      def call(env)
        @app.call(env).on_complete do |env|
          case env[:status]
          when 401
            raise Exceptions::Unauthorized
          else
            if env[:response_headers][:content_type] =~ JSON_CONTENT_TYPE
              parse_body(env)
              check_for_cmis_exception!(env[:body])
            end
          end
        end
      end

      private

      def parse_body(env)
        unless env[:response_headers][:content_disposition]
          env[:body] = JSON.parse(env[:body])
        end
      end

      def check_for_cmis_exception!(body)
        return unless body.is_a?(Hash)

        if exception = body['exception']
          raise exception_class(exception), "#{exception}: #{body['message']}"
        end
      end

      def exception_class(exception)
        clazz = exception.dup
        clazz[0] = clazz[0].upcase
        CMIS::Exceptions.const_get(clazz)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cmis-ruby-0.5 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.11 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.10 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.9 lib/cmis/connection/response_parser.rb