class ActiveForce::Bulk::Job

Constants

OPERATIONS
STATES

Attributes

content_url[RW]
id[RW]
object[RW]
operation[RW]
records[R]
state[RW]

Public Class Methods

new(operation:, object:, id: nil, records: nil) click to toggle source
# File lib/active_force/bulk/job.rb, line 21
def initialize(operation:, object:, id: nil, records: nil)
  @operation = operation.to_sym
  @object = object
  @id = id
  @records = records
  @state = nil
  @content_url = nil
  initialize_state_methods
end
run(...) click to toggle source
# File lib/active_force/bulk/job.rb, line 48
def self.run(...)
  job = new(...)
  job.create
  job.upload
  job.run
  job
end

Public Instance Methods

abort() click to toggle source
# File lib/active_force/bulk/job.rb, line 74
def abort
  change_state(STATES[:Aborted])
end
create() click to toggle source
# File lib/active_force/bulk/job.rb, line 31
def create
  request_body = default_job_options.merge(operation: operation, object: object)
  response = client.post("#{ingest_path}/", request_body)
  update_attributes_from(response)
  response
end
delete() click to toggle source
# File lib/active_force/bulk/job.rb, line 78
def delete
  response = client.delete("#{ingest_path}/#{id}")
  response
end
failed_results() click to toggle source
# File lib/active_force/bulk/job.rb, line 56
def failed_results
  client.get("#{ingest_path}/#{id}/failedResults/")
end
finished?() click to toggle source
# File lib/active_force/bulk/job.rb, line 83
def finished?
  job_complete? || failed? || aborted?
end
info() click to toggle source
# File lib/active_force/bulk/job.rb, line 64
def info
  response = client.get("#{ingest_path}/#{id}")
  update_attributes_from(response)
  response
end
result() click to toggle source
# File lib/active_force/bulk/job.rb, line 44
def result
  ActiveForce::Bulk::JobResult.new(job: self)
end
run() click to toggle source
# File lib/active_force/bulk/job.rb, line 70
def run
  change_state(STATES[:UploadComplete])
end
successful_results() click to toggle source
# File lib/active_force/bulk/job.rb, line 60
def successful_results
  client.get("#{ingest_path}/#{id}/successfulResults/")
end
upload() click to toggle source
# File lib/active_force/bulk/job.rb, line 38
def upload
  headers = {"Content-Type": 'text/csv'}
  response = client.put(content_url, records.to_csv, headers)
  response
end

Private Instance Methods

change_state(value) click to toggle source
# File lib/active_force/bulk/job.rb, line 107
def change_state(value)
  request_body = {state: value}
  headers = {"Content-Type": "application/json"}
  response = client.patch("#{ingest_path}/#{id}", request_body, headers)
  update_attributes_from(response)
  response
end
client() click to toggle source
# File lib/active_force/bulk/job.rb, line 95
def client
  @client ||= ActiveForce.sfdc_client
end
default_job_options() click to toggle source
# File lib/active_force/bulk/job.rb, line 99
def default_job_options
  {
    columnDelimiter: 'COMMA',
    contentType: 'CSV',
    lineEnding: 'LF',
  }
end
ingest_path() click to toggle source
# File lib/active_force/bulk/job.rb, line 91
def ingest_path
  "/services/data/v#{client.options[:api_version]}/jobs/ingest"
end
initialize_state_methods() click to toggle source

Defines question methods for various states of the job, e.g. open?, in_progress?, etc.

# File lib/active_force/bulk/job.rb, line 124
      def initialize_state_methods
        STATES.values.each do |state|
          state_method = <<-STATE_METHOD
            def #{state.to_s.underscore}?
              @state == '#{state}'
            end
          STATE_METHOD
          self.class_eval(state_method)
        end
      end
update_attributes_from(response) click to toggle source
# File lib/active_force/bulk/job.rb, line 115
def update_attributes_from(response)
  return unless response.body.present?

  %i[id state object operation contentUrl].each do |attr|
    self.send("#{attr.to_s.underscore}=", response.body[attr.to_s]) if response.body[attr.to_s].present?
  end
end