# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'json/pure' require 'App42_Ruby_API/App42ResponseBuilder' require 'storage/Storage' require 'storage/Query' require 'storage/OrderByType' module App42 module Storage # # # StorageResponseBuilder class converts the JSON response retrieved from the # server to the value object i.e Storage # # class StorageResponseBuilder < App42ResponseBuilder # # Converts the response in JSON format to the value object i.e Storage # # @param json # - response in JSON format # # @return Storage object filled with json data # # def buildResponse(json) storageObj = Storage.new jsonDocList = Array.new storageObj.jsonDocList = (jsonDocList) storageObj.strResponse = (json) jsonObj = JSON.parse(json) jsonObjApp42 = jsonObj["app42"] jsonObjResponse = jsonObjApp42["response"] storageObj.isResponseSuccess=(jsonObjResponse.fetch("success")) jsonObjStorage = jsonObjResponse.fetch("storage"); buildObjectFromJSONTree(storageObj, jsonObjStorage); if jsonObjStorage.key?("jsonDoc") == false return storageObj; end if jsonObjStorage.fetch("jsonDoc").instance_of?(Hash) # Only One attribute is there jsonObjDoc = jsonObjStorage.fetch("jsonDoc"); document = App42::Storage::JSONDocument.new(storageObj) buildJsonDocument(document, jsonObjDoc); else # There is an Array of attribute jsonObjDocArray = jsonObjStorage.fetch("jsonDoc") jsonObjDocArray.length.times do |i| # Get Individual Attribute Node and set it into Object jsonObjDoc = jsonObjDocArray[i] document = App42::Storage::JSONDocument.new(storageObj) buildJsonDocument(document, jsonObjDoc); end end return storageObj end # # Builds the Json Document for the storage w.r.t their docId # # @param document # - document for storage # @param jsonObjDoc # - jsonDoc object for storage # def buildJsonDocument(document,jsonObjDoc) if jsonObjDoc.fetch("_id").instance_of?(Hash) idObj = jsonObjDoc.fetch("_id"); oIdObj = idObj.fetch("$oid") document.docId = oIdObj jsonObjDoc.delete("_id") document.jsonDoc = jsonObjDoc.to_s end end end end end