class App42::Push::PushNotificationResponseBuilder

PushNotificationResponseBuilder class converts the JSON response retrieved from the server to the value object i.e Push Notification

Public Instance Methods

buildResponse(json) click to toggle source

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

@param json

- response in JSON format

@return Push Notification object filled with json data

# File lib/push/PushNotificationResposneBuilder.rb, line 28
def buildResponse(json)
  pushObj = PushNotification.new()
  channelList = Array.new
  pushObj.channelList=(channelList)

  pushObj.strResponse=json

  jsonObj = JSON.parse(json)
  jsonObjApp42 = jsonObj.fetch("app42");
  jsonObjResponse = jsonObjApp42.fetch("response");
  pushObj.isResponseSuccess=(jsonObjResponse.fetch("success"));
  jsonObjPush= jsonObjResponse.fetch("push");

  buildObjectFromJSONTree(pushObj, jsonObjPush);

  if jsonObjPush.key?("channels") == false
    return pushObj
  end

  jsonObjChannels = jsonObjPush.fetch("channels");

  if jsonObjChannels.key?("channel") == false
    return pushObj;
  end

  if jsonObjChannels.fetch("channel").instance_of?(Hash)
    jsonObjChannel = jsonObjChannels.fetch("channel");
    channelObject = App42::Push::Channel.new(pushObj)
    buildObjectFromJSONTree(channelObject,jsonObjChannel);

  else

    jsonObjChannelArray= jsonObjChannels.fetch("channel");

    jsonObjChannelArray.length.times do |i|
      jsonObjChannel = jsonObjChannelArray.fetch(i);
      channelObject = App42::Push::Channel.new(pushObj)
      buildObjectFromJSONTree(channelObject,jsonObjChannel);
    end
  end

  return pushObj;
end