class SalesforceBulkQuery::Api

Abstracts the whole library, class the user interacts with

Constants

CHECK_INTERVAL

Public Class Methods

new(client, options={}) click to toggle source

Constructor @param client [Restforce] An instance of the Restforce client, that is used internally to access Salesforce api @param options

# File lib/salesforce_bulk_query.rb, line 23
def initialize(client, options={})
  @logger = options[:logger]

  api_version = options[:api_version] || @@DEFAULT_API_VERSION
  ssl_version = options[:ssl_version]
  # use our own logging middleware if logger passed
  if @logger && client.respond_to?(:middleware)
    client.middleware.use(SalesforceBulkQuery::Logger, @logger, options)
  end

  # initialize connection
  @connection = SalesforceBulkQuery::Connection.new(client, api_version, @logger, options[:filename_prefix],ssl_version)
end

Public Instance Methods

instance_url() click to toggle source

Get the Salesforce instance URL

# File lib/salesforce_bulk_query.rb, line 38
def instance_url
  # make sure it ends with /
  url = @connection.client.instance_url
  url += '/' if url[-1] != '/'
  return url
end
query(sobject, soql, options={}) click to toggle source

Query the Salesforce API. It's a blocking method - waits until the query is resolved can take quite some time @param sobject Salesforce object, e.g. “Opportunity” @param soql SOQL query, e.g. “SELECT Name FROM Opportunity” @return hash with :filenames and other useful stuff

# File lib/salesforce_bulk_query.rb, line 52
def query(sobject, soql, options={})
  @logger.info "Running query: #{soql}. Gem version salesforce_bulk_query: #{SalesforceBulkQuery::VERSION}" if @logger
  check_interval = options[:check_interval] || CHECK_INTERVAL
  time_limit = options[:time_limit] # in seconds

  start_time = Time.now

  # start the machinery
  query = start_query(sobject, soql, options)
  results = nil

  loop do
    # get available results and check the status
    results = query.get_available_results(options)
    @logger.debug "get_available_results: #{results}"

    # if finished get the result and we're done
    if results[:succeeded]

      # we're done
      @logger.info "Query succeeded. Results: #{results}" if @logger
      break
    end

    # if we've run out of time limit, go away
    if time_limit && (Time.now - start_time > time_limit)
      @logger.warn "Ran out of time limit, downloading what's available and terminating" if @logger

      @logger.info "Downloaded the following files: #{results[:filenames]} The following didn't finish in time: #{results[:unfinished_subqueries]}." if @logger
      break
    end

    @logger.info "Sleeping #{check_interval}" if @logger
    @logger.info "Downloaded files: #{results[:filenames].length} Jobs in progress: #{query.jobs_in_progress.length}"
    sleep(check_interval)
  end

  # nice list of files to log
  if @logger && ! results[:filenames].empty?

    @logger.info "Download finished. Downloaded files in #{File.dirname(results[:filenames][0])}. Filename size [line count]:"
    @logger.info "\n" + results[:filenames].sort.map{|f| "#{File.basename(f)} #{File.size(f)} #{Utils.line_count(f) if options[:count_lines]}"}.join("\n")
  end
  return results
end
start_query(sobject, soql, options={}) click to toggle source

Start the query (synchronous method) @params see query @return Query instance with the running query

# File lib/salesforce_bulk_query.rb, line 101
def start_query(sobject, soql, options={})
  # create the query, start it and return it
  query = SalesforceBulkQuery::Query.new(sobject, soql, @connection, {:logger => @logger}.merge(options))
  query.start(options)
  return query
end