require 'json/pure' require 'App42ResponseBuilder.rb' require 'appTab/Usage.rb' require 'appTab/BandwidthUnit.rb' require 'appTab/StorageUnit.rb' require 'appTab/TimeUnit.rb' module App42 module AppTab # # # UsageResponseBuilder class converts the JSON response retrieved from the # server to the value object i.e Usage # # class UsageResponseBuilder < App42ResponseBuilder # # Converts the response in JSON format to the value object i.e Usage # # @param json # - response in JSON format # # @return Usage object filled with json data # # @raise Exception # def buildResponse(json) appTabJSONObj = getServiceJSONObject("appTab", json); usageJsonObj = appTabJSONObj.fetch("usages").fetch("usage"); usage = Usage.new usage.strResponse = json usage.isResponseSuccess = isResponseSuccess(json) buildObjectFromJSONTree(usage, usageJsonObj); if usageJsonObj.key?("level") levelList = Array.new usage.levelList=(levelList) if usageJsonObj.fetch("level").instance_of?(Hash) levelJSONObj = usageJsonObj.fetch("level"); level = App42::AppTab::Level.new(usage) buildObjectFromJSONTree(level, levelJSONObj); else levelJSONArray = usageJsonObj.fetch("level"); levelJSONArray.length.times do |i| levelJSONObj = levelJSONArray.fetch(i); level = App42::AppTab::Level.new(usage) buildObjectFromJSONTree(level, levelJSONObj); end end elsif usageJsonObj.key?("oneTime") oneTimeList = Array.new usage.oneTimeList=(oneTimeList) if usageJsonObj.fetch("oneTime").instance_of?(Hash) oneTimeJSONObj = usageJsonObj.fetch("oneTime"); oneTime = App42::AppTab::OneTime.new(usage) buildObjectFromJSONTree(oneTime, oneTimeJSONObj); else oneTimeJSONArray = usageJsonObj.fetch("oneTime"); oneTimeJSONArray.length.times do |i| oneTimeJSONObj = oneTimeJSONArray.fetch(i); oneTime = App42::AppTab::OneTime.new(usage) buildObjectFromJSONTree(oneTime, oneTimeJSONObj); end end elsif usageJsonObj.key?("feature") featureList = Array.new usage.featureList=(featureList) if usageJsonObj.fetch("feature").instance_of?(Hash) featureJSONObj = usageJsonObj.fetch("feature"); feature = App42::AppTab::Feature.new(usage) buildObjectFromJSONTree(feature, featureJSONObj); else featureJSONArray = usageJsonObj.fetch("feature"); featureJSONArray.length.times do |i| featureJSONObj = featureJSONArray.fetch(i); feature = App42::AppTab::Feature.new(usage) buildObjectFromJSONTree(feature, featureJSONObj); end end elsif usageJsonObj.key?("bandwidth") bandwidthList = Array.new usage.bandwidthList=(bandwidthList) if usageJsonObj.fetch("bandwidth").instance_of?(Hash) bandwidthJSONObj = usageJsonObj.fetch("bandwidth"); bandwidth = App42::AppTab::Bandwidth.new(usage) buildObjectFromJSONTree(bandwidth, bandwidthJSONObj); else bandwidthJSONArray = usageJsonObj.fetch("bandwidth"); bandwidthJSONArray.length.times do |i| bandwidthJSONObj = bandwidthJSONArray.fetch(i); bandwidth = App42::AppTab::Bandwidth.new(usage) buildObjectFromJSONTree(bandwidth, bandwidthJSONObj); end end elsif usageJsonObj.key?("storage") storageList = Array.new usage.storageList=(storageList) if usageJsonObj.fetch("storage").instance_of?(Hash) storageJSONObj = usageJsonObj.fetch("storage"); storage = App42::AppTab::Storage.new(usage) buildObjectFromJSONTree(storage, storageJSONObj); else storageJSONArray = usageJsonObj.fetch("storage"); storageJSONArray.length.times do |i| storageJSONObj = storageJSONArray.fetch(i); storage = App42::AppTab::Storage.new(usage) buildObjectFromJSONTree(storage, storageJSONObj); end end elsif usageJsonObj.key?("time") timeList = Array.new usage.timeList=(timeList) if usageJsonObj.fetch("time").instance_of?(Hash) timeJSONObj = usageJsonObj.fetch("time"); time = App42::AppTab::Time.new(usage) buildObjectFromJSONTree(time, timeJSONObj); else timeJSONArray = usageJsonObj.fetch("time"); timeJSONArray.length.times do |i| timeJSONObj = timeJSONArray.fetch(i); time = App42::AppTab::Time.new(usage) buildObjectFromJSONTree(time, timeJSONObj); end end # elsif usageJsonObj.key?("custom") # customList = Array.new # usage.customList=(customList) # if usageJsonObj.fetch("custom").instance_of?(Hash) # customJSONObj = usageJsonObj.fetch("custom"); # custom = App42::Usage::Custom(usage) # buildObjectFromJSONTree(custom, customJSONObj); # else # customJSONArray = usageJsonObj.fetch("custom"); # customJSONArray.length.times do |i| # customJSONObj = customJSONArray.getJSONObject(i); # custom = App42::Usage::Custom(usage) # buildObjectFromJSONTree(custom, customJSONObj); # end # end end return usage; end end end end