@@ -35,7 +35,7 @@ class BatchedCallResponse
35
35
36
36
##
37
37
# Initialize the call response
38
- #
38
+ #
39
39
# @param [String] call_id
40
40
# UUID of the original call
41
41
# @param [Fixnum] status
@@ -48,7 +48,7 @@ def initialize(call_id, status = nil, headers = nil, body = nil)
48
48
@call_id , @status , @headers , @body = call_id , status , headers , body
49
49
end
50
50
end
51
-
51
+
52
52
# Wraps multiple API calls into a single over-the-wire HTTP request.
53
53
#
54
54
# @example
@@ -58,7 +58,7 @@ def initialize(call_id, status = nil, headers = nil, body = nil)
58
58
# batch = Google::APIClient::BatchRequest.new do |result|
59
59
# puts result.data
60
60
# end
61
- #
61
+ #
62
62
# batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/foo' })
63
63
# batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/bar' })
64
64
#
@@ -80,16 +80,17 @@ class BatchRequest < Request
80
80
# Callback for every call's response. Won't be called if a call defined
81
81
# a callback of its own.
82
82
#
83
- # @return [Google::APIClient::BatchRequest]
83
+ # @return [Google::APIClient::BatchRequest]
84
84
# The constructed object.
85
85
#
86
86
# @yield [Google::APIClient::Result]
87
87
# block to be called when result ready
88
88
def initialize ( options = { } , &block )
89
89
@calls = [ ]
90
+ @global_callback = nil
90
91
@global_callback = block if block_given?
91
92
@last_auto_id = 0
92
-
93
+
93
94
@base_id = SecureRandom . uuid
94
95
95
96
options [ :uri ] ||= 'https://www.googleapis.com/batch'
@@ -104,7 +105,7 @@ def initialize(options = {}, &block)
104
105
# automatically be generated, avoiding collisions. If duplicate call IDs
105
106
# are provided, an error will be thrown.
106
107
#
107
- # @param [Hash, Google::APIClient::Request] call
108
+ # @param [Hash, Google::APIClient::Request] call
108
109
# the call to be added.
109
110
# @param [String] call_id
110
111
# the ID to be used for this call. Must be unique
@@ -126,7 +127,7 @@ def add(call, call_id = nil, &block)
126
127
'A call with this ID already exists: %s' % call_id
127
128
end
128
129
callback = block_given? ? block : @global_callback
129
- @calls << [ call_id , call , callback ]
130
+ @calls << [ call_id , call , callback ]
130
131
return self
131
132
end
132
133
@@ -165,12 +166,12 @@ def to_http_request
165
166
if @calls . nil? || @calls . empty?
166
167
raise BatchError , 'Cannot make an empty batch request'
167
168
end
168
- parts = @calls . map { |( call_id , call , callback ) | serialize_call ( call_id , call ) }
169
+ parts = @calls . map { |( call_id , call , _callback ) | serialize_call ( call_id , call ) }
169
170
build_multipart ( parts , 'multipart/mixed' , BATCH_BOUNDARY )
170
171
super
171
172
end
172
-
173
-
173
+
174
+
174
175
protected
175
176
176
177
##
@@ -183,7 +184,7 @@ def to_http_request
183
184
# @param [Hash] headers
184
185
# the hash of headers and their values.
185
186
#
186
- # @return [String]
187
+ # @return [String]
187
188
# the value of the desired header.
188
189
def find_header ( name , headers )
189
190
_ , header = headers . detect do |h , v |
@@ -197,7 +198,7 @@ def find_header(name, headers)
197
198
#
198
199
# @api private
199
200
#
200
- # @return [String]
201
+ # @return [String]
201
202
# the new, unique ID.
202
203
def new_id
203
204
@last_auto_id += 1
@@ -216,15 +217,15 @@ def new_id
216
217
# @param [String] header
217
218
# Content-ID header value.
218
219
#
219
- # @return [String]
220
+ # @return [String]
220
221
# The extracted ID value.
221
222
def header_to_id ( header )
222
223
if !header . start_with? ( '<' ) || !header . end_with? ( '>' ) ||
223
224
!header . include? ( '+' )
224
225
raise BatchError , 'Invalid value for Content-ID: "%s"' % header
225
226
end
226
227
227
- base , call_id = header [ 1 ...-1 ] . split ( '+' )
228
+ _base , call_id = header [ 1 ...-1 ] . split ( '+' )
228
229
return Addressable ::URI . unencode ( call_id )
229
230
end
230
231
@@ -236,7 +237,7 @@ def header_to_id(header)
236
237
# @param [String] response
237
238
# the response to parse.
238
239
#
239
- # @return [Array<Hash>, String]
240
+ # @return [Array<Hash>, String]
240
241
# the headers and the body, separately.
241
242
def split_headers_and_body ( response )
242
243
headers = { }
@@ -263,12 +264,12 @@ def split_headers_and_body(response)
263
264
# @param [String] call_response
264
265
# the request to deserialize.
265
266
#
266
- # @return [Google::APIClient::BatchedCallResponse]
267
+ # @return [Google::APIClient::BatchedCallResponse]
267
268
# the parsed and converted response.
268
269
def deserialize_call_response ( call_response )
269
270
outer_headers , outer_body = split_headers_and_body ( call_response )
270
271
status_line , payload = outer_body . split ( "\n " , 2 )
271
- protocol , status , reason = status_line . split ( ' ' , 3 )
272
+ _protocol , status , _reason = status_line . split ( ' ' , 3 )
272
273
273
274
headers , body = split_headers_and_body ( payload )
274
275
content_id = find_header ( 'Content-ID' , outer_headers )
@@ -284,7 +285,7 @@ def deserialize_call_response(call_response)
284
285
# @param [Google::APIClient::Request] call
285
286
# the call to serialize.
286
287
#
287
- # @return [Faraday::UploadIO]
288
+ # @return [Faraday::UploadIO]
288
289
# the serialized request
289
290
def serialize_call ( call_id , call )
290
291
method , uri , headers , body = call . to_http_request
@@ -293,7 +294,7 @@ def serialize_call(call_id, call)
293
294
request << "\r \n %s: %s" % [ header , value ]
294
295
end
295
296
if body
296
- # TODO - CompositeIO if body is a stream
297
+ # TODO - CompositeIO if body is a stream
297
298
request << "\r \n \r \n "
298
299
if body . respond_to? ( :read )
299
300
request << body . read
@@ -303,7 +304,7 @@ def serialize_call(call_id, call)
303
304
end
304
305
Faraday ::UploadIO . new ( StringIO . new ( request ) , 'application/http' , 'ruby-api-request' , 'Content-ID' => id_to_header ( call_id ) )
305
306
end
306
-
307
+
307
308
##
308
309
# Convert an id to a Content-ID header value.
309
310
#
@@ -319,7 +320,7 @@ def serialize_call(call_id, call)
319
320
def id_to_header ( call_id )
320
321
return '<%s+%s>' % [ @base_id , Addressable ::URI . encode ( call_id ) ]
321
322
end
322
-
323
+
323
324
end
324
325
end
325
326
end
0 commit comments