# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'json/pure' require 'App42_Ruby_API/App42ResponseBuilder' require 'geo/Geo' require 'geo/GeoPoint' module App42 module Geo # # # GeoResponseBuilder class converts the JSON response retrieved from the server # to the value object i.e Geo # # class GeoResponseBuilder < App42ResponseBuilder # # Converts the response in JSON format to the value object i.e Geo # # @param json # - response in JSON format # # @return Geo object filled with json data # # def buildResponse(json) puts "testing #{json}" geoObj = Geo.new() pointList = Array.new geoObj.pointList=(pointList) geoObj.strResponse=json jsonObj = JSON.parse(json) jsonObjApp42 = jsonObj.fetch("app42") jsonObjResponse = jsonObjApp42.fetch("response") geoObj.isResponseSuccess = jsonObjResponse.fetch("success") jsonObjGeoStorage = jsonObjResponse.fetch("geo").fetch("storage") buildObjectFromJSONTree(geoObj, jsonObjGeoStorage); if jsonObjGeoStorage.key?("points") == false return geoObj end buildInternalObj(geoObj, jsonObjGeoStorage); return geoObj; end # # Converts the response in JSON format to the list of value objects i.e Geo # # @param json # - response in JSON format # # @return List of Geo Points object filled with json data # # def buildArrayResponse(json) geoObjList = Array.new jsonObj = JSON.parse(json) jsonObjApp42 = jsonObj.fetch("app42") jsonObjResponse = jsonObjApp42.fetch("response") jsonObjGeoStorage = jsonObjResponse.fetch("geo") if jsonObjGeoStorage.fetch("storage").instance_of?(Hash) #Single Item jsonObjGeoStorage = jsonObjGeoStorage.fetch("storage"); geoObj = App42::Geo::Geo.new() pointList = Array.new geoObj.pointList=pointList geoObj.strResponse=json geoObj.isResponseSuccess = jsonObjResponse.fetch("success") buildObjectFromJSONTree(geoObj, jsonObjGeoStorage); geoObjList.push(geoObj) if jsonObjGeoStorage.key?("points") buildInternalObj(geoObj,jsonObjGeoStorage); end else jsonStorageArray = jsonObjGeoStorage.fetch("storage"); jsonStorageArray.length.times do |i| jsonObjStorage = jsonStorageArray[i] geoObj = App42::Geo::Geo.new() pointList = Array.new geoObj.pointList=pointList geoObj.strResponse=json geoObj.isResponseSuccess = jsonObjResponse.fetch("success") buildObjectFromJSONTree(geoObj, jsonObjStorage); geoObjList.push(geoObj) if jsonObjStorage.key?("points") buildInternalObj(geoObj,jsonObjStorage); end end end return geoObjList end # # Converts the Geo JSON object to the value object i.e Geo # # @param jsonObjGeoStorage # - geo data as JSONObject # @param geoObj # - new geo object # # @return Geo object filled with json data # # def buildInternalObj(geoObj, jsonObjGeoStorage) jsonGeoPoints = jsonObjGeoStorage.fetch("points"); if jsonGeoPoints.key?("point") == false return geoObj end if jsonGeoPoints.fetch("point").instance_of?(Hash) #Only One attribute is there jsonObjPoint = jsonGeoPoints.fetch("point") pointsItem = App42::Geo::Point.new(geoObj) buildObjectFromJSONTree(pointsItem, jsonObjPoint); else #There is an Array of attribute jsonObjPointsArray = jsonGeoPoints.fetch("point"); jsonObjPointsArray.length.times do |i| jsonObjPoint = jsonObjPointsArray[i] pointsItem = App42::Geo::Point.new(geoObj) buildObjectFromJSONTree(pointsItem, jsonObjPoint); end end return geoObj; end end end end