Class: Google::APIClient::Service::BatchRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/google/api_client/service/batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, calls, &block) {|Google::APIClient::Service::Result| ... } ⇒ BatchRequest

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

Parameters:

  • calls (Array)

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

  • block (Proc)

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

Yields:



49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#base_batchObject (readonly)

Returns the value of attribute base_batch.



104
105
106
# File 'lib/google/api_client/service/batch.rb', line 104

def base_batch
  @base_batch
end

Instance Method Details

#add(call, &block) {|Google::APIClient::Service::Result| ... } ⇒ Google::APIClient::Service::BatchRequest

Add a new call to the batch request.

Parameters:

Yields:

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#executeObject

Executes the batch request.



100
101
102
# File 'lib/google/api_client/service/batch.rb', line 100

def execute
  @service.execute(self)
end