Class: App42::Game::GameResponseBuilder

Inherits:
App42ResponseBuilder show all
Defined in:
lib/game/GameResponseBuilder.rb

Overview

GameResponseBuilder class converts the JSON response retrieved from the server to the value object i.e Game

Instance Method Summary collapse

Methods inherited from App42ResponseBuilder

#buildObjectFromJSONTree, #getNames, #getServiceJSONObject, #getTotalRecords, #isResponseSuccess

Instance Method Details

#buildArrayResponse(json) ⇒ Object

Converts the response in JSON format to the list of value objects i.e Game

Parameters:

  • json
    • response in JSON format

Returns:

  • List of Game object filled with json data



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/game/GameResponseBuilder.rb', line 47

def buildArrayResponse(json)
  gamesJSONObj = getServiceJSONObject("games", json)
  gameList = Array.new

  if gamesJSONObj["game"].instance_of?(Array)
    gameJSONArray = gamesJSONObj["game"]
    gameJSONArray.length.times do |i|
      gameJSONObj = gameJSONArray[i]
      game = buildGameObject(gameJSONObj);
      game.isResponseSuccess = isResponseSuccess(json)
      game.strResponse=json
      gameList.push(game)
    end
  else
    gameJSONObject = gamesJSONObj["game"]
    game = buildGameObject(gameJSONObject);
    game.isResponseSuccess = isResponseSuccess(json)
    game.strResponse=json
    gameList.push(game)
  end
  return gameList
end

#buildGameObject(gameJSONObject) ⇒ Object

Converts the Game JSON object to the value object i.e Game

Parameters:

  • gameJSONObject
    • Game data as JSONObject

Returns:

  • Game object filled with json data



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/game/GameResponseBuilder.rb', line 79

def buildGameObject(gameJSONObject)
  game = Game.new()
  scoreList = Array.new
  game.scoreList=(scoreList)
  buildObjectFromJSONTree(game, gameJSONObject);

  if gameJSONObject.key?("scores") && gameJSONObject.fetch("scores").key?("score")
    if gameJSONObject.fetch("scores").fetch("score").instance_of?(Hash)
      scoreJSONObj = gameJSONObject.fetch("scores").fetch("score");
      score = App42::Game::Score.new(game)

      buildObjectFromJSONTree(score, scoreJSONObj);
    else
      scoreJSONArray= gameJSONObject.fetch("scores").fetch("score");
      scoreJSONArray.length.times do |i|
        scoreJSONObj = scoreJSONArray[i]
        score = App42::Game::Score.new(game)

        buildObjectFromJSONTree(score, scoreJSONObj);
      end
    end
  end
  return game

end

#buildResponse(json) ⇒ Object

Converts the response in JSON format to the value object i.e Game

Parameters:

  • json
    • response in JSON format

Returns:

  • Game object filled with json data



27
28
29
30
31
32
33
34
# File 'lib/game/GameResponseBuilder.rb', line 27

def buildResponse(json)
  gamesJSONObj = getServiceJSONObject("games", json)
  gameJSONObj = gamesJSONObj["game"]
  game = buildGameObject(gameJSONObj);
  game.isResponseSuccess = isResponseSuccess(json)
  game.strResponse=json
  return game
end