Class: Duracloud::ResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/duracloud/response_handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



10
11
12
# File 'lib/duracloud/response_handler.rb', line 10

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/duracloud/response_handler.rb', line 8

def response
  @response
end

Class Method Details

.call(response) ⇒ Object



4
5
6
# File 'lib/duracloud/response_handler.rb', line 4

def self.call(response)
  new(response).call
end

Instance Method Details

#callObject



14
15
16
17
# File 'lib/duracloud/response_handler.rb', line 14

def call
  handle_error
  log_response
end

#error_messageObject



37
38
39
40
41
42
43
# File 'lib/duracloud/response_handler.rb', line 37

def error_message
  if response.plain_text? && response.has_body?
    response.body
  else
    [ response.status, response.reason ].join(' ')
  end
end

#exceptionObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/duracloud/response_handler.rb', line 45

def exception
  case response.status
  when 400
    BadRequestError
  when 404
    NotFoundError
  when 409
    ConflictError
  else
    if response.status >= 500
      ServerError
    else
      Error
    end
  end
end

#handle_errorObject



31
32
33
34
35
# File 'lib/duracloud/response_handler.rb', line 31

def handle_error
  if response.error?
    raise exception, error_message
  end
end

#log_responseObject



19
20
21
22
23
# File 'lib/duracloud/response_handler.rb', line 19

def log_response
  if loggable_response_body?
    Duracloud.logger.info(response.body)
  end
end

#loggable_response_body?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/duracloud/response_handler.rb', line 25

def loggable_response_body?
  %w(POST PUT DELETE).include?(response.request_method) &&
    response.plain_text? &&
    response.has_body?
end