Sha256: 40768fc404ae1f568007dd4e9540368519bce2ceba09adbcccf939ba6d85acb8

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

require 'rubygems'
require 'json'

module RHCP

  class Response

    class Status
      OK="ok"
      ERROR="error"
    end

    # TODO these should be attr_reader's, but then we've got to solve json_create() differently
    attr_accessor :status
    attr_accessor :error_text
    attr_accessor :error_detail
    attr_accessor :data
    
    # textual description of the result (optional)
    attr_accessor :result_text

    def initialize
      @status = Status::OK
      @error_text = ""
      @error_detail = ""
      @result_text = "";
    end
    
    def mark_as_error(text, detail="")
      @status = Status::ERROR
      @error_text = text
      @error_detail = detail
    end

    def set_payload(data)
      @data = data
    end
    
    def self.reconstruct_from_json(json_data)
      object = JSON.parse(json_data)
      instance = self.new()
      instance.status = object["status"]
      instance.error_text = object["error_text"]
      instance.error_detail = object["error_detail"]
      instance.set_payload(object["data"])
      instance.result_text = object['result_text']
      instance
    end
    
    def to_json(*args)
      {
        'status' => @status,
        'error_text' => @error_text,
        'error_detail' => @error_detail,
        'data' => @data,   # TODO what about JSONinification of data? (probably data should be JSON-ish data only, i.e. no special objects)
        'result_text' => @result_text
      }.to_json(*args)
    end    

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rhcp-0.1.4 lib/rhcp/response.rb
rhcp-0.1.5 lib/rhcp/response.rb
rhcp-0.1.6 lib/rhcp/response.rb
rhcp-0.1.7 lib/rhcp/response.rb
rhcp-0.1.8 lib/rhcp/response.rb
rhcp-0.1.9 lib/rhcp/response.rb
rhcp-0.1.2 lib/rhcp/response.rb