# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'json/pure' require 'App42ResponseBuilder.rb' require 'shopping/Catalogue.rb' module App42 module Shopping # # # CatalogueResponseBuilder class converts the JSON response retrieved from the # server to the value object i.e Catalogue # # class CatalogueResponseBuilder < App42ResponseBuilder # # Converts the response in JSON format to the value object i.e Catalogue # # @param json # - response in JSON format # # @return Catalogue object filled with json data # # def buildResponse(json) puts "testing #{json}" cataloguesJSONObject = getServiceJSONObject("catalogues", json); catalogueJSONObject = cataloguesJSONObject.fetch("catalogue") catalogue = buildCatalogueObject(catalogueJSONObject); catalogue.strResponse=json catalogue.isResponseSuccess = isResponseSuccess(json) return catalogue end # # Converts the response in JSON format to the list of value objects i.e # Catalogue # # @param response # - response in JSON format # # @return List of Catalogue object filled with json data # # def buildArrayResponse(json) cataloguesJSONObject = getServiceJSONObject("catalogues", json); catalogueList = Array.new if cataloguesJSONObject["catalogue"].instance_of?(Array) catalogueJSONArray = cataloguesJSONObject.fetch("catalogue"); catalogueJSONArray.length.times do |i| catalogueJSONObj = catalogueJSONArray[i] # catalogue = Catalogue.new catalogue = buildCatalogueObject(catalogueJSONObj); catalogue.strResponse=json catalogue.isResponseSuccess = isResponseSuccess(json) catalogueList.push(catalogue) end else catalogueJSONObj = cataloguesJSONObject["catalogue"] catalogue = buildCatalogueObject(catalogueJSONObj); catalogue.strResponse=json catalogue.isResponseSuccess = isResponseSuccess(json) catalogueList.push(catalogue) end return catalogueList end # # Converts the User JSON object to the value object i.e Catalogue # # @param catalogueJSONObj # - Catalogue data as JSONObject # # @return Catalogue object filled with json data # def buildCatalogueObject(catalogueJSONObj) catalogue = Catalogue.new(); categoryList = Array.new catalogue.categoryList = categoryList buildObjectFromJSONTree(catalogue, catalogueJSONObj); if catalogueJSONObj.key?("categories") && catalogueJSONObj.fetch("categories").key?("category") #Fetch Category if catalogueJSONObj.fetch("categories").fetch("category").instance_of?(Hash) #Single Category categoryJSONObj = catalogueJSONObj.fetch("categories").fetch("category"); catalogueCategory = App42::Shopping::Category.new(catalogue) itemList = Array.new catalogueCategory.itemList=(itemList) buildObjectFromJSONTree(catalogueCategory, categoryJSONObj); #Check for Items Now if(categoryJSONObj.key?("items") && categoryJSONObj.fetch("items").key?("item")) if categoryJSONObj.fetch("items").fetch("item").instance_of?(Hash) #Single Item itemJSONObj = categoryJSONObj.fetch("items").fetch("item"); item = App42::Shopping::Item.new(catalogueCategory) buildObjectFromJSONTree(item, itemJSONObj); else # Multiple Items categoryJSONArray = categoryJSONObj.fetch("items").fetch("item"); categoryJSONArray.length.times do |j| itemJSONObj = categoryJSONArray[j] item = App42::Shopping::Item.new(catalogueCategory) buildObjectFromJSONTree(item, itemJSONObj); end end end else #Multiple Categories itemJSONArray = catalogueJSONObj.fetch("categories").fetch("category"); itemJSONArray.length.times do |j| categoryJSONObj = itemJSONArray(j) catalogueCategory = App42::Shopping::Category.new(catalogue) buildObjectFromJSONTree(catalogueCategory, categoryJSONObj); # Check for Items Now if categoryJSONObj.key?("items") && categoryJSONObj.fetch("items").key?("item") if categoryJSONObj.fetch("items").fetch("item").instance_of?(Hash) #Single Item itemJSONObj = categoryJSONObj.fetch("items").fetch("item"); item = App42::Shopping::Item.new(catalogueCategory) itemList = Array.new catalogueCategory.itemList=(itemList) buildObjectFromJSONTree(item, itemJSONObj); else #Multiple Items categoryJSONArray = categoryJSONObj.fetch("items").fetch("item"); categoryJSONArray.length.times do |j| itemJSONObj = categoryJSONArray(j) item = App42::Shopping::Item.new(catalogueCategory) buildObjectFromJSONTree(item, itemJSONObj); end end end end end end return catalogue; end end end end