class App42::Gallery::AlbumResponseBuilder

AlbumResponseBuilder class converts the JSON response retrieved from the server to the value object i.e Album

Public Instance Methods

buildAlbumObject(albumsJSONObj) click to toggle source

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

# File lib/gallery/AlbumResponseBuilder.rb, line 75
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
buildArrayResponse(json) click to toggle source
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

# File lib/gallery/AlbumResponseBuilder.rb, line 116
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
buildResponse(json) click to toggle source

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

# File lib/gallery/AlbumResponseBuilder.rb, line 27
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
setTagList(photoObj, photoJsonObj) click to toggle source

set tags to the list

@param photoObj @param photoJsonObj

@return photo object

@raise Exception

# File lib/gallery/AlbumResponseBuilder.rb, line 150
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