Class: SalesforceFsdb::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dParamsGeneral = {}, dParamsToken = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/salesforce_fsdb/client.rb', line 8

def initialize(dParamsGeneral={},dParamsToken={})
  @oSfRestClient  = SalesforceFsdb::RestClient.new(dParamsGeneral,dParamsToken)
  @sFsdbBaseDir   = dParamsGeneral[:data_dir] || '.'
  @iMaxAgeSec     = dParamsGeneral[:max_age]  || 3600*24*7
  @dType3ToType   = {
    'Account'     => 'act',
    'Contact'     => 'con',
    'Opportunity' => 'opp',
    'User'        => 'usr'
  }
end

Instance Attribute Details

#oSfRestClientObject

Returns the value of attribute oSfRestClient.



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

def oSfRestClient
  @oSfRestClient
end

Instance Method Details

#getDirForType(sType3 = nil) ⇒ Object



24
25
26
27
28
# File 'lib/salesforce_fsdb/client.rb', line 24

def getDirForType(sType3=nil)
  sType3 = @dType3ToType[sType3] || sType3
  sDir   = File.join(@sFsdbBaseDir,sType3)
  return sDir
end

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



19
20
21
22
23
# File 'lib/salesforce_fsdb/client.rb', line 19

def getFileForSfidAndType(sSfid=nil,sType3=nil)
  sType3 = @dType3ToType[sType3] || sType3
  sFile  = %Q{sf_#{sType3}_#{sSfid}.json}
  return sFile
end

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



29
30
31
32
33
# File 'lib/salesforce_fsdb/client.rb', line 29

def getPathForSfidAndType(sSfid=nil,sType3=nil)
  sType3 = @dType3ToType[sType3] || sType3
  sPath  = File.join( self.getDirForType(sType3), self.getFileForSfidAndType(sSfid,sType3))
  return sPath
end

#getSobjectForSfidAndType(sSfid = nil, sType3 = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/salesforce_fsdb/client.rb', line 34

def getSobjectForSfidAndType(sSfid=nil,sType3=nil)
  sType3    = @dType3ToType[sType3] || sType3
  sPath     = self.getPathForSfidAndType(sSfid,sType3)
  dObj      = { :meta => { :iEpochRetrieved => -1, :iStatus => 404 } }
  iEpochNow = Time.now.to_i
  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) && dObj[:meta].has_key?(:iEpochRetrieved)
      iEpochRetrieved = dObj[:meta][:iEpochRetrieved]
      iEpochNow       = Time.now.to_i
      iAgeSec         = iEpochNow - iEpochRetrieved
      if iAgeSec < @iMaxAgeSec && dTop[:source].has_key?(:Id)
        dObj = dTop[:source]
        return dObj
      end
    end
  end
  return self.getSobjectForSfidAndTypeFromRemote(sSfid,sType3)
end

#getSobjectForSfidAndTypeFromRemote(sSfid = nil, sType3 = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/salesforce_fsdb/client.rb', line 54

def getSobjectForSfidAndTypeFromRemote(sSfid=nil,sType3=nil)
  dObj = @oSfRestClient.getSobjectForSfidAndType(sSfid,sType3)
  if dObj.has_key?(:Id)
    sDir        = self.getDirForType(sType3)
    sPath       = self.getPathForSfidAndType(sSfid,sType3)
    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 {}
end