Class: SalesforceCache::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_cache/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#oSfRestClientObject

Returns the value of attribute oSfRestClient.



7
8
9
# File 'lib/salesforce_cache/client.rb', line 7

def oSfRestClient
  @oSfRestClient
end

Instance Method Details

#getDirForType(sType = nil) ⇒ Object



26
27
28
29
# File 'lib/salesforce_cache/client.rb', line 26

def getDirForType(sType=nil)
  sDir   = File.join(@sFsdbBaseDir,sType)
  return sDir
end

#getFileForSfidAndType(sSfid = nil, sType = nil) ⇒ Object



21
22
23
24
# File 'lib/salesforce_cache/client.rb', line 21

def getFileForSfidAndType(sSfid=nil,sType=nil)
  sFile  = %Q{sf_#{sType}_#{sSfid}.json}
  return sFile
end

#getPathForSfidAndType(sSfid = nil, sType = nil) ⇒ Object



31
32
33
34
# File 'lib/salesforce_cache/client.rb', line 31

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/salesforce_cache/client.rb', line 36

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/salesforce_cache/client.rb', line 59

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/salesforce_cache/client.rb', line 84

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



103
104
105
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
# File 'lib/salesforce_cache/client.rb', line 103

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]
        return self.getSobjectForSfidAndType(sSfidAct,sType)
      end

    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