Class: App42::Email::EmailResponseBuilder

Inherits:
App42ResponseBuilder show all
Defined in:
lib/email/EmailResponseBuilder.rb

Overview

EmailResponseBuilder class converts the JSON response retrieved from the server to the value object i.e Email

Instance Method Summary collapse

Methods inherited from App42ResponseBuilder

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

Instance Method Details

#buildResponse(json) ⇒ Object

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

Parameters:

  • json
    • response in JSON format

Returns:

  • Email object filled with json data



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/email/EmailResponseBuilder.rb', line 26

def buildResponse(json)
  emailObj = Email.new()

  configList = Array.new
  emailObj.configList=(configList)
  emailObj.strResponse=json
  jsonObj = JSON.parse(json)

  jsonObjApp42 = jsonObj["app42"]
  jsonObjResponse = jsonObjApp42["response"]

  emailObj.isResponseSuccess=(jsonObjResponse.fetch("success"))
  jsonObjEmail = jsonObjResponse["email"]

  buildObjectFromJSONTree(emailObj, jsonObjEmail);

  if jsonObjEmail.has_key?("configurations") == false
    return emailObj
  end

  jsonEmailConfig = jsonObjEmail["configurations"]

  if jsonEmailConfig.has_key?("config") == false
    return emailObj
  end

  if jsonEmailConfig["config"].instance_of?(Hash)
    # Only One attribute is there

    jsonObjConfig = jsonEmailConfig["config"]
    configItem = App42::Email::Configuration.new(emailObj)

    buildObjectFromJSONTree(configItem, jsonObjConfig);
  else

    # There is an Array of attribute
    jsonObjConfigArray = jsonEmailConfig["config"]
    jsonObjConfigArray.length.times do |i|
      # Get Individual Attribute Node and set it into Object

      jsonObjConfig = jsonObjConfigArray[i]
      configItem = App42::Email::Configuration.new(emailObj)
      buildObjectFromJSONTree(configItem, jsonObjConfig);
    end
  end
  return emailObj
end