Module: Dirigible::Utils

Defined in:
lib/dirigible/utils.rb

Class Method Summary collapse

Class Method Details

.handle_api_error(response) ⇒ Object

Raises:

  • (klass)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dirigible/utils.rb', line 4

def self.handle_api_error(response)
  message = parse_message(response)

  klass = case response.status
    when 400 then BadRequest
    when 401 then Unauthorized
    when 404 then NotFound
    when 405 then MethodNotAllowed
    when 406 then NotAcceptable
    when 503 then ServiceUnavailable
    else Error
  end

  raise klass.new(message)
end

.parse_json(json) ⇒ Object



20
21
22
# File 'lib/dirigible/utils.rb', line 20

def self.parse_json(json)
  MultiJson.load(json, symbolize_keys: true)
end

.parse_message(response) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/dirigible/utils.rb', line 24

def self.parse_message(response)
  begin
    parse_json(response.body)
  rescue
    response.body
  end
end