Sha256: 339a596121ab729ffb69f19923d1944b43c9155ea302730b1d8694388cf52eaa

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# -----------------------------------------------------------------------
#  Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved.
# -----------------------------------------------------------------------

require 'json'

class App42ResponseBuilder
  #      @param obj
  #      @param jsonObj
  #      @raise Exception
  def buildObjectFromJSONTree(obj,jsonObj)
    names = getNames(jsonObj)
    for name in names
      value = jsonObj[name]
      fieldName = "@"+name
      obj.instance_variable_set(:"#{fieldName}", value)
    end
  end

  #      @param serviceName
  #      @param json
  #      @return
  #      @raise Exception

  def getServiceJSONObject(serviceName,json)
    jsonObj = JSON.parse(json) rescue nil
    jsonObjApp42 = jsonObj["app42"]
    jsonObjResponse = jsonObjApp42["response"]
    jsonObjService = jsonObjResponse["#{serviceName}"]
    return jsonObjService;
  end

  #      @param json
  #      @return
  #      @raise Exception

  def isResponseSuccess(json)
    jsonObj = JSON.parse(json) rescue nil
    p jsonObj
    jsonObjApp42 = jsonObj["app42"]
    jsonObjResponse = jsonObjApp42["response"]
    return jsonObjResponse["success"]
  end

  def getNames(obj)
    names = []
    obj.each do |key, value|
      # puts"key is #{key}"  # key holds the key, value holds the value
      names.push(key)
    end
    return names
  end

  def getTotalRecords(json)
    totalRecords = -1;
    jsonObj = JSON.parse(json) rescue nil
    jsonObjApp42 = jsonObj["app42"]
    jsonObjResponse = jsonObjApp42["response"]
    if jsonObjResponse != nil && jsonObjResponse.key?("totalRecords")
      totalRecords = jsonObjResponse.fetch("totalRecords");
    end
    return totalRecords
    #json_obj = JSON.parse(json) rescue nil
    #totalRecords = json_obj.fetch("totalRecords") if  !json_obj["app42"]["response"].nil? && json_obj["app42"]["response"].key?("totalRecords")
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
App42_Ruby_API-0.8.4 lib/App42_Ruby_API/App42ResponseBuilder.rb