Two failing tests, neither of which I can do much about.

This commit is contained in:
Bob Aman 2012-08-01 00:16:26 +03:00
parent 45bc8f4898
commit 992a698b0a
2 changed files with 49 additions and 18 deletions

View File

@ -163,7 +163,7 @@ module Google
unless k.kind_of?(String) unless k.kind_of?(String)
raise TypeError, "Expected String, got #{k.class}." raise TypeError, "Expected String, got #{k.class}."
end end
accu << [k,v] accu << [k, v]
accu accu
end end
else else
@ -204,7 +204,7 @@ module Google
# encode all non-template parameters # encode all non-template parameters
params = "" params = ""
unless query_parameters.empty? unless query_parameters.empty?
params = "?" + Addressable::URI.form_encode(query_parameters) params = "?" + Addressable::URI.form_encode(query_parameters.sort)
end end
# Normalization is necessary because of undesirable percent-escaping # Normalization is necessary because of undesirable percent-escaping
# during URI template expansion # during URI template expansion
@ -311,7 +311,7 @@ module Google
required_variables = ((self.parameter_descriptions.select do |k, v| required_variables = ((self.parameter_descriptions.select do |k, v|
v['required'] v['required']
end).inject({}) { |h,(k,v)| h[k]=v; h }).keys end).inject({}) { |h,(k,v)| h[k]=v; h }).keys
missing_variables = required_variables - parameters.map(&:first) missing_variables = required_variables - parameters.map { |(k, _)| k }
if missing_variables.size > 0 if missing_variables.size > 0
raise ArgumentError, raise ArgumentError,
"Missing required parameters: #{missing_variables.join(', ')}." "Missing required parameters: #{missing_variables.join(', ')}."

View File

@ -41,8 +41,25 @@ module Google
self.parameters = options[:parameters] || {} self.parameters = options[:parameters] || {}
# These parameters are handled differently because they're not # These parameters are handled differently because they're not
# parameters to the API method, but rather to the API system. # parameters to the API method, but rather to the API system.
if self.parameters.kind_of?(Array)
if options[:key]
self.parameters.reject! { |k, _| k == 'key' }
self.parameters << ['key', options[:key]]
end
if options[:user_ip]
self.parameters.reject! { |k, _| k == 'userIp' }
self.parameters << ['userIp', options[:user_ip]]
end
elsif self.parameters.kind_of?(Hash)
self.parameters['key'] ||= options[:key] if options[:key] self.parameters['key'] ||= options[:key] if options[:key]
self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip] self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]
# Convert to Array, because they're easier to work with when
# repeated parameters are an issue.
self.parameters = self.parameters.to_a
else
raise TypeError,
"Expected Array or Hash, got #{self.parameters.class}."
end
self.headers = options[:headers] || {} self.headers = options[:headers] || {}
if options[:media] if options[:media]
self.media = options[:media] self.media = options[:media]
@ -50,25 +67,31 @@ module Google
case upload_type case upload_type
when "media" when "media"
if options[:body] || options[:body_object] if options[:body] || options[:body_object]
raise ArgumentError, "Can not specify body & body object for simple uploads" raise ArgumentError,
"Can not specify body & body object for simple uploads."
end end
self.headers['Content-Type'] ||= self.media.content_type self.headers['Content-Type'] ||= self.media.content_type
self.body = self.media self.body = self.media
when "multipart" when "multipart"
unless options[:body_object] unless options[:body_object]
raise ArgumentError, "Multipart requested but no body object" raise ArgumentError, "Multipart requested but no body object."
end end
# This is all a bit of a hack due to signet requiring body to be a string # This is all a bit of a hack due to Signet requiring body to be a
# Ideally, update signet to delay serialization so we can just pass # string. Ideally, update Signet to delay serialization so we can
# streams all the way down through to the HTTP lib # just pass streams all the way down through to the HTTP library.
metadata = StringIO.new(serialize_body(options[:body_object])) metadata = StringIO.new(serialize_body(options[:body_object]))
env = { env = {
:request_headers => {'Content-Type' => "multipart/related;boundary=#{MULTIPART_BOUNDARY}"}, :request_headers => {
:request => { :boundary => MULTIPART_BOUNDARY } 'Content-Type' =>
"multipart/related;boundary=#{MULTIPART_BOUNDARY}"
},
:request => {:boundary => MULTIPART_BOUNDARY}
} }
multipart = Faraday::Request::Multipart.new multipart = Faraday::Request::Multipart.new
self.body = multipart.create_multipart(env, [ self.body = multipart.create_multipart(env, [
[nil,Faraday::UploadIO.new(metadata, 'application/json', 'file.json')], [nil, Faraday::UploadIO.new(
metadata, 'application/json', 'file.json'
)],
[nil, self.media]]) [nil, self.media]])
self.headers.update(env[:request_headers]) self.headers.update(env[:request_headers])
when "resumable" when "resumable"
@ -82,7 +105,7 @@ module Google
self.body = '' self.body = ''
end end
else else
raise ArgumentError, "Invalid uploadType for media" raise ArgumentError, "Invalid uploadType for media."
end end
elsif options[:body] elsif options[:body]
self.body = options[:body] self.body = options[:body]
@ -96,8 +119,12 @@ module Google
self.http_method = options[:http_method] || 'GET' self.http_method = options[:http_method] || 'GET'
self.uri = options[:uri] self.uri = options[:uri]
unless self.parameters.empty? unless self.parameters.empty?
self.uri.query_values = query_values = (self.uri.query_values(Array) || [])
(self.uri.query_values || {}).merge(self.parameters) self.uri.query = Addressable::URI.form_encode(
(query_values + self.parameters).sort
)
self.uri.query = nil if self.uri.query == ""
puts "reference: " + self.uri.to_s
end end
end end
end end
@ -199,7 +226,8 @@ module Google
accu accu
end).string end).string
else else
raise TypeError, "Expected body to be String, IO, or Enumerable chunks." raise TypeError,
"Expected body to be String, IO, or Enumerable chunks."
end end
end end
@ -248,6 +276,7 @@ module Google
return self.connection.build_request( return self.connection.build_request(
self.http_method.to_s.downcase.to_sym self.http_method.to_s.downcase.to_sym
) do |req| ) do |req|
puts "request: " + Addressable::URI.parse(self.uri).normalize.to_s
req.url(Addressable::URI.parse(self.uri).normalize.to_s) req.url(Addressable::URI.parse(self.uri).normalize.to_s)
req.headers = Faraday::Utils::Headers.new(self.headers) req.headers = Faraday::Utils::Headers.new(self.headers)
req.body = self.body req.body = self.body
@ -267,7 +296,9 @@ module Google
options[:headers] = self.headers options[:headers] = self.headers
options[:body] = self.body options[:body] = self.body
options[:connection] = self.connection options[:connection] = self.connection
options[:authorization] = self.authorization unless self.authorization.nil? unless self.authorization.nil?
options[:authorization] = self.authorization
end
return options return options
end end
end end