# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'json/pure' require 'App42_Ruby_API/App42ResponseBuilder' require 'shopping/Cart' module App42 module Shopping # # # CartResponseBuilder class converts the JSON response retrieved from the # server to the value object i.e Cart # # class CartResponseBuilder < App42ResponseBuilder # # Converts the response in JSON format to the value object i.e Cart # # @param json # - response in JSON format # # @return Cart object filled with json data # def buildResponse(json) cartsJSONObj = getServiceJSONObject("carts", json) cartJSONObj = cartsJSONObj.fetch("cart") cart = buildCartObject(cartJSONObj); cart.strResponse=json cart.isResponseSuccess = isResponseSuccess(json) return cart end # # Converts the Cart JSON object to the value object i.e Cart # # @param cartJSONObj # - cart data as JSONObject # # @return Cart object filled with json data # # def buildCartObject(cartJSONObj) cart = Cart.new(); itemList = Array.new() cart.itemList = itemList buildObjectFromJSONTree(cart, cartJSONObj); if cartJSONObj.key?("items") && cartJSONObj.fetch("items").key?("item") if cartJSONObj.fetch("items").fetch("item").instance_of?(Hash) #Single Item itemJSONObj = cartJSONObj.fetch("items").fetch("item") item = App42::Shopping::Item.new(cart) buildObjectFromJSONTree(item, itemJSONObj); else # Multiple Items itemJSONArray = cartJSONObj.fetch("items").fetch("item"); itemJSONArray.length.times do |i| itemJSONObj = itemJSONArray[i] item = App42::Shopping::Item.new(cart) buildObjectFromJSONTree(item, itemJSONObj); end end end if cartJSONObj.key?("payments") && cartJSONObj.fetch("payments").key?("payment") # Fetch Payment paymentJSONObj = cartJSONObj.fetch("payments").fetch("payment"); puts paymentJSONObj payment = App42::Shopping::Payment.new(cart) buildObjectFromJSONTree(payment, paymentJSONObj); end return cart; end # # Converts the response in JSON format to the list of value objects i.e # Cart # # @param json # - response in JSON format # # @return List of Cart object filled with json data # # def buildArrayResponse(json) cartsJSONObj = getServiceJSONObject("carts", json); cartList = Array.new if cartsJSONObj.fetch("cart").instance_of?(Array) cartJSONArray = cartsJSONObj.fetch("cart") cartJSONArray.length.times do |i| cartJSONObj = cartJSONArray[i] cart = buildCartObject(cartJSONObj); cart.strResponse=json cart.isResponseSuccess = isResponseSuccess(json) cartList.push(cart) end else cartJSONObj = cartsJSONObj.fetch("cart"); cart = buildCartObject(cartJSONObj); cart.strResponse=json cart.isResponseSuccess = isResponseSuccess(json) cartList.push(cart) end return cartList end end end end