class SalesforceCache::Client
Attributes
oSfRestClient[RW]
Public Class Methods
new(dParamsGeneral={},dParamsToken={})
click to toggle source
# File lib/salesforce_cache/client.rb, line 9 def initialize(dParamsGeneral={},dParamsToken={}) if dParamsGeneral.has_key?(:disable_remote) && dParamsGeneral[:disable_remote] @bRemoteEnabled = false else @bRemoteEnabled = true end @oSfRestClient = @bRemoteEnabled \ ? SalesforceCache::RestClient.new(dParamsGeneral,dParamsToken) : nil @sFsdbBaseDir = dParamsGeneral[:data_dir] || '.' @iMaxAgeSec = dParamsGeneral[:max_age] || 3600*24*7 end
Public Instance Methods
getCachedIds(xTypes=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 141 def getCachedIds(xTypes=nil) dTypes = {} if xTypes.is_a?(String) xTypes.split(',').each do |sType| sType.strip! dTypes[ sType ] = 1 end elsif xTypes.is_a?(Hash) xTypes.keys.each do |sType| sType.strip! dTypes[ sType ] = 1 end elsif xTypes.is_a?(Array) xTypes.each do |sType| sType.strip! dTypes[ sType ] = 1 end end if dTypes.keys.length == 0 Dir.entries(@sFsdbBaseDir).each do |sEntry| next if sEntry =~ /^\.\.?$/ sPath = File.join(@sFsdbBaseDir,sEntry) if Dir.exists?(sPath) dTypes[ sEntry ] = 1 end end end aIdsInfo = [] dTypes.keys.each do |sType| sDirType = File.join(@sFsdbBaseDir,sType) next unless Dir.exists?(sDirType) sTypeRe = Regexp.escape(sType) Dir.entries(sDirType).each do |sEntry| if sEntry =~ /^sf_#{sTypeRe}_(.+)\.json$/ sId = $1 dInfo = {:id => sId, :type => sType} sPathJson = File.join(sDirType,sEntry) if File.size(sPathJson) > 0 aIdsInfo.push(dInfo) end end end end return aIdsInfo end
getDirForType(sType=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 29 def getDirForType(sType=nil) sDir = File.join(@sFsdbBaseDir,sType) return sDir end
getFileForSfidAndType(sSfid=nil,sType=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 21 def getFileForSfidAndType(sSfid=nil,sType=nil) return nil if sSfid.nil? || sType.nil? sSfid.strip! sType.strip! sFile = %Q{sf_#{sType}_#{sSfid}.json} return sFile end
getPathForSfidAndType(sSfid=nil,sType=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 34 def getPathForSfidAndType(sSfid=nil,sType=nil) sPath = File.join( self.getDirForType(sType), self.getFileForSfidAndType(sSfid,sType)) return sPath end
getResForSoqlAndPath(sSoql=nil,sPath=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 39 def getResForSoqlAndPath(sSoql=nil,sPath=nil) return nil if sSoql.nil? if ! sPath.nil? && sPath !~ /^\s*\// sPath.strip! sPath = File.join(@sFsdbBaseDir,sPath) end dRes = { :meta => { :iEpochRetrieved => -1, :iStatus => 404 } } if !sPath.nil? && File.exists?(sPath) jTop = File.open(sPath,'r').read dTop = JSON.parse(jTop,:symbolize_names=>true) if dTop.has_key?(:source) && dTop.has_key?(:meta) && dTop[:meta].has_key?(:iEpochRetrieved) iEpochRetrieved = dTop[:meta][:iEpochRetrieved] iEpochNow = Time.now.to_i iAgeSec = iEpochNow - iEpochRetrieved if ! @bRemoteEnabled || ( iAgeSec < @iMaxAgeSec && dTop[:source].has_key?(:done) ) dRes = dTop[:source] return dRes end end end return @bRemoteEnabled ? self.getResForSoqlAndPathFromRemote(sSoql,sPath) : nil end
getResForSoqlAndPathFromRemote(sSoql=nil,sPath=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 62 def getResForSoqlAndPathFromRemote(sSoql=nil,sPath=nil) return nil if sSoql.nil? if ! sPath.nil? && sPath !~ /^\s*\// sPath.strip! sPath = File.join(@sFsdbBaseDir,sPath) end dRes = @oSfRestClient.getSoqlResults(sSoql) if ! dRes.nil? && dRes.has_key?(:done) && dRes[:done] == true && ! sPath.nil? if sPath =~ /^([\S]+)\/[^\/]+$/ sDir = $1 FileUtils.mkdir_p(sDir) if ! Dir.exists?(sDir) end File.open(sPath,'w') do |fTop| dTop = { :meta => { :iEpochRetrieved => Time.now.to_i, :iStatus => 200 }, :source => dRes } jTop = JSON.dump(dTop) fTop.puts(jTop) end return dRes end return nil end
getSobjectForSfidAndType(sSfid=nil,sType=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 87 def getSobjectForSfidAndType(sSfid=nil,sType=nil) sPath = self.getPathForSfidAndType(sSfid,sType) dObj = { :meta => { :iEpochRetrieved => -1, :iStatus => 404 } } if File.exists?(sPath) jTop = File.open(sPath,'r').read dTop = JSON.parse(jTop,:symbolize_names=>true) if dTop.has_key?(:source) && dTop.has_key?(:meta) && dTop[:meta].has_key?(:iEpochRetrieved) iEpochRetrieved = dTop[:meta][:iEpochRetrieved] iEpochNow = Time.now.to_i iAgeSec = iEpochNow - iEpochRetrieved if ! @bRemoteEnabled || ( iAgeSec < @iMaxAgeSec && dTop[:source].has_key?(:Id) ) dObj = dTop[:source] return dObj end end end return @bRemoteEnabled ? self.getSobjectForSfidAndTypeFromRemote(sSfid,sType) : nil end
getSobjectForSfidAndTypeFromRemote(sSfid=nil,sType=nil)
click to toggle source
# File lib/salesforce_cache/client.rb, line 106 def getSobjectForSfidAndTypeFromRemote(sSfid=nil,sType=nil) dObj = @oSfRestClient.getSobjectForSfidAndType(sSfid,sType) if (dObj.is_a?(Array) && dObj[0][:errorCode] == 'NOT_FOUND') || dObj.nil? if sType == 'Account' dObjOppTry = self.getSobjectForSfidAndType(sSfid,'Opportunity') if dObjOppTry.is_a?(Hash) && dObjOppTry.has_key?(:AccountId) sSfidAct = dObjOppTry[:AccountId] if ! sSfidAct.nil? && sSfidAct.kind_of?(String) && sSfidAct.length > 0 return self.getSobjectForSfidAndType(sSfidAct,sType) end end return nil else return nil end end if dObj.is_a?(Hash) && dObj.has_key?(:Id) sDir = self.getDirForType(sType) sPath = self.getPathForSfidAndType(sSfid,sType) FileUtils.mkdir_p(sDir) if ! Dir.exists?(sDir) File.open(sPath,'w') do |fTop| dTop = { :meta => { :iEpochRetrieved => Time.now.to_i, :iStatus => 200 }, :source => dObj } jTop = JSON.dump(dTop) fTop.puts(jTop) end return dObj end return nil end