Sha256: 691e43da052f13f3f2afdf447cc6424b963a9d854ba6b78a73e85c73e966b63d

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

module SparkApi
  # API Response interface
  module Response
    ATTRIBUTES = [:code, :message, :results, :success, :pagination, :details, :d, :errors, :sparkql_errors]
    attr_accessor *ATTRIBUTES
    def success?
      @success
    end
  end
  
  # Nice and handy class wrapper for the api response hash
  class ApiResponse < ::Array
    include SparkApi::Response
    def initialize(d)
      begin
        self.d = d["D"]
        if self.d.nil? || self.d.empty?
          raise InvalidResponse, "The server response could not be understood"
        end
        self.message    = self.d["Message"]
        self.code       = self.d["Code"]
        self.results    = Array(self.d["Results"])
        self.success    = self.d["Success"]
        self.pagination = self.d["Pagination"]
        self.details    = self.d["Details"] || []
        self.errors     = self.d["Errors"]
        self.sparkql_errors = self.d['SparkQLErrors']
        super(results)
      rescue Exception => e
        SparkApi.logger.error "Unable to understand the response! #{d}"
        raise
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spark_api-1.4.24 lib/spark_api/response.rb
spark_api-1.4.23 lib/spark_api/response.rb
spark_api-1.4.22 lib/spark_api/response.rb
spark_api-1.4.21 lib/spark_api/response.rb
spark_api-1.4.20 lib/spark_api/response.rb
spark_api-1.4.19 lib/spark_api/response.rb