Class: Projects::Parser::UserParser

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/projects/parser/UserParser.rb

Overview

  • Parse the JSON response into respective objects.

Instance Method Summary collapse

Instance Method Details

#getUsers(response) ⇒ Object

  • Parse the JSON response and make it into List of User object.

Parameters

  • response
    • This JSON response contains the details of users.

Returns

  • List of User object.



23
24
25
26
27
28
29
30
31
# File 'lib/projects/parser/UserParser.rb', line 23

def getUsers(response)
  users_all_json = JSON.parse response
  users_all_array = users_all_json["users"]
  users_class_array = Array.new
  for i in 0...users_all_array.length
    users_class_array.push(jsonToUser(users_all_array[i]))
  end
  return users_class_array
end

#jsonToUser(jsonObject) ⇒ Object

  • Parse the JSONObject into User object.

Parameters

  • response
    • JSONObject contains the details of a user.

Returns

  • User object.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/projects/parser/UserParser.rb', line 43

def jsonToUser(jsonObject)
  user = User.new

  if jsonObject.has_key?("id")
    user.setId(jsonObject["id"])
  end
  if jsonObject.has_key?("name")
    user.setName(jsonObject["name"])
  end
  if jsonObject.has_key?("email")
    user.setEmail(jsonObject["email"])
  end
  if jsonObject.has_key?("role")
    user.setRole(jsonObject["role"])
  end
  
  return user
end