# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'json/pure' require 'App42ResponseBuilder.rb' require 'gallery/Album.rb' module App42 module Gallery # # # AlbumResponseBuilder class converts the JSON response retrieved from the # server to the value object i.e Album # # class AlbumResponseBuilder < App42ResponseBuilder # # Converts the response in JSON format to the value object i.e Album # # @param json # - response in JSON format # # @return Album object filled with json data # # def buildResponse(json) albumsJSONObj = getServiceJSONObject("albums", json) albumJSONObj = albumsJSONObj.fetch("album") albumObj = App42::Gallery::Album.new() photoList = Array.new albumObj.photoList = photoList albumObj.strResponse=json albumObj.isResponseSuccess= isResponseSuccess(json) buildObjectFromJSONTree(albumObj, albumJSONObj); if albumJSONObj.key?("photos") == false return albumObj end if albumJSONObj.fetch("photos").key?("photo") == false return albumObj end if albumJSONObj.fetch("photos").fetch("photo").instance_of?(Hash) #Single Entry photoObj = App42::Gallery::Photo.new(albumObj) buildObjectFromJSONTree(photoObj, albumJSONObj.fetch("photos").fetch("photo")); photoObj = setTagList(photoObj, albumJSONObj.fetch("photos").fetch("photo")); else # Multiple Entry photoJSONArray = albumJSONObj.fetch("photos").fetch("photo"); photoJSONArray.length.times do |i| photoJSONObj = photoJSONArray[i] photoObj = App42::Gallery::Photo.new(albumObj) buildObjectFromJSONTree(photoObj, photoJSONObj); photoObj = setTagList(photoObj, photoJSONObj); end end return albumObj end # # Converts the Album JSON object to the value object i.e Album # # @param albumsJSONObj # - Album data as JSONObject # # @return Album object filled with json data # # def buildAlbumObject(albumsJSONObj) albumObj = App42::Gallery::Album.new buildObjectFromJSONTree(albumObj, albumsJSONObj); photoList = Array.new albumObj.photoList = photoList if albumsJSONObj.key?("photos") && albumsJSONObj.fetch("photos").key?("photo") if albumsJSONObj.fetch("photos").fetch("photo").instance_of?(Hash) photoJsonObj = albumsJSONObj.fetch("photos").fetch("photo"); # Single Entry photoObj = App42::Gallery::Photo.new(albumObj) buildObjectFromJSONTree(photoObj, albumsJSONObj.fetch("photos").fetch("photo")); photoObj = setTagList(photoObj, photoJsonObj); else # Multiple Entry photoJSONArray = albumsJSONObj.fetch("photos").fetch("photo") photoJSONArray.length.times do |j| photoJSONObj = photoJSONArray[j] photoObj = App42::Gallery::Photo.new(albumObj) buildObjectFromJSONTree(photoObj, photoJSONObj); photoObj = setTagList(photoObj, photoJSONObj); end end end return albumObj; end # # Converts the response in JSON format to the list of value objects i.e # Album # # @param json # - response in JSON format # #@return List of Album object filled with json data # def buildArrayResponse(json) albumsJSONObj = getServiceJSONObject("albums", json) albumList = Array.new if albumsJSONObj.fetch("album").instance_of?(Array) albumJSONArray = albumsJSONObj.fetch("album"); albumJSONArray.length.times do |i| albumJSONObj = albumJSONArray[i] album = buildAlbumObject(albumJSONObj); album.strResponse=json album.isResponseSuccess= (isResponseSuccess(json)) albumList.push(album) end else albumJSONObject = albumsJSONObj.fetch("album"); album = buildAlbumObject(albumJSONObject); album.strResponse=json album.isResponseSuccess= (isResponseSuccess(json)) albumList.push(album) end return albumList end # # set tags to the list # # @param photoObj # @param photoJsonObj # # @return photo object # # @raise Exception # def setTagList(photoObj, photoJsonObj) if photoJsonObj.key?("tags") tagList = Array.new if photoJsonObj.fetch("tags").instance_of?(Array) tagArr = photoJsonObj.fetch("tags"); tagArr.length.times do |i| tagList.push(tagArr.fetch(i)); end else tagList.push(photoJsonObj.fetch("tags")); end photoObj.tagList = (tagList); end return photoObj; end end end end