Module: Uffizzi::ResponseHelper

Defined in:
lib/uffizzi/response_helper.rb

Class Method Summary collapse

Class Method Details

.created?(response) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/uffizzi/response_helper.rb', line 6

def created?(response)
  response[:code] == Net::HTTPCreated
end

.forbidden?(response) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/uffizzi/response_helper.rb', line 18

def forbidden?(response)
  response[:code] == Net::HTTPForbidden
end

.get_response_errors(response) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/uffizzi/response_helper.rb', line 47

def get_response_errors(response)
  return response if response.is_a?(Array)
  return [response.to_s] unless response.is_a?(Hash)

  body = response.fetch(:body, response.to_s)
  return [body.to_s] unless body.is_a?(Hash)

  errors = body.fetch(:errors, body.to_s)
  return [errors.to_s] unless [Hash, Array].include?(errors.class)

  errors
end

.handle_failed_response(response, addtional_errors = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/uffizzi/response_helper.rb', line 30

def handle_failed_response(response, addtional_errors = [])
  errors = get_response_errors(response)
  common_msg = errors_to_string(errors)
  addtional_msg = errors_to_string(addtional_errors)
  message = [common_msg, addtional_msg]
    .reject { |m| m == '' }
    .join("\n")
    .concat("\n")

  raise Uffizzi::ServerResponseError.new(message)
end

.handle_invalid_compose_response(response) ⇒ Object



42
43
44
45
# File 'lib/uffizzi/response_helper.rb', line 42

def handle_invalid_compose_response(response)
  message = errors_to_string(response[:body][:compose_file][:payload][:errors])
  raise Uffizzi::ServerResponseError.new(message)
end

.no_content?(response) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/uffizzi/response_helper.rb', line 22

def no_content?(response)
  response[:code] == Net::HTTPNoContent
end

.not_found?(response) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/uffizzi/response_helper.rb', line 14

def not_found?(response)
  response[:code] == Net::HTTPNotFound
end

.ok?(response) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/uffizzi/response_helper.rb', line 26

def ok?(response)
  response[:code] == Net::HTTPOK
end

.unprocessable_entity?(response) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/uffizzi/response_helper.rb', line 10

def unprocessable_entity?(response)
  response[:code] == Net::HTTPUnprocessableEntity
end