salesforce_fsdb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5800738a75803b32473b3cf8004f4132922692b0
4
+ data.tar.gz: 2bb44d97b73ec6c5d266ff908af62f3d6c7a53de
5
+ SHA512:
6
+ metadata.gz: 3af6c597ceacb8b27a0c8896b0f374300442e9b4a8ff8bcca61d68b858d45f7054a7c302f6e5d5d2e49abbbb1139635922c403dda741c565beb9bea9804965af
7
+ data.tar.gz: f6d3d8dee9c54328bc014eb2a1537082af1d04f1edaacce6bdd3dc35fecc1c6c9b36fcdf2fa356970370c992d7bf7c71201febc0f60d9860626611a31828d05c
data/CHANGELOG CHANGED
@@ -1,5 +1,8 @@
1
+ = 0.0.3
2
+ - Add SOQL result caching.
3
+
1
4
  = 0.0.2
2
- - Use SObject types in filenames
5
+ - Use SObject types in filenames.
3
6
 
4
7
  = 0.0.1
5
8
  - Initial release
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = SalesforceFsdb
2
2
 
3
- Salesforce SObject system to retrieve and cache SObjects.
3
+ Salesforce SObject system to retrieve and cache queries from Salesforce.com including SObjects and SOQL results.
4
4
 
5
5
  == Installation
6
6
 
@@ -33,12 +33,28 @@ Download and install salesforce_fsdb with the following:
33
33
  sfFsdbClient = SalesforceFsdb::Client.new(paramsGeneral,paramsToken)
34
34
  sObjectHash = sfFsdbClient.getSobjectForSfidAndType('my_sobject_id','Account')
35
35
 
36
+ sSfid = '1234567890'
37
+ sSoql = %{ SELECT Id,AccountId,Name,IsWon,StageName,CloseDate from Opportunity where AccountId='#{sSfid}' }
38
+ sPath = "AccountOpportunities/sf_act_#{sSfid}_opps.jff"
39
+
40
+ dResults = sfFsdbClient.getResForSoqlAndPath( sSoql, sPath )
41
+
36
42
  # without using a filesystem cache
37
43
  sfRestClient = SalesforceFsdb::RestClient.new(paramsGeneral,paramsToken)
38
44
  sObjectHash = sfRestClient.getSobjectForSfidAndType('my_sobject_id','Account')
39
45
 
40
46
  == Notes
41
47
 
48
+ === Max Age
49
+
50
+ The :max_age parameter is used to indicate the maximum age in seconds for cached data. If local data exceeds maximum age, a new copy of the data is retrieved from Salesforce.com. To ensure retrieval of a new copy, use :max_age=-1.
51
+
52
+ === Directory Paths
53
+
54
+ Salesforce Sobjects are stored on the file system under File.join( paramsGeneral[:data_dir], Sobject_type ).
55
+
56
+ SOQL results are stored in the path indicated. Sub-directories are created for SOQL paths. If a relative directory is provided, it is appended to the :data_dir. If an absolute directory is provided, it is used as an absolute directory in its entirety.
57
+
42
58
  === Object Size
43
59
 
44
60
  Some large Salesforce Objects may generate Salesforce errors. A future release will allow specifying specific fields for retrieval.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rdoc/task'
5
5
  desc 'Default: run unit tests.'
6
6
  task :default => :test
7
7
 
8
- desc 'Test the Base58GMP library.'
8
+ desc 'Test the SalesforceFsdb library.'
9
9
  Rake::TestTask.new do |t|
10
10
  t.libs << 'lib'
11
11
  t.pattern = 'test/**/test_*.rb'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,32 +5,84 @@ require 'salesforce_fsdb/rest_client'
5
5
  module SalesforceFsdb
6
6
  class Client
7
7
  attr_accessor :oSfRestClient
8
+
8
9
  def initialize(dParamsGeneral={},dParamsToken={})
9
10
  @oSfRestClient = SalesforceFsdb::RestClient.new(dParamsGeneral,dParamsToken)
10
11
  @sFsdbBaseDir = dParamsGeneral[:data_dir] || '.'
11
12
  @iMaxAgeSec = dParamsGeneral[:max_age] || 3600*24*7
12
13
  end
14
+
13
15
  def getFileForSfidAndType(sSfid=nil,sType=nil)
14
16
  sFile = %Q{sf_#{sType}_#{sSfid}.json}
15
17
  return sFile
16
18
  end
19
+
17
20
  def getDirForType(sType=nil)
18
21
  sDir = File.join(@sFsdbBaseDir,sType)
19
22
  return sDir
20
23
  end
24
+
21
25
  def getPathForSfidAndType(sSfid=nil,sType=nil)
22
26
  sPath = File.join( self.getDirForType(sType), self.getFileForSfidAndType(sSfid,sType))
23
27
  return sPath
24
28
  end
29
+
30
+ def getResForSoqlAndPath(sSoql=nil,sPath=nil)
31
+ return nil if sSoql.nil?
32
+ if ! sPath.nil? && sPath !~ /^\s*\//
33
+ sPath.strip!
34
+ sPath = File.join(@sFsdbBaseDir,sPath)
35
+ end
36
+ dRes = { :meta => { :iEpochRetrieved => -1, :iStatus => 404 } }
37
+ if !sPath.nil? && File.exists?(sPath)
38
+ jTop = File.open(sPath,'r').read
39
+ dTop = JSON.parse(jTop,:symbolize_names=>true)
40
+ if dTop.has_key?(:source) && dTop.has_key?(:meta) && dTop[:meta].has_key?(:iEpochRetrieved)
41
+ iEpochRetrieved = dTop[:meta][:iEpochRetrieved]
42
+ iEpochNow = Time.now.to_i
43
+ iAgeSec = iEpochNow - iEpochRetrieved
44
+ if iAgeSec < @iMaxAgeSec && dTop[:source].has_key?(:done)
45
+ dRes = dTop[:source]
46
+ return dRes
47
+ end
48
+ end
49
+ end
50
+ return self.getResForSoqlAndPathFromRemote(sSoql,sPath)
51
+ end
52
+
53
+ def getResForSoqlAndPathFromRemote(sSoql=nil,sPath=nil)
54
+ return nil if sSoql.nil?
55
+ if ! sPath.nil? && sPath !~ /^\s*\//
56
+ sPath.strip!
57
+ sPath = File.join(@sFsdbBaseDir,sPath)
58
+ end
59
+ dRes = @oSfRestClient.getSoqlResults(sSoql)
60
+ if ! dRes.nil? && dRes.has_key?(:done) && dRes[:done] == true && ! sPath.nil?
61
+ if sPath =~ /^([\S]+)\/[^\/]+$/
62
+ sDir = $1
63
+ FileUtils.mkdir_p(sDir) if ! Dir.exists?(sDir)
64
+ end
65
+ File.open(sPath,'w') do |fTop|
66
+ dTop = {
67
+ :meta => { :iEpochRetrieved => Time.now.to_i, :iStatus => 200 },
68
+ :source => dRes
69
+ }
70
+ jTop = JSON.dump(dTop)
71
+ fTop.puts(jTop)
72
+ end
73
+ return dRes
74
+ end
75
+ return nil
76
+ end
77
+
25
78
  def getSobjectForSfidAndType(sSfid=nil,sType=nil)
26
79
  sPath = self.getPathForSfidAndType(sSfid,sType)
27
80
  dObj = { :meta => { :iEpochRetrieved => -1, :iStatus => 404 } }
28
- iEpochNow = Time.now.to_i
29
81
  if File.exists?(sPath)
30
82
  jTop = File.open(sPath,'r').read
31
83
  dTop = JSON.parse(jTop,:symbolize_names=>true)
32
- if dTop.has_key?(:source) && dTop.has_key?(:meta) && dObj[:meta].has_key?(:iEpochRetrieved)
33
- iEpochRetrieved = dObj[:meta][:iEpochRetrieved]
84
+ if dTop.has_key?(:source) && dTop.has_key?(:meta) && dTop[:meta].has_key?(:iEpochRetrieved)
85
+ iEpochRetrieved = dTop[:meta][:iEpochRetrieved]
34
86
  iEpochNow = Time.now.to_i
35
87
  iAgeSec = iEpochNow - iEpochRetrieved
36
88
  if iAgeSec < @iMaxAgeSec && dTop[:source].has_key?(:Id)
@@ -41,6 +93,7 @@ module SalesforceFsdb
41
93
  end
42
94
  return self.getSobjectForSfidAndTypeFromRemote(sSfid,sType)
43
95
  end
96
+
44
97
  def getSobjectForSfidAndTypeFromRemote(sSfid=nil,sType=nil)
45
98
  dObj = @oSfRestClient.getSobjectForSfidAndType(sSfid,sType)
46
99
  if dObj.has_key?(:Id)
@@ -57,7 +110,8 @@ module SalesforceFsdb
57
110
  end
58
111
  return dObj
59
112
  end
60
- return {}
113
+ return nil
61
114
  end
115
+
62
116
  end
63
117
  end
@@ -3,8 +3,10 @@ require 'json'
3
3
 
4
4
  module SalesforceFsdb
5
5
  class RestClient
6
+
6
7
  attr_accessor :sSfTokenUrl
7
8
  attr_accessor :dTokenRes
9
+
8
10
  def initialize(dParamsGeneral={},dParamsToken={})
9
11
  @dParamsToken = dParamsToken
10
12
  @sSfTokenUrl = dParamsGeneral.has_key?(:token_url) ? dParamsGeneral[:token_url] \
@@ -14,20 +16,24 @@ module SalesforceFsdb
14
16
  @dTokenRes = {}
15
17
  self.loadToken()
16
18
  end
19
+
17
20
  def loadToken()
18
21
  oRes = Faraday.post(@sSfTokenUrl,@dParamsToken)
19
22
  @dTokenRes = JSON.parse(oRes.body,:symbolize_names=>true)
20
23
  return self
21
24
  end
25
+
22
26
  def getAttr(yKey=nil)
23
27
  yKey = yKey.to_sym if yKey.is_a?(String)
24
28
  xxVal = @dTokenRes.has_key?(yKey) ? @dTokenRes[yKey] : nil
25
29
  return xxVal
26
30
  end
31
+
27
32
  def getHeaders()
28
33
  dHeaders = { 'Authorization' => 'Bearer ' + self.getAttr(:access_token) }
29
34
  return dHeaders
30
35
  end
36
+
31
37
  def getSoqlResults(sSoql=nil)
32
38
  dQry = { :q => sSoql}
33
39
  sUrl = "https://#{@sApiFqdn}/services/data/v#{@sApiVersion}/query"
@@ -36,10 +42,12 @@ module SalesforceFsdb
36
42
  dRes = JSON.parse(oRes.body,:symbolize_names=>true)
37
43
  return dRes
38
44
  end
45
+
39
46
  def getSfUrlForSfidAndType(sSfid=nil,sType=nil)
40
47
  sUrl = "https://#{@sApiFqdn}/services/data/v#{@sApiVersion}/sobjects/#{sType}/#{sSfid}/"
41
48
  return sUrl
42
49
  end
50
+
43
51
  def getSobjectForSfidAndType(sSfid=nil,sType=nil)
44
52
  sUrl = self.getSfUrlForSfidAndType(sSfid,sType)
45
53
  sHead = self.getHeaders()
@@ -47,5 +55,6 @@ module SalesforceFsdb
47
55
  dRes = JSON.parse(oRes.body,:symbolize_names=>true)
48
56
  return dRes
49
57
  end
58
+
50
59
  end
51
60
  end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'salesforce_fsdb'
3
- s.version = '0.0.2'
4
- s.date = '2014-01-28'
3
+ s.version = '0.0.3'
4
+ s.date = '2014-02-06'
5
5
  s.summary = 'Salesforce Filesystem DB'
6
6
  s.description = 'Filesystem cache manager for Salesforce sObjects'
7
7
  s.authors = ['John Wang']
@@ -19,4 +19,4 @@ Gem::Specification.new do |s|
19
19
  ]
20
20
  s.homepage = 'http://johnwang.com/'
21
21
  s.license = 'MIT'
22
- end
22
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: salesforce_fsdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - John Wang
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-01-28 00:00:00.000000000 Z
11
+ date: 2014-02-06 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Filesystem cache manager for Salesforce sObjects
15
14
  email: johncwang@gmail.com
@@ -22,33 +21,32 @@ files:
22
21
  - README.rdoc
23
22
  - Rakefile
24
23
  - VERSION
25
- - salesforce_fsdb.gemspec
26
24
  - lib/salesforce_fsdb/client.rb
27
25
  - lib/salesforce_fsdb/rest_client.rb
26
+ - salesforce_fsdb.gemspec
28
27
  - test/test_setup.rb
29
28
  homepage: http://johnwang.com/
30
29
  licenses:
31
30
  - MIT
31
+ metadata: {}
32
32
  post_install_message:
33
33
  rdoc_options: []
34
34
  require_paths:
35
35
  - lib
36
36
  required_ruby_version: !ruby/object:Gem::Requirement
37
- none: false
38
37
  requirements:
39
- - - ! '>='
38
+ - - ">="
40
39
  - !ruby/object:Gem::Version
41
40
  version: '0'
42
41
  required_rubygems_version: !ruby/object:Gem::Requirement
43
- none: false
44
42
  requirements:
45
- - - ! '>='
43
+ - - ">="
46
44
  - !ruby/object:Gem::Version
47
45
  version: '0'
48
46
  requirements: []
49
47
  rubyforge_project:
50
- rubygems_version: 1.8.23
48
+ rubygems_version: 2.2.1
51
49
  signing_key:
52
- specification_version: 3
50
+ specification_version: 4
53
51
  summary: Salesforce Filesystem DB
54
52
  test_files: []