salesforce_fsdb 0.0.1

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: b9202234becb758cb247e79a0c299c33718f3cb5
4
+ data.tar.gz: 5e2049d2b4f535dc1841128c479040678e809466
5
+ SHA512:
6
+ metadata.gz: a570ecbd949fb17204ea58681c5a4bb7100bbfc3113f40d46e7dd20bed05fde7e5d8640916e5661bc05e41e9d2077656783b7f37b5681c9a5ee274964f79d5c4
7
+ data.tar.gz: a5df11edcd11aa61c5dfa4965be04dbe69a3db18c9be6cdb48b923dba1dbba844b22269f899d62fe8c84c2158a07e53f63acb5d94b327b9ec895dae6038ae3e1
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ = 0.0.1
2
+ - Initial release
3
+ - Filesystem cache of Account, Contact, Opportunity and User sObjects.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 John Wang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ = SalesforceFsdb
2
+
3
+ Salesforce SObject system to retrieve and cache SObjects.
4
+
5
+ == Installation
6
+
7
+ === Gem Installation
8
+
9
+ Download and install salesforce_fsdb with the following:
10
+
11
+ gem install salesforce_fsdb
12
+
13
+ == Usage
14
+
15
+ require 'salesforce_fsdb/client'
16
+
17
+ paramsGeneral = {
18
+ :api_fqdn => 'na1.salesforce.com',
19
+ :api_version => '29.0',
20
+ :data_dir => '/path/to/sf_data',
21
+ :max_age => 60*60*24*7
22
+ }
23
+
24
+ paramsToken = {
25
+ :grant_type => 'password',
26
+ :client_id => 'my_client_id',
27
+ :client_secret => 'my_client_secret',
28
+ :username => 'my_username',
29
+ :password => 'my_password'
30
+ }
31
+
32
+ # using the filesystem cache with max_age
33
+ sfFsdbClient = SalesforceFsdb::Client.new(paramsGeneral,paramsToken)
34
+ sObjectHash = sfFsdbClient.getSobjectForSfidAndType('my_sobject_id','Account')
35
+
36
+ # without using a filesystem cache
37
+ sfRestClient = SalesforceFsdb::RestClient.new(paramsGeneral,paramsToken)
38
+ sObjectHash = sfRestClient.getSobjectForSfidAndType('my_sobject_id','Account')
39
+
40
+ == Notes
41
+
42
+ === Object Size
43
+
44
+ Some large Salesforce Objects may generate Salesforce errors. A future release will allow specifying specific fields for retrieval.
45
+
46
+ == Links
47
+
48
+ Salesforce REST API Reference
49
+
50
+ http://www.salesforce.com/us/developer/docs/api_rest/
51
+
52
+ == Problems, Comments, Suggestions?
53
+
54
+ All of the above are most welcome. mailto:johncwang@gmail.com
55
+
56
+ == Credits
57
+
58
+ John Wang - http://johnwang.com
59
+
60
+ == License
61
+
62
+ SalesforceFsdb is available under an MIT-style license.
63
+
64
+ :include: MIT-LICENSE
65
+
66
+ == Warranty
67
+
68
+ This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the Base58GMP library.'
9
+ Rake::TestTask.new do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/test_*.rb'
12
+ t.verbose = false
13
+ end
14
+
15
+ desc 'Generate RDoc documentation.'
16
+ RDoc::Task.new do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'salesforce_fsdb'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,73 @@
1
+ require 'fileutils'
2
+ require 'json'
3
+ require 'salesforce_fsdb/rest_client'
4
+
5
+ module SalesforceFsdb
6
+ class Client
7
+ attr_accessor :oSfRestClient
8
+ def initialize(dParamsGeneral={},dParamsToken={})
9
+ @oSfRestClient = SalesforceFsdb::RestClient.new(dParamsGeneral,dParamsToken)
10
+ @sFsdbBaseDir = dParamsGeneral[:data_dir] || '.'
11
+ @iMaxAgeSec = dParamsGeneral[:max_age] || 3600*24*7
12
+ @dType3ToType = {
13
+ 'Account' => 'act',
14
+ 'Contact' => 'con',
15
+ 'Opportunity' => 'opp',
16
+ 'User' => 'usr'
17
+ }
18
+ end
19
+ def getFileForSfidAndType(sSfid=nil,sType3=nil)
20
+ sType3 = @dType3ToType[sType3] || sType3
21
+ sFile = %Q{sf_#{sType3}_#{sSfid}.json}
22
+ return sFile
23
+ end
24
+ def getDirForType(sType3=nil)
25
+ sType3 = @dType3ToType[sType3] || sType3
26
+ sDir = File.join(@sFsdbBaseDir,sType3)
27
+ return sDir
28
+ end
29
+ def getPathForSfidAndType(sSfid=nil,sType3=nil)
30
+ sType3 = @dType3ToType[sType3] || sType3
31
+ sPath = File.join( self.getDirForType(sType3), self.getFileForSfidAndType(sSfid,sType3))
32
+ return sPath
33
+ end
34
+ def getSobjectForSfidAndType(sSfid=nil,sType3=nil)
35
+ sType3 = @dType3ToType[sType3] || sType3
36
+ sPath = self.getPathForSfidAndType(sSfid,sType3)
37
+ dObj = { :meta => { :iEpochRetrieved => -1, :iStatus => 404 } }
38
+ iEpochNow = Time.now.to_i
39
+ if File.exists?(sPath)
40
+ jTop = File.open(sPath,'r').read
41
+ dTop = JSON.parse(jTop,:symbolize_names=>true)
42
+ if dTop.has_key?(:source) && dTop.has_key?(:meta) && dObj[:meta].has_key?(:iEpochRetrieved)
43
+ iEpochRetrieved = dObj[:meta][:iEpochRetrieved]
44
+ iEpochNow = Time.now.to_i
45
+ iAgeSec = iEpochNow - iEpochRetrieved
46
+ if iAgeSec < @iMaxAgeSec && dTop[:source].has_key?(:Id)
47
+ dObj = dTop[:source]
48
+ return dObj
49
+ end
50
+ end
51
+ end
52
+ return self.getSobjectForSfidAndTypeFromRemote(sSfid,sType3)
53
+ end
54
+ def getSobjectForSfidAndTypeFromRemote(sSfid=nil,sType3=nil)
55
+ dObj = @oSfRestClient.getSobjectForSfidAndType(sSfid,sType3)
56
+ if dObj.has_key?(:Id)
57
+ sDir = self.getDirForType(sType3)
58
+ sPath = self.getPathForSfidAndType(sSfid,sType3)
59
+ FileUtils.mkdir_p(sDir) if ! Dir.exists?(sDir)
60
+ File.open(sPath,'w') do |fTop|
61
+ dTop = {
62
+ :meta => { :iEpochRetrieved => Time.now.to_i, :iStatus => 200 },
63
+ :source => dObj
64
+ }
65
+ jTop = JSON.dump(dTop)
66
+ fTop.puts(jTop)
67
+ end
68
+ return dObj
69
+ end
70
+ return {}
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,62 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module SalesforceFsdb
5
+ class RestClient
6
+ attr_accessor :sSfTokenUrl
7
+ attr_accessor :dTokenRes
8
+ def initialize(dParamsGeneral={},dParamsToken={})
9
+ @dParamsToken = dParamsToken
10
+ @sSfTokenUrl = dParamsGeneral.has_key?(:token_url) ? dParamsGeneral[:token_url] \
11
+ : 'https://login.salesforce.com/services/oauth2/token'
12
+ @sApiFqdn = dParamsGeneral.has_key?(:api_fqdn) ? dParamsGeneral[:api_fqdn] : 'na5.salesforce.com'
13
+ @sApiVersion = dParamsGeneral.has_key?(:api_version) ? dParamsGeneral[:api_version] : '27.0'
14
+ @dTokenRes = {}
15
+ @dType3ToType = {
16
+ 'act' => 'Account',
17
+ 'con' => 'Contact',
18
+ 'opp' => 'Opportunity',
19
+ 'usr' => 'User'
20
+ }
21
+ self.loadToken()
22
+ end
23
+ def loadToken()
24
+ oRes = Faraday.post(@sSfTokenUrl,@dParamsToken)
25
+ @dTokenRes = JSON.parse(oRes.body,:symbolize_names=>true)
26
+ return self
27
+ end
28
+ def getAttr(yKey=nil)
29
+ yKey = yKey.to_sym if yKey.is_a?(String)
30
+ xxVal = @dTokenRes.has_key?(yKey) ? @dTokenRes[yKey] : nil
31
+ return xxVal
32
+ end
33
+ def getHeaders()
34
+ dHeaders = { 'Authorization' => 'Bearer ' + self.getAttr(:access_token) }
35
+ return dHeaders
36
+ end
37
+ def getSoqlResults(sSoql=nil)
38
+ dQry = { :q => sSoql}
39
+ sUrl = "https://#{@sApiFqdn}/services/data/v#{@sApiVersion}/query"
40
+ oRes = Faraday.get(sUrl,dQry)
41
+ dRes = JSON.parse(oRes.body,:symbolize_names=>true)
42
+ return dRes
43
+ end
44
+ def getNormalizedType(sType=nil)
45
+ #sType = @dType3ToType[sType] if @dType3ToType.has_key?(sType)
46
+ sType = @dType3ToType[sType] || sType
47
+ return sType
48
+ end
49
+ def getSfUrlForSfidAndType(sSfid=nil,sType=nil)
50
+ sType = self.getNormalizedType(sType)
51
+ sUrl = "https://#{@sApiFqdn}/services/data/v#{@sApiVersion}/sobjects/#{sType}/#{sSfid}/"
52
+ return sUrl
53
+ end
54
+ def getSobjectForSfidAndType(sSfid=nil,sType=nil)
55
+ sUrl = self.getSfUrlForSfidAndType(sSfid,sType)
56
+ sHead = self.getHeaders()
57
+ oRes = Faraday.get(sUrl,{},sHead)
58
+ dRes = JSON.parse(oRes.body,:symbolize_names=>true)
59
+ return dRes
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'salesforce_fsdb'
3
+ s.version = '0.0.1'
4
+ s.date = '2014-01-27'
5
+ s.summary = 'Salesforce Filesystem DB'
6
+ s.description = 'Filesystem cache manager for Salesforce sObjects'
7
+ s.authors = ['John Wang']
8
+ s.email = 'johncwang@gmail.com'
9
+ s.files = [
10
+ 'CHANGELOG',
11
+ 'MIT-LICENSE',
12
+ 'README.rdoc',
13
+ 'Rakefile',
14
+ 'VERSION',
15
+ 'salesforce_fsdb.gemspec',
16
+ 'lib/salesforce_fsdb/client.rb',
17
+ 'lib/salesforce_fsdb/rest_client.rb',
18
+ 'test/test_setup.rb'
19
+ ]
20
+ s.homepage = 'http://johnwang.com/'
21
+ s.license = 'MIT'
22
+ end
@@ -0,0 +1,29 @@
1
+ require 'test/unit'
2
+ require 'salesforce_fsdb/client'
3
+
4
+ class SalesforceFsdbTest < Test::Unit::TestCase
5
+ def testSetup
6
+ paramsGeneral = {
7
+ :api_fqdn => 'na1.salesforce.com',
8
+ :api_version => '29.0',
9
+ :data_dir => '/path/to/sf_data',
10
+ :max_age => 60*60*24*7
11
+ }
12
+
13
+ paramsToken = {
14
+ :grant_type => 'password',
15
+ :client_id => 'my_client_id',
16
+ :client_secret => 'my_client_secret',
17
+ :username => 'my_username',
18
+ :password => 'my_password'
19
+ }
20
+
21
+ sfFsdbClient = SalesforceFsdb::Client.new(paramsGeneral,paramsToken)
22
+ accountDir = sfFsdbClient.getDirForType('Account')
23
+
24
+ assert_equal accountDir, File.join(paramsGeneral[:data_dir],'act')
25
+
26
+ tokenUrl = 'https://login.salesforce.com/services/oauth2/token'
27
+ assert_equal tokenUrl, sfFsdbClient.oSfRestClient.sSfTokenUrl
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: salesforce_fsdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Wang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Filesystem cache manager for Salesforce sObjects
14
+ email: johncwang@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG
20
+ - MIT-LICENSE
21
+ - README.rdoc
22
+ - Rakefile
23
+ - VERSION
24
+ - lib/salesforce_fsdb/client.rb
25
+ - lib/salesforce_fsdb/rest_client.rb
26
+ - salesforce_fsdb.gemspec
27
+ - test/test_setup.rb
28
+ homepage: http://johnwang.com/
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.1
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Salesforce Filesystem DB
52
+ test_files: []