21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / lib / google / api_client / service / batch.rb
1 # Copyright 2013 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 require 'google/api_client/service/result'
16 require 'google/api_client/batch'
17
18 module Google
19   class APIClient
20     class Service
21
22       ##
23       # Helper class to contain the result of an individual batched call.
24       #
25       class BatchedCallResult < Result
26         # @return [Fixnum] Index of the call
27         def call_index
28           return @base_result.response.call_id.to_i - 1
29         end
30       end
31
32       ##
33       #
34       #
35       class BatchRequest
36         ##
37         # Creates a new batch request.
38         # This class shouldn't be instantiated directly, but rather through
39         # Service.batch.
40         #
41         # @param [Array] calls
42         #   List of Google::APIClient::Service::Request to be made.
43         # @param [Proc] block
44         #   Callback for every call's response. Won't be called if a call
45         #   defined a callback of its own.
46         #
47         # @yield [Google::APIClient::Service::Result]
48         #   block to be called when result ready
49         def initialize(service, calls, &block)
50           @service = service
51           @base_batch = Google::APIClient::BatchRequest.new
52           @global_callback = block if block_given?
53
54           if calls && calls.length > 0
55             calls.each do |call|
56               add(call)
57             end
58           end
59         end
60
61         ##
62         # Add a new call to the batch request.
63         #
64         # @param [Google::APIClient::Service::Request] call
65         #   the call to be added.
66         # @param [Proc] block
67         #   callback for this call's response.
68         #
69         # @return [Google::APIClient::Service::BatchRequest]
70         #   the BatchRequest, for chaining
71         #
72         # @yield [Google::APIClient::Service::Result]
73         #   block to be called when result ready
74         def add(call, &block)
75           if !block_given? && @global_callback.nil?
76             raise BatchError, 'Request needs a block'
77           end
78           callback = block || @global_callback
79           base_call = {
80             :api_method => call.method,
81             :parameters => call.parameters
82           }
83           if call.respond_to? :body
84             if call.body.respond_to? :to_hash
85               base_call[:body_object] = call.body
86             else
87               base_call[:body] = call.body
88             end
89           end
90           @base_batch.add(base_call) do |base_result|
91             result = Google::APIClient::Service::BatchedCallResult.new(
92                 call, base_result)
93             callback.call(result)
94           end
95           return self
96         end
97
98         ##
99         # Executes the batch request.
100         def execute
101           @service.execute(self)
102         end
103
104         attr_reader :base_batch
105
106       end
107
108     end
109   end
110 end