Fixed issue with invalid type checking.

git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@51 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
Bob Aman 2010-10-09 02:41:38 +00:00
parent 638f8a810c
commit e50442091c
3 changed files with 90 additions and 10 deletions

View File

@ -265,13 +265,13 @@ module Google
api_method = self.discovered_method(
api_method.to_s, options[:service_version]
)
elsif !api_method.kind_of?(::Google::APIClient::Service)
elsif !api_method.kind_of?(::Google::APIClient::Method)
raise TypeError,
"Expected String, Symbol, or Google::APIClient::Service, " +
"Expected String, Symbol, or Google::APIClient::Method, " +
"got #{api_method.class}."
end
unless api_method
raise ArgumentError, "API method does not exist."
raise ArgumentError, "API method could not be found."
end
request = api_method.generate_request(parameters, body, headers)
if options[:signed]

View File

@ -377,16 +377,74 @@ module Google
#
# @param [Hash, Array] parameters
# The parameters to send.
# @param [String] body The body for the HTTP request.
# @param [String, StringIO] body The body for the HTTP request.
# @param [Hash, Array] headers The HTTP headers for the request.
#
# @return [Array] The generated HTTP request.
def generate_request(parameters={}, body='', headers=[])
if body.respond_to?(:string)
body = body.string
elsif body.respond_to?(:to_str)
body = body.to_str
else
raise TypeError, "Expected String or StringIO, got #{body.class}."
end
if !headers.kind_of?(Array) && !headers.kind_of?(Hash)
raise TypeError, "Expected Hash or Array, got #{headers.class}."
end
method = self.description['httpMethod'] || 'GET'
uri = self.generate_uri(parameters)
headers = headers.to_a if headers.kind_of?(Hash)
return [method, uri.to_str, headers, [body]]
end
##
# Returns a <code>Hash</code> of the parameter descriptions for
# this method.
#
# @return [Hash] The parameter descriptions.
def parameter_descriptions
@parameter_descriptions ||= Hash[self.description['parameters'] || {}]
end
##
# Returns an <code>Array</code> of the parameters for this method.
#
# @return [Array] The parameters.
def parameters
@parameters ||= Hash[self.description['parameters'] || {}].keys
end
##
# Returns an <code>Array</code> of the required parameters for this
# method.
#
# @return [Array] The required parameters.
#
# @example
# # A list of all required parameters.
# method.required_parameters
def required_parameters
@required_parameters ||= Hash[self.parameter_descriptions.select do |k, v|
v['required']
end].keys
end
##
# Returns an <code>Array</code> of the optional parameters for this
# method.
#
# @return [Array] The optional parameters.
#
# @example
# # A list of all optional parameters.
# method.optional_parameters
def optional_parameters
@optional_parameters ||= Hash[self.parameter_descriptions.reject do |k, v|
v['required']
end].keys
end
##
# Verifies that the parameters are valid for this method. Raises an
# exception if validation fails.
@ -397,8 +455,7 @@ module Google
# @return [NilClass] <code>nil</code> if validation passes.
def validate_parameters(parameters={})
parameters = self.normalize_parameters(parameters)
parameter_description = self.description['parameters'] || {}
required_variables = Hash[parameter_description.select do |k, v|
required_variables = Hash[self.parameter_descriptions.select do |k, v|
v['required']
end].keys
missing_variables = required_variables - parameters.keys
@ -407,8 +464,8 @@ module Google
"Missing required parameters: #{missing_variables.join(', ')}."
end
parameters.each do |k, v|
if parameter_description[k]
pattern = parameter_description[k]['pattern']
if self.parameter_descriptions[k]
pattern = self.parameter_descriptions[k]['pattern']
if pattern
regexp = Regexp.new("^#{pattern}$")
if v !~ regexp

View File

@ -160,7 +160,7 @@ describe Google::APIClient, 'configured for the prediction API' do
@client.latest_service('bogus').should == nil
end
it 'should generate requests against the correct URIs' do
it 'should generate valid requests' do
request = @client.generate_request(
'prediction.training.insert',
{'query' => '12345'},
@ -169,13 +169,30 @@ describe Google::APIClient, 'configured for the prediction API' do
{:signed => false}
)
method, uri, headers, body = request
method.should == 'POST'
uri.should ==
'https://www.googleapis.com/prediction/v1/training?query=12345'
Hash[headers].should == {}
body.should respond_to(:each)
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
:'prediction.training.insert',
{'query' => '12345'},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/prediction/v1/training?query=12345'
end
it 'should generate requests against the correct URIs' do
prediction = @client.discovered_service('prediction', 'v1')
request = @client.generate_request(
:'prediction.training.insert',
prediction.training.insert,
{'query' => '12345'},
'',
[],
@ -221,6 +238,12 @@ describe Google::APIClient, 'configured for the prediction API' do
@client.generate_request(42)
end).should raise_error(TypeError)
end
it 'should raise an error for bogus methods' do
(lambda do
@client.generate_request(@client.discovered_service('prediction'))
end).should raise_error(TypeError)
end
end
describe Google::APIClient, 'configured for the buzz API' do