class Google::APIClient::Service::BatchRequest

Attributes

base_batch[R]

Public Class Methods

new(service, calls, &block) click to toggle source

Creates a new batch request. This class shouldn't be instantiated directly, but rather through Service.batch.

@param [Array] calls

List of Google::APIClient::Service::Request to be made.

@param [Proc] block

Callback for every call's response. Won't be called if a call
defined a callback of its own.

@yield [Google::APIClient::Service::Result]

block to be called when result ready
# File lib/google/api_client/service/batch.rb, line 49
def initialize(service, calls, &block)
  @service = service
  @base_batch = Google::APIClient::BatchRequest.new
  @global_callback = block if block_given?

  if calls && calls.length > 0
    calls.each do |call|
      add(call)
    end
  end
end

Public Instance Methods

add(call, &block) click to toggle source

Add a new call to the batch request.

@param [Google::APIClient::Service::Request] call

the call to be added.

@param [Proc] block

callback for this call's response.

@return [Google::APIClient::Service::BatchRequest]

the BatchRequest, for chaining

@yield [Google::APIClient::Service::Result]

block to be called when result ready
# File lib/google/api_client/service/batch.rb, line 74
def add(call, &block)
  if !block_given? && @global_callback.nil?
    raise BatchError, 'Request needs a block'
  end
  callback = block || @global_callback
  base_call = {
    :api_method => call.method,
    :parameters => call.parameters
  }
  if call.respond_to? :body
    if call.body.respond_to? :to_hash
      base_call[:body_object] = call.body
    else
      base_call[:body] = call.body
    end
  end
  @base_batch.add(base_call) do |base_result|
    result = Google::APIClient::Service::BatchedCallResult.new(
        call, base_result)
    callback.call(result)
  end
  return self
end
execute() click to toggle source

Executes the batch request.

# File lib/google/api_client/service/batch.rb, line 100
def execute
  @service.execute(self)
end