class Contentful::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:

It also sets a status which can be one of:

Error Repsonses also contain a:

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/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

Public Instance Methods

load_json() click to toggle source

Returns the JSON body of the response

# File lib/contentful/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/response.rb, line 51
def error_object?
  object['sys']['type'] == 'Error'
end
invalid_response?() click to toggle source
# File lib/contentful/response.rb, line 80
def invalid_response?
  [400, 404].include?(raw.status)
end
no_content_response?() click to toggle source
# File lib/contentful/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/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/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/response.rb, line 88
def parse_json!
  @object = load_json
rescue MultiJson::LoadError => error
  @error_message = error.message
  @status = :error
  UnparsableJson.new(self)
end
service_unavailable_error() click to toggle source
# File lib/contentful/response.rb, line 69
def service_unavailable_error
  @status = :error
  @error_message = '503 - Service Unavailable'
  @object = Error[@raw.status].new(self)
end
service_unavailable_response?() click to toggle source
# File lib/contentful/response.rb, line 65
def service_unavailable_response?
  @raw.status == 503
end
unzip_response(response) click to toggle source
# File lib/contentful/response.rb, line 96
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/response.rb, line 61
def valid_http_response?
  [200, 201].include?(raw.status)
end