# The base URI for the service.
# @param [String] method_name
# The identifier for the method.
- # @param [Hash] method_description
+ # @param [Hash] discovery_document
# The section of the discovery document that applies to this method.
#
# @return [Google::APIClient::Method] The constructed method object.
@discovery_document = discovery_document
end
+ # @return [String] unparsed discovery document for the method
+ attr_reader :discovery_document
+
##
# Returns the API this method belongs to.
#
# @return [String] The method identifier.
attr_reader :name
- ##
- # Returns the parsed section of the discovery document that applies to
- # this method.
- #
- # @return [Hash] The method description.
- attr_reader :description
-
##
# Returns the base URI for the method.
#
##
# Updates the method with the new base.
#
- # @param [Addressable::URI, #to_str, String] new_base
+ # @param [Addressable::URI, #to_str, String] new_method_base
# The new base URI to use for the method.
def method_base=(new_method_base)
@method_base = Addressable::URI.parse(new_method_base)
@uri_template = nil
end
+ ##
+ # Returns a human-readable description of the method.
+ #
+ # @return [Hash] The API description.
+ def description
+ return @discovery_document['description']
+ end
+
##
# Returns the method ID.
#
# @return [Addressable::Template] The URI template.
def uri_template
return @uri_template ||= Addressable::Template.new(
- self.method_base.join(Addressable::URI.parse(@discovery_document['path']))
+ self.method_base.join(Addressable::URI.parse("./" + @discovery_document['path']))
)
end
unless k.kind_of?(String)
raise TypeError, "Expected String, got #{k.class}."
end
- accu << [k,v]
+ accu << [k, v]
accu
end
else
##
# Expands the method's URI template using a parameter list.
#
+ # @api private
# @param [Hash, Array] parameters
# The parameter list to use.
#
# @return [Addressable::URI] The URI after expansion.
def generate_uri(parameters={})
parameters = self.normalize_parameters(parameters)
+
self.validate_parameters(parameters)
template_variables = self.uri_template.variables
upload_type = parameters.assoc('uploadType') || parameters.assoc('upload_type')
# encode all non-template parameters
params = ""
unless query_parameters.empty?
- params = "?" + Addressable::URI.form_encode(query_parameters)
+ params = "?" + Addressable::URI.form_encode(query_parameters.sort)
end
# Normalization is necessary because of undesirable percent-escaping
# during URI template expansion
##
# Generates an HTTP request for this method.
#
+ # @api private
# @param [Hash, Array] parameters
# The parameters to send.
# @param [String, StringIO] body The body for the HTTP request.
# @param [Hash, Array] headers The HTTP headers for the request.
+ # @option options [Faraday::Connection] :connection
+ # The HTTP connection to use.
#
# @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
+ def generate_request(parameters={}, body='', headers={}, options={})
if !headers.kind_of?(Array) && !headers.kind_of?(Hash)
raise TypeError, "Expected Hash or Array, got #{headers.class}."
end
- method = self.http_method
+ method = self.http_method.to_s.downcase.to_sym
uri = self.generate_uri(parameters)
- headers = headers.to_a if headers.kind_of?(Hash)
- return Faraday::Request.create(method.to_s.downcase.to_sym) do |req|
- req.url(Addressable::URI.parse(uri))
- req.headers = Faraday::Utils::Headers.new(headers)
- req.body = body
- end
+ headers = Faraday::Utils::Headers.new(headers)
+ return [method, uri, headers, body]
end
# Verifies that the parameters are valid for this method. Raises an
# exception if validation fails.
#
+ # @api private
# @param [Hash, Array] parameters
# The parameters to verify.
#
required_variables = ((self.parameter_descriptions.select do |k, v|
v['required']
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
raise ArgumentError,
"Missing required parameters: #{missing_variables.join(', ')}."