Fixed issue with header/options ambiguity.

This commit is contained in:
Bob Aman 2011-08-02 16:20:54 -04:00
parent a281c1bcf5
commit 0d2cbe26df
2 changed files with 18 additions and 8 deletions

View File

@ -533,14 +533,10 @@ module Google
# ) # )
def execute(*params) def execute(*params)
# This block of code allows us to accept multiple parameter passing # This block of code allows us to accept multiple parameter passing
# styles, and maintaining backwards compatibility. # styles, and maintaining some backwards compatibility.
if params.last.respond_to?(:to_hash) && params.size != 2 #
# Hash options are tricky. If we get two arguments, it's ambiguous # Note: I'm extremely tempted to deprecate this style of execute call.
# whether to treat them as API parameters or Hash options, but since if params.last.respond_to?(:to_hash) && params.size == 1
# it's rare to need to pass in options, we must assume that the
# developer wanted to pass API parameters. Prefer using named
# parameters to avoid this issue. Unnamed parameters should be
# considered syntactic sugar.
options = params.pop options = params.pop
else else
options = {} options = {}

View File

@ -14,6 +14,7 @@
require 'spec_helper' require 'spec_helper'
require 'json'
require 'signet/oauth_1/client' require 'signet/oauth_1/client'
require 'httpadapter/adapters/net_http' require 'httpadapter/adapters/net_http'
@ -238,6 +239,19 @@ describe Google::APIClient do
) )
end).should raise_error(Google::APIClient::ClientError) end).should raise_error(Google::APIClient::ClientError)
end end
it 'should correctly handle unnamed parameters' do
@client.authorization = :oauth_2
@client.authorization.access_token = '12345'
result = @client.execute(
@prediction.training.insert,
{},
JSON.generate({"id" => "bucket/object"}),
{'Content-Type' => 'application/json'}
)
method, uri, headers, body = result.request
Hash[headers]['Content-Type'].should == 'application/json'
end
end end
describe 'with the buzz API' do describe 'with the buzz API' do