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 # TODO this should be called 'cookies' attr_accessor :context # textual description of the result (optional) attr_accessor :result_text attr_accessor :created_at def initialize @status = Status::OK @error_text = "" @error_detail = "" @result_text = "" @context = nil @created_at = Time.now().to_i 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 set_context(new_context) @context = new_context 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.context = object['cookies'] instance.created_at = object['created_at'] 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, 'cookies' => @context, 'created_at' => @created_at }.to_json(*args) end end end