Module: Noko::Response

Extended by:
Response
Included in:
Response
Defined in:
lib/noko/response.rb

Instance Method Summary collapse

Instance Method Details

#error(response) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/noko/response.rb', line 27

def error(response)
  if response.content_type == 'application/json'
    body = JSON.parse(response.body)

    error_class(response).new(body['message'])
  else
    error_class(response)
  end
end

#error_class(object) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/noko/response.rb', line 37

def error_class(object)
  case object
  when Net::HTTPBadRequest then Noko::ClientError
  when Net::HTTPUnauthorized then Noko::AuthenticationError
  when Net::HTTPNotFound then Noko::NotFound
  when Net::HTTPClientError then Noko::ClientError
  when Net::HTTPServerError then Noko::ServerError
  else
    Noko::Error
  end
end

#parse(response) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/noko/response.rb', line 8

def parse(response)
  if response.is_a?(Net::HTTPNoContent)
    return :no_content
  end

  if response.content_type == 'application/json'
    object = JSON.parse(response.body, symbolize_names: true, object_class: Noko::Record)

    if response['Link']
      object.singleton_class.module_eval { attr_accessor :link }
      object.link = Noko::LinkHeader.parse(response['Link'])
    end

    return object
  end

  response.body
end