Class: Contentful::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/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.



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

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 invalid_response?
    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.



24
25
26
# File 'lib/contentful/response.rb', line 24

def error_message
  @error_message
end

#objectObject (readonly)

Returns the value of attribute object.



24
25
26
# File 'lib/contentful/response.rb', line 24

def object
  @object
end

#rawObject (readonly)

Returns the value of attribute raw.



24
25
26
# File 'lib/contentful/response.rb', line 24

def raw
  @raw
end

#requestObject (readonly)

Returns the value of attribute request.



24
25
26
# File 'lib/contentful/response.rb', line 24

def request
  @request
end

#statusObject (readonly)

Returns the value of attribute status.



24
25
26
# File 'lib/contentful/response.rb', line 24

def status
  @status
end

Instance Method Details

#load_jsonObject

Returns the JSON body of the response



45
46
47
# File 'lib/contentful/response.rb', line 45

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