Class: Projects::Parser::ForumParser

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

Overview

  • Parse the JSON response into respective objects.

Instance Method Summary collapse

Instance Method Details

#getCategories(response) ⇒ Object

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

Parameters

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

Returns

  • List of Category object.



131
132
133
134
135
136
137
138
139
# File 'lib/projects/parser/ForumParser.rb', line 131

def getCategories(response)
  categories_all_json = JSON.parse response
  categories_all_array = categories_all_json["categories"]
  categories_class_array = Array.new
  for i in 0...categories_all_array.length
    categories_class_array.push(jsonToCategory(categories_all_array[i]))
  end
  return categories_class_array
end

#getCategory(response) ⇒ Object

  • Parse the JSON response and make it into Category object.

Parameters

  • response
    • This JSON response contains the details of a category.

Returns

  • Category object.



151
152
153
154
155
# File 'lib/projects/parser/ForumParser.rb', line 151

def getCategory(response)
  category_json = JSON.parse response
  category_array = category_json["categories"]
  return jsonToCategory(category_array[0])
end

#getComment(response) ⇒ Object

  • Parse the JSON response and make it into Comment object.

Parameters

  • response
    • This JSON response contains the details of a comment.

Returns

  • Comment object.



208
209
210
211
212
# File 'lib/projects/parser/ForumParser.rb', line 208

def getComment(response)
  comment_json = JSON.parse response
  comment_array = comment_json["comments"]
  return jsonToComment(comment_array[0])
end

#getComments(response) ⇒ Object

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

Parameters

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

Returns

  • List of Comment object.



188
189
190
191
192
193
194
195
196
# File 'lib/projects/parser/ForumParser.rb', line 188

def getComments(response)
  comments_all_json = JSON.parse response
  comments_all_array = comments_all_json["comments"]
  comments_class_array = Array.new
  for i in 0...comments_all_array.length
    comments_class_array.push(jsonToComment(comments_all_array[i]))
  end
  return comments_class_array
end

#getForum(response) ⇒ Object

  • Parse the JSON response and make it into Forum object.

Parameters

  • response
    • This JSON response contains the details of a forum.

Returns

  • Forum object.



44
45
46
47
48
# File 'lib/projects/parser/ForumParser.rb', line 44

def getForum(response)
  forum_json = JSON.parse response
  forum_array = forum_json["forums"]
  return jsonToForum(forum_array[0])
end

#getForums(response) ⇒ Object

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

Parameters

  • response
    • This JSON response contains the details fo forums.

Returns

  • List of Forum object.



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

def getForums(response)
  forums_all_json = JSON.parse response
  forums_all_array = forums_all_json["forums"]
  forums_class_array = Array.new
  for i in 0...forums_all_array.length
    forums_class_array.push(jsonToForum(forums_all_array[i]))
  end
  return forums_class_array
end

#getResult(response) ⇒ Object

  • Parse the JSON response and get the success message.

Parameters

  • response
    • This JSON response contains the success message.

Returns

  • String object.



115
116
117
118
119
# File 'lib/projects/parser/ForumParser.rb', line 115

def getResult(response)
  jsonObject = JSON.parse response
  result = jsonObject["response"]
  return result
end

#jsonToCategory(jsonObject) ⇒ Object

  • Parse the JSONObject into Category object.

  • jsonObject
    • JSONObject contains the details of a category.

Returns

  • Category object.



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/projects/parser/ForumParser.rb', line 165

def jsonToCategory(jsonObject)
  category = Category.new
  
  if jsonObject.has_key?("id")
    category.setId(jsonObject["id"])
  end
  if jsonObject.has_key?("name")
    category.setName(jsonObject["name"])
  end
  
  return category
end

#jsonToComment(jsonObject) ⇒ Object

  • Parse the JSONObject into Comment object.

Parameters

  • jsonObject
    • JSONObject contains the details of a comment.

Returns

  • Comment object.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/projects/parser/ForumParser.rb', line 224

def jsonToComment(jsonObject)
  comment = Comment.new
  
  if jsonObject.has_key?("id")
    comment.setId(jsonObject["id"])
  end
  if jsonObject.has_key?("content")
    comment.setContent(jsonObject["content"])
  end
  if jsonObject.has_key?("posted_by")
    comment.setPostedBy(jsonObject["posted_by"])
  end
  if jsonObject.has_key?("posted_person")
    comment.setPostedPerson(jsonObject["posted_person"])
  end
  if jsonObject.has_key?("post_date")
    comment.setPostDate(jsonObject["post_date"])
  end
  if jsonObject.has_key?("post_date_format")
    comment.setPostDateFormat(jsonObject["post_date_format"])
  end
  if jsonObject.has_key?("post_date_long")
    comment.setPostDateLong(jsonObject["post_date_long"])
  end
  
  return comment
end

#jsonToForum(jsonObject) ⇒ Object

  • Parse the JSONObject into Forum object.

Parameters

  • jsonObject
    • JSONObject contains the details of a forum.

Returns

  • Forum object.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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/projects/parser/ForumParser.rb', line 60

def jsonToForum(jsonObject)
  forum = Forum.new
  
  if jsonObject.has_key?("id") 
    forum.setId(jsonObject["id"])
  end
  if jsonObject.has_key?("name")
    forum.setName(jsonObject["name"])
  end
  if jsonObject.has_key?("content")
    forum.setContent(jsonObject["content"])
  end
  if jsonObject.has_key?("is_sticky_post")
    forum.setIsStickyPost(jsonObject["is_sticky_post"])
  end
  if jsonObject.has_key?("is_announcement_post")
    forum.setIsAnnouncementPost(jsonObject["is_announcement_post"])
  end
  if jsonObject.has_key?("posted_by")
    forum.setPostedBy(jsonObject["posted_by"])
  end
  if jsonObject.has_key?("posted_person")
    forum.setPostedPerson(jsonObject["posted_person"])
  end
  if jsonObject.has_key?("post_date")
    forum.setPostDate(jsonObject["post_date"])
  end
  if jsonObject.has_key?("post_date_format")
    forum.setPostDateFormat(jsonObject["post_date_format"])
  end
  if jsonObject.has_key?("post_date_long")
    forum.setPostDateLong(jsonObject["post_date_long"])
  end
  
  if jsonObject.has_key?("link")
    link = jsonObject["link"]
    
    if link.has_key?("self")
      forum.setURL(link["self"]["url"])
    end
  end
  
  return forum
end