Class: App42::User::UserResponseBuilder

Inherits:
App42ResponseBuilder show all
Defined in:
lib/user/UserResponseBuilder.rb

Overview

UserResponseBuilder class converts the JSON response retrieved from the server to the value object i.e User

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 User

Parameters:

  • json
    • response in JSON format

Returns:

  • List of User object filled with json data



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

def buildArrayResponse(json)
  usersJSONObj = getServiceJSONObject("users", json);
  userList = Array.new

  if usersJSONObj["user"].instance_of?(Array)
    userJSONArray = usersJSONObj["user"]
    userJSONArray.length.times do |i|
      userJSONObject = userJSONArray[i]
      user = buildUserObject(userJSONObject);
      user.strResponse=json
      user.isResponseSuccess = isResponseSuccess(json)
      userList.push(user)
    end
  else
    userJSONObject = usersJSONObj["user"]
    user = buildUserObject(userJSONObject);
    user.strResponse=json
    user.isResponseSuccess = isResponseSuccess(json)
    userList.push(user)
  end
  return userList
end

#buildResponse(json) ⇒ Object

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

Parameters:

  • json
    • response in JSON format

Returns:

  • User object filled with json data



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

def buildResponse(json)
  usersJSONObj = getServiceJSONObject("users", json)
  userJSOnObj = usersJSONObj["user"]
  user = buildUserObject(userJSOnObj);
  user.strResponse=json
  user.isResponseSuccess = isResponseSuccess(json)
  return user
end

#buildUserObject(userJSONObj) ⇒ Object

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

Parameters:

  • userJSONObj
    • user data as JSONObject

Returns:

  • User object filled with json data



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

def buildUserObject(userJSONObj)
  user = User.new
  buildObjectFromJSONTree(user,userJSONObj)

  if userJSONObj.key?('profile')
    profileJSONObj = userJSONObj["profile"]
    profile = App42::User::Profile.new(user)
    buildObjectFromJSONTree(profile, profileJSONObj);
  end

  if userJSONObj.key?("role")
    roleList = Array.new
    if userJSONObj.fetch("role").instance_of?(Array)
      roleArr = userJSONObj.fetch("role");
      roleArr.length.times do |i|
        roleList.push(roleArr.fetch(i))
      end
    else
      roleList.push(userJSONObj.fetch("role"));
    end
    user.roleList = roleList
  end
  return user;
end