Class: App42::Storage::StorageResponseBuilder

Inherits:
App42ResponseBuilder show all
Defined in:
lib/storage/StorageResponseBuilder.rb

Overview

StorageResponseBuilder class converts the JSON response retrieved from the server to the value object i.e Storage

Instance Method Summary collapse

Methods inherited from App42ResponseBuilder

#buildObjectFromJSONTree, #getNames, #getServiceJSONObject, #getTotalRecords, #isResponseSuccess

Instance Method Details

#buildJsonDocument(document, jsonObjDoc) ⇒ Object

Builds the Json Document for the storage w.r.t their docId

Parameters:

  • document
    • document for storage

  • jsonObjDoc
    • jsonDoc object for storage



75
76
77
78
79
80
81
82
83
# File 'lib/storage/StorageResponseBuilder.rb', line 75

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

#buildResponse(json) ⇒ Object

Converts the response in JSON format to the value object i.e Storage

Parameters:

  • json
    • response in JSON format

Returns:

  • Storage object filled with json data



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/storage/StorageResponseBuilder.rb', line 29

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