Sha256: 2ab5496f788cf68d87a249d409d5afd587835185672ed967d5052d87fe58625c

Contents?: true

Size: 1.69 KB

Versions: 6

Compression:

Stored size: 1.69 KB

Contents

require_relative 'error'
require 'multi_json'

module Contentful
  # 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
  class Response
    attr_reader :raw, :object, :status, :error_message, :request

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

      if parse_json!
        parse_contentful_error!
      end
    end


    private

    def parse_json!
      @object = MultiJson.load(raw.to_s)
      true
    rescue MultiJson::LoadError => e
      @status = :unparsable_json
      @error_message = e.message
      @object = e
      false
    end

    def parse_contentful_error!
      if @object && @object["sys"]
        if @object["sys"]["type"] == 'Error'
          @status = :contentful_error
          @error_message = object['message']
          true
        else
          false
        end
      else
        @status = :not_contentful
        @error_message = "No contentful system properties found in object"
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
contentful-0.3.0 lib/contentful/response.rb
contentful-0.2.0 lib/contentful/response.rb
contentful-0.1.3 lib/contentful/response.rb
contentful-0.1.2 lib/contentful/response.rb
contentful-0.1.1 lib/contentful/response.rb
contentful-0.1.0 lib/contentful/response.rb