Class: Contentful::Management::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/management/response.rb

Overview

An object representing an answer by the contentful service. It is later used to build a Resource, which is done by the ResourceBuilder.

The Response parses the http response (as returned by the underlying http library) to a JSON object. Responses can be asked the following methods:

  • #raw (raw HTTP response by the HTTP library)

  • #object (the parsed JSON object)

  • #request (the request the response is refering to)

It also sets a #status which can be one of:

  • :ok (seems to be a valid resource object)

  • :contentful_error (valid error object)

  • :not_contentful (valid json, but missing the contentful’s sys property)

  • :unparsable_json (invalid json)

Error Repsonses also contain a:

  • :error_message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, request = nil) ⇒ Response

Returns a new instance of Response.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/contentful/management/response.rb', line 28

def initialize(raw, request = nil)
  @raw = raw
  @request = request
  @status = :ok

  if valid_http_response?
    parse_json!
  elsif no_content_response?
    @status = :no_content
  elsif resource_error?
    parse_contentful_error
  elsif service_unavailable_response?
    service_unavailable_error
  else
    parse_http_error
  end
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



26
27
28
# File 'lib/contentful/management/response.rb', line 26

def error_message
  @error_message
end

#objectObject (readonly)

Returns the value of attribute object.



26
27
28
# File 'lib/contentful/management/response.rb', line 26

def object
  @object
end

#rawObject (readonly)

Returns the value of attribute raw.



26
27
28
# File 'lib/contentful/management/response.rb', line 26

def raw
  @raw
end

#requestObject (readonly)

Returns the value of attribute request.



26
27
28
# File 'lib/contentful/management/response.rb', line 26

def request
  @request
end

#statusObject (readonly)

Returns the value of attribute status.



26
27
28
# File 'lib/contentful/management/response.rb', line 26

def status
  @status
end

Instance Method Details

#load_jsonObject

Returns the JSON body of the response



47
48
49
# File 'lib/contentful/management/response.rb', line 47

def load_json
  MultiJson.load(unzip_response(raw))
end