Module: BunBun::Response

Defined in:
lib/bunbun/response.rb

Class Method Summary collapse

Class Method Details

.error(response) ⇒ Object



19
20
21
# File 'lib/bunbun/response.rb', line 19

def self.error(response)
  error_class(response).new(error_message(response))
end

.error_class(response) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/bunbun/response.rb', line 23

def self.error_class(response)
  case response
  when Net::HTTPClientError then BunBun::ClientError
  when Net::HTTPServerError then BunBun::ServerError
  else
    BunBun::Error
  end
end

.error_message(response) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/bunbun/response.rb', line 32

def self.error_message(response)
  if response.content_type == 'application/json'
    JSON.parse(response.body).fetch('Message')
  else
    "unexpected #{response.code} #{response.content_type} response"
  end
end

.parse(response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bunbun/response.rb', line 5

def self.parse(response)
  unless response.is_a?(Net::HTTPSuccess)
    raise error(response)
  end

  body = response.body

  unless response.content_type == 'application/json'
    return body
  end

  JSON.parse(body)
end