Sha256: 838fa0c0ec88b0ee52c4da8add87d8a42b488bf4ea71907fb695d858ef018674

Contents?: true

Size: 979 Bytes

Versions: 6

Compression:

Stored size: 979 Bytes

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
              env[:body] = JSON.parse(env[:body])
              check_for_cmis_exception!(env[:body])
            end
          end
        end
      end

      private

      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

6 entries across 6 versions & 1 rubygems

Version Path
cmis-ruby-0.4.8 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.7 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.6 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.5 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.4 lib/cmis/connection/response_parser.rb
cmis-ruby-0.4.3 lib/cmis/connection/response_parser.rb