Sha256: 44d796577b1f1eddf1acd98035085c84b8b9240a2750dffcec1d499798470fd8

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 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 = CSV.parse(@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

1 entries across 1 versions & 1 rubygems

Version Path
qualtrics-0.5.9 lib/qualtrics/response.rb