Sha256: 2fde95ff8bc7be3d564dc4c398f305599a299317cc6c2c7b1708fc104f1f1968

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'json'
require 'csv'

module Qualtrics
	class Response

    def initialize(raw_response)
      @raw_response = raw_response
      if status != 200
        raise Qualtrics::ServerErrorEncountered, error_message
      end
    end

    def success?
      body['Meta'].nil? ? false : body['Meta']['Status'] == 'Success'
    end

    def result
      if content_type == 'application/vnd.msexcel'
        body.nil? ? {} : body
      else
        body['Result'].nil? ? {} : body['Result']
      end
    end

    def status
      @raw_response.status
    end

    protected

    def body
      if @body.nil?
        if @raw_response.body == ''
          @body = {}
        elsif content_type == 'application/json'
          @body = JSON.parse(@raw_response.body)
        elsif content_type == 'application/vnd.msexcel'
          @body = @raw_response.body
        else
          raise Qualtrics::UnexpectedContentType, content_type
        end
      end
      @body
    end

    def content_type
      if @content_type.nil?
        header = @raw_response.headers['Content-Type']
        if header.nil?
          @content_type = {}
        else
          @content_type = header
        end
      end
      @content_type
    end

    private
    def error_message
      body['Meta'].nil? ? 'No error message' : body['Meta']['ErrorMessage']
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qualtrics-0.6.1 lib/qualtrics/response.rb
qualtrics-0.6.0 lib/qualtrics/response.rb
qualtrics-0.5.9b lib/qualtrics/response.rb