Class: CMIS::Connection::ResponseParser

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/cmis/connection/response_parser.rb

Constant Summary collapse

JSON_CONTENT_TYPE =
/\/(x-)?json(;.+?)?$/i.freeze

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cmis/connection/response_parser.rb', line 9

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

  response.on_complete do |env|

    # Remove Authorization header when following redirects
    # This hack should be removed when issue #81 is merged
    # Cf. https://github.com/lostisland/faraday_middleware/pull/81
    if [301, 302, 303, 307].include?(response.status)
      env[:request_headers].delete('Authorization')
    end

    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

rescue Faraday::ConnectionFailed
  raise Exceptions::Timeout
end