executrix 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ M2I4ZDNlY2E3NzEyMjNiOThiMDFjMTg0Yjk0ZmM0ZDIxOThlOTgxNw==
5
+ data.tar.gz: !binary |-
6
+ MzllZDVhODY1NDNhNTY4ZmIwMmE0M2NjZmM1NWNhZDA1NTE4MzA2Nw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NDg4ODI3NWZhYjU0MjZiNWE5MzIwYmNjOTg0YTUzODE5ODlkYmMxMjMxODE3
10
+ MTVjYWU5ZGQxZjIwNzVkNzBiZmQ4ZWJlNGU5OGE1MWEyZDY4Nzk5NTQ1ZjRm
11
+ NzRlYjgwYzA3YWFmOTQ3ZmMwMTcwYzY5MDgyMjdkMzFjYTQ3OTM=
12
+ data.tar.gz: !binary |-
13
+ NjM0ZjQ3YTg1MWJiNDBmYWM2YWE2YTI2ZTZiNmY3YzYyZGI1ZDBhOGEwYmIx
14
+ M2RkZTRlOTQ2ODI1Y2E3ZDhjOWIwZjEwNDc1M2QzOGY5MDRjZWY0MWY2M2Q3
15
+ ZDU3MjRmYWVkOWM2YmViYTNhYzBhMjk0YTZiMDEyNTJjMzdlZjM=
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ /vendor/bundle
6
+ /tmp
7
+ /coverage
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --tty --color --format documentation
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p392
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ bundler_args: --without documentation production
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - jruby-19mode
7
+ - rbx-19mode
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: rbx-19mode
11
+ script:
12
+ - rake spec
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test, :development do
6
+ gem 'coveralls', require: false
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+
10
+ gem 'rb-inotify', require: false
11
+ gem 'rb-fsevent', require: false
12
+ gem 'rb-fchange', require: false
13
+ end
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2012 Jorge Valdivia
3
+ Copyright (c) 2013 Leif Gensert, Propertybase GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # Executrix
2
+
3
+ DISCLAIMER: This gem is a rewrite of the [salesforce_bulk](https://github.com/jorgevaldivia/salesforce_bulk) gem. As the original maintainer didn't respon to my [pull-request](https://github.com/jorgevaldivia/salesforce_bulk/pull/14) I decided to rerelease the gem under different name. This is it.
4
+
5
+ The original Copyright Notice and all the original commit logs have been retained.
6
+
7
+ ## Overview
8
+
9
+ Executrix is a simple ruby gem for connecting to and using the [Salesforce Bulk API](http://www.salesforce.com/us/developer/docs/api_asynch/index.htm). This gem only supports the functionality provided by the bulk API.
10
+
11
+ ## Installation
12
+
13
+ ~~~ sh
14
+ $ sudo gem install executrix
15
+ ~~~
16
+
17
+ ## How to use
18
+
19
+ Using this gem is simple and straight forward.
20
+
21
+ ### Initialize
22
+
23
+ ~~~ ruby
24
+ require 'executrix'
25
+ salesforce = Executrix::Api.new('YOUR_SALESFORCE_USERNAME', 'YOUR_SALESFORCE_PASSWORD+YOUR_SALESFORCE_TOKEN')
26
+ ~~~
27
+
28
+ To use sandbox:
29
+
30
+ ~~~ ruby
31
+ salesforce = Executrix::Api.new('YOUR_SALESFORCE_SANDBOX_USERNAME', 'YOUR_SALESFORCE_PASSWORD+YOUR_SALESFORCE_SANDBOX_TOKEN', true)
32
+ ~~~
33
+
34
+ Note: the second parameter is a combination of your Salesforce token and password. So if your password is xxxx and your token is yyyy, the second parameter will be xxxxyyyy
35
+
36
+ ### Operations
37
+
38
+ ~~~ ruby
39
+ # Insert
40
+ new_account = {'name' => 'Test Account', 'type' => 'Other'} # Add as many fields per record as needed.
41
+ records_to_insert = []
42
+ records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
43
+ result = salesforce.insert('Account', records_to_insert)
44
+ puts "reference to the bulk job: #{result.inspect}"
45
+ ~~~
46
+
47
+ ~~~ ruby
48
+ # Update
49
+ updated_account = {'name' => 'Test Account -- Updated', 'id' => 'a00A0001009zA2m'} # Nearly identical to an insert, but we need to pass the salesforce id.
50
+ records_to_update = []
51
+ records_to_update.push(updated_account)
52
+ salesforce.update('Account', records_to_update)
53
+ ~~~
54
+
55
+ ~~~ ruby
56
+ # Upsert
57
+ upserted_account = {'name' => 'Test Account -- Upserted', 'External_Field_Name' => '123456'} # Fields to be updated. External field must be included
58
+ records_to_upsert = []
59
+ records_to_upsert.push(upserted_account)
60
+ salesforce.upsert('Account', records_to_upsert, 'External_Field_Name') # Note that upsert accepts an extra parameter for the external field name
61
+ ~~~
62
+
63
+ ~~~ ruby
64
+ # Delete
65
+ deleted_account = {'id' => 'a00A0001009zA2m'} # We only specify the id of the records to delete
66
+ records_to_delete = []
67
+ records_to_delete.push(deleted_account)
68
+ salesforce.delete('Account', records_to_delete)
69
+ ~~~
70
+
71
+ ~~~ ruby
72
+ # Query
73
+ res = salesforce.query('Account', 'select id, name, createddate from Account limit 3') # We just need to pass the sobject name and the query string
74
+ puts res.result.records.inspect
75
+ ~~~
76
+
77
+ ### Query status
78
+
79
+ The above examples all return immediately after sending the data to the Bulk API. If you want to wait, until the batch finished, call the final_status method on the batch-reference.
80
+
81
+ ~~~ ruby
82
+ new_account = {'name' => 'Test Account', 'type' => 'Other'} # Add as many fields per record as needed.
83
+ records_to_insert = []
84
+ records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
85
+ batch_reference = salesforce.insert('Account', records_to_insert)
86
+ results = batch_reference.final_status
87
+ puts "the results: #{results.inspect}"
88
+ ~~~
89
+
90
+ Additionally you cann pass in a block to query the current state of the batch job:
91
+
92
+ ~~~ ruby
93
+ new_account = {'name' => 'Test Account', 'type' => 'Other'} # Add as many fields per record as needed.
94
+ records_to_insert = []
95
+ records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
96
+ batch_reference = salesforce.insert('Account', records_to_insert)
97
+ results = batch_reference.final_status do |status|
98
+ puts "running: #{status.inspect}"
99
+ end
100
+ puts "the results: #{results.inspect}"
101
+ ~~~
102
+
103
+ The block will yield every 2 seconds, but you can also specify the poll interval:
104
+
105
+ ~~~ ruby
106
+ new_account = {'name' => 'Test Account', 'type' => 'Other'} # Add as many fields per record as needed.
107
+ records_to_insert = []
108
+ records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
109
+ batch_reference = salesforce.insert('Account', records_to_insert)
110
+ poll_interval = 10
111
+ results = batch_reference.final_status(poll_interval) do |status|
112
+ puts "running: #{status.inspect}"
113
+ end
114
+ puts "the results: #{results.inspect}"
115
+ ~~~
116
+
117
+ ## Copyright
118
+
119
+ Copyright (c) 2012 Jorge Valdivia.
120
+ Copyright (c) 2013 Leif Gensert, [Propertybase GmbH](http://propertybase.com)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
data/executrix.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'executrix/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'executrix'
8
+ gem.version = Executrix::VERSION
9
+ gem.authors = ["Jorge Valdivia"]
10
+ gem.email = ["jorge@valdivia.me"]
11
+ gem.homepage = 'https://github.com/propertybase/executrix'
12
+ gem.summary = %q{Ruby support for the Salesforce Bulk API}
13
+ gem.description = %q{This gem provides a super simple interface for the Salesforce Bulk API. It provides support for insert, update, upsert, delete, and query.}
14
+
15
+ gem.rubyforge_project = 'executrix'
16
+
17
+ gem.files = `git ls-files`.split("\n")
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency 'rake'
23
+ gem.add_dependency 'nori', '~> 2.0'
24
+ gem.add_dependency 'nokogiri', '~> 1.5'
25
+ gem.add_development_dependency 'rspec', '~> 2.13'
26
+ gem.add_development_dependency 'webmock', '~> 1.11'
27
+ end
data/lib/executrix.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'executrix/version'
2
+ require 'executrix/helper'
3
+ require 'executrix/batch'
4
+ require 'executrix/http'
5
+ require 'executrix/connection'
6
+
7
+ module Executrix
8
+ class Api
9
+ SALESFORCE_API_VERSION = '27.0'
10
+
11
+ def initialize(username, password, sandbox = false, api_version = SALESFORCE_API_VERSION)
12
+ @connection = Executrix::Connection.connect(
13
+ username,
14
+ password,
15
+ api_version,
16
+ sandbox)
17
+ end
18
+
19
+ def upsert(sobject, records, external_field)
20
+ start_job('upsert', sobject, records, external_field)
21
+ end
22
+
23
+ def update(sobject, records)
24
+ start_job('update', sobject, records)
25
+ end
26
+
27
+ def insert(sobject, records)
28
+ start_job('insert', sobject, records)
29
+ end
30
+
31
+ def delete(sobject, records)
32
+ start_job('delete', sobject, records)
33
+ end
34
+
35
+ def query(sobject, query)
36
+ job_id = @connection.create_job(
37
+ 'query',
38
+ sobject,
39
+ nil)
40
+ batch_id = @connection.add_query(job_id, query)
41
+ @connection.close_job job_id
42
+ batch_reference = Executrix::Batch.new @connection, job_id, batch_id
43
+ batch_reference.init_result_id
44
+ batch_reference.final_status
45
+ end
46
+
47
+ private
48
+ def start_job(operation, sobject, records, external_field=nil)
49
+ job_id = @connection.create_job(
50
+ operation,
51
+ sobject,
52
+ external_field)
53
+ batch_id = @connection.add_batch job_id, records
54
+ @connection.close_job job_id
55
+ Executrix::Batch.new @connection, job_id, batch_id
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,51 @@
1
+ module Executrix
2
+ class Batch
3
+ def initialize connection, job_id, batch_id
4
+ @connection = connection
5
+ @job_id = job_id
6
+ @batch_id = batch_id
7
+
8
+ if @batch_id == -1
9
+ @final_status = {
10
+ state: 'Completed',
11
+ state_message: 'Empty Request'
12
+ }
13
+ end
14
+ end
15
+
16
+ def final_status poll_interval=2
17
+ return @final_status if @final_status
18
+
19
+ @final_status = self.status
20
+ while ['Queued', 'InProgress'].include?(@final_status[:state])
21
+ sleep poll_interval
22
+ @final_status = self.status
23
+ yield @final_status if block_given?
24
+ end
25
+
26
+ raise @final_status[:state_message] if @final_status[:state] == 'Failed'
27
+
28
+ @final_status.merge({
29
+ results: results
30
+ })
31
+ end
32
+
33
+ def status
34
+ @connection.query_batch @job_id, @batch_id
35
+ end
36
+
37
+ # only needed for query
38
+ def init_result_id
39
+ max_retries = 5
40
+ retry_count = 0
41
+ while @result_id.nil? && retry_count < max_retries
42
+ @result_id = @connection.query_batch_result_id(@job_id, @batch_id)[:result]
43
+ retry_count += 1
44
+ end
45
+ end
46
+
47
+ def results
48
+ @connection.query_batch_result_data(@job_id, @batch_id, @result_id)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,95 @@
1
+ module Executrix
2
+ class Connection
3
+ def initialize(username, password, api_version, sandbox)
4
+ @username = username
5
+ @password = password
6
+ @api_version = api_version
7
+ @sandbox = sandbox
8
+ end
9
+
10
+ def login
11
+ response = Executrix::Http.login(
12
+ @sandbox,
13
+ @username,
14
+ @password,
15
+ @api_version)
16
+
17
+ @session_id = response[:session_id]
18
+ @instance = response[:instance]
19
+ self
20
+ end
21
+
22
+ def create_job operation, sobject, external_field
23
+ Executrix::Http.create_job(
24
+ @instance,
25
+ @session_id,
26
+ operation,
27
+ sobject,
28
+ @api_version,
29
+ external_field)[:id]
30
+ end
31
+
32
+ def close_job job_id
33
+ Executrix::Http.close_job(
34
+ @instance,
35
+ @session_id,
36
+ job_id,
37
+ @api_version)[:id]
38
+ end
39
+
40
+ def add_query job_id, data_or_soql
41
+ Executrix::Http.add_batch(
42
+ @instance,
43
+ @session_id,
44
+ job_id,
45
+ data_or_soql,
46
+ @api_version)[:id]
47
+ end
48
+
49
+ def query_batch job_id, batch_id
50
+ Executrix::Http.query_batch(
51
+ @instance,
52
+ @session_id,
53
+ job_id,
54
+ batch_id,
55
+ @api_version,
56
+ )
57
+ end
58
+
59
+ def query_batch_result_id job_id, batch_id
60
+ Executrix::Http.query_batch_result_id(
61
+ @instance,
62
+ @session_id,
63
+ job_id,
64
+ batch_id,
65
+ @api_version,
66
+ )
67
+ end
68
+
69
+ def query_batch_result_data job_id, batch_id, result_id
70
+ Executrix::Http.query_batch_result_data(
71
+ @instance,
72
+ @session_id,
73
+ job_id,
74
+ batch_id,
75
+ result_id,
76
+ @api_version,
77
+ )
78
+ end
79
+
80
+ def add_batch job_id, records
81
+ return -1 if records.nil? || records.empty?
82
+
83
+ Executrix::Http.add_batch(
84
+ @instance,
85
+ @session_id,
86
+ job_id,
87
+ Executrix::Helper.records_to_csv(records),
88
+ @api_version)[:id]
89
+ end
90
+
91
+ def self.connect(username, password, api_version, sandbox)
92
+ self.new(username, password, api_version, sandbox).login
93
+ end
94
+ end
95
+ end