Class: SalesforceCache::Client
- Inherits:
-
Object
- Object
- SalesforceCache::Client
- Defined in:
- lib/salesforce_cache/client.rb
Instance Attribute Summary collapse
-
#oSfRestClient ⇒ Object
Returns the value of attribute oSfRestClient.
Instance Method Summary collapse
- #getCachedIds(xTypes = nil) ⇒ Object
- #getDirForType(sType = nil) ⇒ Object
- #getFileForSfidAndType(sSfid = nil, sType = nil) ⇒ Object
- #getPathForSfidAndType(sSfid = nil, sType = nil) ⇒ Object
- #getResForSoqlAndPath(sSoql = nil, sPath = nil) ⇒ Object
- #getResForSoqlAndPathFromRemote(sSoql = nil, sPath = nil) ⇒ Object
- #getSobjectForSfidAndType(sSfid = nil, sType = nil) ⇒ Object
- #getSobjectForSfidAndTypeFromRemote(sSfid = nil, sType = nil) ⇒ Object
-
#initialize(dParamsGeneral = {}, dParamsToken = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(dParamsGeneral = {}, dParamsToken = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 |
# 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 |
Instance Attribute Details
#oSfRestClient ⇒ Object
Returns the value of attribute oSfRestClient.
7 8 9 |
# File 'lib/salesforce_cache/client.rb', line 7 def oSfRestClient @oSfRestClient end |
Instance Method Details
#getCachedIds(xTypes = nil) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# 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) ⇒ Object
29 30 31 32 |
# 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) ⇒ Object
21 22 23 24 25 26 27 |
# 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) ⇒ Object
34 35 36 37 |
# 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) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# 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) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# 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) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# 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 |