Skip to content
This repository has been archived by the owner on Aug 22, 2021. It is now read-only.

Commit

Permalink
Fixe class variables
Browse files Browse the repository at this point in the history
  • Loading branch information
yatish27coupa committed May 20, 2016
1 parent c46695a commit d60f487
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 36 deletions.
File renamed without changes.
17 changes: 11 additions & 6 deletions lib/salesforce_bulk_api.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
require 'rubygems'
require 'bundler'
Bundler.require()
require 'salesforce_bulk_api/version'
require 'net/https'
require 'xmlsimple'
require 'csv'

require 'salesforce_bulk_api/version'
require 'salesforce_bulk_api/concerns/throttling'
require 'salesforce_bulk_api/job'
require 'salesforce_bulk_api/connection'

module SalesforceBulkApi

class Api
attr_reader :connection

@@SALESFORCE_API_VERSION = '32.0'
SALESFORCE_API_VERSION = '32.0'

def initialize(client)
@connection = SalesforceBulkApi::Connection.new(@@SALESFORCE_API_VERSION, client)
@connection = SalesforceBulkApi::Connection.new(SALESFORCE_API_VERSION, client)
@listeners = { job_created: [] }
end

Expand Down Expand Up @@ -70,7 +69,13 @@ def job_from_id(job_id)
def do_operation(operation, sobject, records, external_field, get_response, timeout, batch_size, send_nulls = false, no_null_list = [])
count operation.to_sym

job = SalesforceBulkApi::Job.new(operation: operation, sobject: sobject, records: records, external_field: external_field, connection: @connection)
job = SalesforceBulkApi::Job.new(
operation: operation,
sobject: sobject,
records: records,
external_field: external_field,
connection: @connection
)

job.create_job(batch_size, send_nulls, no_null_list)
@listeners[:job_created].each {|callback| callback.call(job)}
Expand Down
41 changes: 17 additions & 24 deletions lib/salesforce_bulk_api/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@ module SalesforceBulkApi
class Connection
include Concerns::Throttling

@@XML_HEADER = '<?xml version="1.0" encoding="utf-8" ?>'
@@API_VERSION = nil
@@LOGIN_HOST = 'login.salesforce.com'
@@INSTANCE_HOST = nil # Gets set in login()

def initialize(api_version,client)
@client=client
@session_id = nil
@server_url = nil
@instance = nil
@@API_VERSION = api_version
@@LOGIN_PATH = "/services/Soap/u/#{@@API_VERSION}"
@@PATH_PREFIX = "/services/async/#{@@API_VERSION}/"
LOGIN_HOST = 'login.salesforce.com'

def initialize(api_version, client)
@client = client
@api_version = api_version
@path_prefix = "/services/async/#{@api_version}/"

login()
end
Expand All @@ -25,21 +18,21 @@ def login()
client_type = @client.class.to_s
case client_type
when "Restforce::Data::Client"
@session_id=@client.options[:oauth_token]
@server_url=@client.options[:instance_url]
@session_id = @client.options[:oauth_token]
@server_url = @client.options[:instance_url]
else
@session_id=@client.oauth_token
@server_url=@client.instance_url
@session_id = @client.oauth_token
@server_url = @client.instance_url
end
@instance = parse_instance()
@@INSTANCE_HOST = "#{@instance}.salesforce.com"
@instance_host = "#{@instance}.salesforce.com"
end

def post_xml(host, path, xml, headers)
host = host || @@INSTANCE_HOST
if host != @@LOGIN_HOST # Not login, need to add session id to header
host = host || @instance_host
if host != LOGIN_HOST # Not login, need to add session id to header
headers['X-SFDC-Session'] = @session_id
path = "#{@@PATH_PREFIX}#{path}"
path = "#{@path_prefix}#{path}"
end
i = 0
begin
Expand All @@ -59,9 +52,9 @@ def post_xml(host, path, xml, headers)
end

def get_request(host, path, headers)
host = host || @@INSTANCE_HOST
path = "#{@@PATH_PREFIX}#{path}"
if host != @@LOGIN_HOST # Not login, need to add session id to header
host = host || @instance_host
path = "#{@path_prefix}#{path}"
if host != LOGIN_HOST # Not login, need to add session id to header
headers['X-SFDC-Session'] = @session_id;
end

Expand Down
8 changes: 3 additions & 5 deletions lib/salesforce_bulk_api/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def initialize(args)
@XML_HEADER = '<?xml version="1.0" encoding="utf-8" ?>'
end



def create_job(batch_size, send_nulls, no_null_list)
@batch_size = batch_size
@send_nulls = send_nulls
Expand All @@ -26,7 +24,8 @@ def create_job(batch_size, send_nulls, no_null_list)
xml = "#{@XML_HEADER}<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">"
xml += "<operation>#{@operation}</operation>"
xml += "<object>#{@sobject}</object>"
if !@external_field.nil? # This only happens on upsert
# This only happens on upsert
if !@external_field.nil?
xml += "<externalIdFieldName>#{@external_field}</externalIdFieldName>"
end
xml += "<contentType>XML</contentType>"
Expand All @@ -42,7 +41,6 @@ def create_job(batch_size, send_nulls, no_null_list)
raise SalesforceException.new("#{response_parsed['exceptionMessage'][0]} (#{response_parsed['exceptionCode'][0]})") if response_parsed['exceptionCode']

@job_id = response_parsed['id'][0]

end

def close_job()
Expand Down Expand Up @@ -74,7 +72,7 @@ def add_batches
@records_dup = @records.clone

super_records = []
(@records_dup.size/@batch_size).to_i.times do
(@records_dup.size / @batch_size).to_i.times do
super_records << @records_dup.pop(@batch_size)
end
super_records << @records_dup unless @records_dup.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/salesforce_bulk_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SalesforceBulkApi
VERSION = '0.0.12'
VERSION = '0.0.13'
end

0 comments on commit d60f487

Please sign in to comment.