class Contentful::Management::Response
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
Attributes
error_message[R]
object[R]
raw[R]
request[R]
status[R]
Public Class Methods
new(raw, request = nil)
click to toggle source
# File lib/contentful/management/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 resource_error? parse_contentful_error elsif service_unavailable_response? service_unavailable_error else parse_http_error end end
Public Instance Methods
load_json()
click to toggle source
Returns the JSON body of the response
# File lib/contentful/management/response.rb, line 45 def load_json MultiJson.load(unzip_response(raw)) end
Private Instance Methods
error_object?()
click to toggle source
# File lib/contentful/management/response.rb, line 51 def error_object? object['sys']['type'] == 'Error' end
no_content_response?()
click to toggle source
# File lib/contentful/management/response.rb, line 84 def no_content_response? raw.to_s == '' && raw.status == 204 end
parse_contentful_error()
click to toggle source
# File lib/contentful/management/response.rb, line 55 def parse_contentful_error @object = load_json @error_message = object['message'] if error_object? parse_http_error end
parse_http_error()
click to toggle source
# File lib/contentful/management/response.rb, line 75 def parse_http_error @status = :error @object = Error[raw.status].new(self) end
parse_json!()
click to toggle source
# File lib/contentful/management/response.rb, line 88 def parse_json! @object = load_json rescue MultiJson::LoadError => error @error_message = error.message UnparsableJson.new(self) end
resource_error?()
click to toggle source
# File lib/contentful/management/response.rb, line 80 def resource_error? [400, 404, 422, 429].include?(raw.status) end
unzip_response(response)
click to toggle source
# File lib/contentful/management/response.rb, line 95 def unzip_response(response) parsed_response = response.to_s if response.headers['Content-Encoding'].eql?('gzip') sio = StringIO.new(parsed_response) gz = Zlib::GzipReader.new(sio) gz.read else parsed_response end end
valid_http_response?()
click to toggle source
# File lib/contentful/management/response.rb, line 61 def valid_http_response? [200, 201].include?(raw.status) end