class App42::Email::EmailResponseBuilder

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

Public Instance Methods

buildResponse(json) click to toggle source

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

@param json

- response in JSON format

@return Email object filled with json data

# 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