2010-09-16 19:12:52 +00:00
|
|
|
# Copyright 2010 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
|
2010-09-16 19:12:52 +00:00
|
|
|
require 'json'
|
|
|
|
require 'addressable/uri'
|
|
|
|
require 'addressable/template'
|
2011-01-19 23:41:37 +00:00
|
|
|
|
|
|
|
require 'google/inflection'
|
2011-05-04 11:44:35 +00:00
|
|
|
require 'google/api_client/errors'
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2010-10-05 23:49:00 +00:00
|
|
|
module Google
|
|
|
|
class APIClient
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# A service that has been described by a discovery document.
|
2011-05-04 11:44:35 +00:00
|
|
|
class API
|
2010-10-07 00:02:22 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a description of a particular version of a service.
|
|
|
|
#
|
2011-05-04 11:44:35 +00:00
|
|
|
# @param [String] api
|
2010-10-07 00:02:22 +00:00
|
|
|
# The identifier for the service. Note that while this frequently
|
|
|
|
# matches the first segment of all of the service's RPC names, this
|
|
|
|
# should not be assumed. There is no requirement that these match.
|
2011-05-04 11:44:35 +00:00
|
|
|
# @param [String] version
|
2010-10-07 00:02:22 +00:00
|
|
|
# The identifier for the service version.
|
2011-05-04 11:44:35 +00:00
|
|
|
# @param [Hash] api_description
|
2010-10-07 00:02:22 +00:00
|
|
|
# The section of the discovery document that applies to this service
|
|
|
|
# version.
|
|
|
|
#
|
2011-05-04 11:44:35 +00:00
|
|
|
# @return [Google::APIClient::API] The constructed service object.
|
|
|
|
def initialize(document_base, discovery_document)
|
|
|
|
@document_base = Addressable::URI.parse(document_base)
|
|
|
|
@discovery_document = discovery_document
|
2010-09-16 19:12:52 +00:00
|
|
|
metaclass = (class <<self; self; end)
|
|
|
|
self.resources.each do |resource|
|
2011-01-19 23:41:37 +00:00
|
|
|
method_name = Google::INFLECTOR.underscore(resource.name).to_sym
|
2010-09-16 19:12:52 +00:00
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { resource }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.methods.each do |method|
|
2011-01-19 23:41:37 +00:00
|
|
|
method_name = Google::INFLECTOR.underscore(method.name).to_sym
|
2010-09-16 19:12:52 +00:00
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { method }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
##
|
|
|
|
# Returns the id of the service.
|
|
|
|
#
|
|
|
|
# @return [String] The service id.
|
|
|
|
def id
|
|
|
|
return @discovery_document['id']
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Returns the identifier for the service.
|
|
|
|
#
|
|
|
|
# @return [String] The service identifier.
|
2011-05-04 11:44:35 +00:00
|
|
|
def name
|
|
|
|
return @discovery_document['name']
|
|
|
|
end
|
2010-10-07 00:02:22 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the version of the service.
|
|
|
|
#
|
|
|
|
# @return [String] The service version.
|
2011-05-04 11:44:35 +00:00
|
|
|
def version
|
|
|
|
return @discovery_document['version']
|
|
|
|
end
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Returns the parsed section of the discovery document that applies to
|
|
|
|
# this version of the service.
|
|
|
|
#
|
|
|
|
# @return [Hash] The service description.
|
2011-05-04 11:44:35 +00:00
|
|
|
def description
|
|
|
|
return @discovery_document['description']
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns true if this is the preferred version of this API.
|
|
|
|
#
|
|
|
|
# @return [TrueClass, FalseClass]
|
|
|
|
# Whether or not this is the preferred version of this API.
|
|
|
|
def preferred
|
|
|
|
return @discovery_document['preferred']
|
|
|
|
end
|
2010-10-07 00:02:22 +00:00
|
|
|
|
|
|
|
##
|
2011-05-12 20:14:15 +00:00
|
|
|
# Returns the base URI for the discovery document.
|
2010-10-07 00:02:22 +00:00
|
|
|
#
|
2011-05-12 20:14:15 +00:00
|
|
|
# @return [Addressable::URI] The base URI.
|
2011-05-04 11:44:35 +00:00
|
|
|
attr_reader :document_base
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the base URI for this version of the service.
|
|
|
|
#
|
|
|
|
# @return [Addressable::URI] The base URI that methods are joined to.
|
2011-05-12 20:14:15 +00:00
|
|
|
def method_base
|
|
|
|
if @discovery_document['basePath']
|
|
|
|
return @method_base ||= (
|
2011-05-04 11:44:35 +00:00
|
|
|
self.document_base +
|
2011-05-12 20:14:15 +00:00
|
|
|
Addressable::URI.parse(@discovery_document['basePath'])
|
2011-05-04 11:44:35 +00:00
|
|
|
).normalize
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
|
2010-10-22 22:56:01 +00:00
|
|
|
##
|
|
|
|
# Updates the hierarchy of resources and methods with the new base.
|
|
|
|
#
|
|
|
|
# @param [Addressable::URI, #to_str, String] new_base
|
|
|
|
# The new base URI to use for the service.
|
2011-05-12 20:14:15 +00:00
|
|
|
def method_base=(new_method_base)
|
|
|
|
@method_base = Addressable::URI.parse(new_method_base)
|
2010-10-22 22:56:01 +00:00
|
|
|
self.resources.each do |resource|
|
2011-05-12 20:14:15 +00:00
|
|
|
resource.method_base = @method_base
|
2010-10-22 22:56:01 +00:00
|
|
|
end
|
|
|
|
self.methods.each do |method|
|
2011-05-12 20:14:15 +00:00
|
|
|
method.method_base = @method_base
|
2010-10-22 22:56:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# A list of resources available at the root level of this version of the
|
|
|
|
# service.
|
|
|
|
#
|
|
|
|
# @return [Array] A list of {Google::APIClient::Resource} objects.
|
2010-09-16 19:12:52 +00:00
|
|
|
def resources
|
|
|
|
return @resources ||= (
|
2011-05-04 11:44:35 +00:00
|
|
|
(@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
|
2011-07-29 22:07:04 +00:00
|
|
|
accu << Google::APIClient::Resource.new(self.method_base, k, v)
|
2010-09-16 19:12:52 +00:00
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# A list of methods available at the root level of this version of the
|
|
|
|
# service.
|
|
|
|
#
|
|
|
|
# @return [Array] A list of {Google::APIClient::Method} objects.
|
2010-09-16 19:12:52 +00:00
|
|
|
def methods
|
|
|
|
return @methods ||= (
|
2011-05-04 11:44:35 +00:00
|
|
|
(@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
|
2011-07-29 22:07:04 +00:00
|
|
|
accu << Google::APIClient::Method.new(self.method_base, k, v)
|
2010-09-16 19:12:52 +00:00
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Converts the service to a flat mapping of RPC names and method objects.
|
|
|
|
#
|
|
|
|
# @return [Hash] All methods available on the service.
|
2010-10-07 22:38:51 +00:00
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# # Discover available methods
|
2011-05-04 11:44:35 +00:00
|
|
|
# method_names = client.discovered_api('buzz').to_h.keys
|
2010-09-16 23:40:08 +00:00
|
|
|
def to_h
|
|
|
|
return @hash ||= (begin
|
|
|
|
methods_hash = {}
|
|
|
|
self.methods.each do |method|
|
2011-05-12 20:14:15 +00:00
|
|
|
methods_hash[method.id] = method
|
2010-09-16 23:40:08 +00:00
|
|
|
end
|
|
|
|
self.resources.each do |resource|
|
|
|
|
methods_hash.merge!(resource.to_h)
|
|
|
|
end
|
|
|
|
methods_hash
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-09-16 19:12:52 +00:00
|
|
|
##
|
|
|
|
# Returns a <code>String</code> representation of the service's state.
|
|
|
|
#
|
|
|
|
# @return [String] The service's state, as a <code>String</code>.
|
|
|
|
def inspect
|
|
|
|
sprintf(
|
2011-05-12 20:14:15 +00:00
|
|
|
"#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id
|
2010-09-16 19:12:52 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# A resource that has been described by a discovery document.
|
2010-09-16 19:12:52 +00:00
|
|
|
class Resource
|
2010-10-07 00:02:22 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a description of a particular version of a resource.
|
|
|
|
#
|
|
|
|
# @param [Addressable::URI] base
|
|
|
|
# The base URI for the service.
|
|
|
|
# @param [String] resource_name
|
|
|
|
# The identifier for the resource.
|
|
|
|
# @param [Hash] resource_description
|
|
|
|
# The section of the discovery document that applies to this resource.
|
|
|
|
#
|
|
|
|
# @return [Google::APIClient::Resource] The constructed resource object.
|
2011-05-12 20:14:15 +00:00
|
|
|
def initialize(method_base, resource_name, discovery_document)
|
|
|
|
@method_base = method_base
|
2010-09-16 19:12:52 +00:00
|
|
|
@name = resource_name
|
2011-05-04 11:44:35 +00:00
|
|
|
@discovery_document = discovery_document
|
2010-09-16 19:12:52 +00:00
|
|
|
metaclass = (class <<self; self; end)
|
|
|
|
self.resources.each do |resource|
|
2011-01-19 23:41:37 +00:00
|
|
|
method_name = Google::INFLECTOR.underscore(resource.name).to_sym
|
2010-09-16 19:12:52 +00:00
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { resource }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.methods.each do |method|
|
2011-01-19 23:41:37 +00:00
|
|
|
method_name = Google::INFLECTOR.underscore(method.name).to_sym
|
2010-09-16 19:12:52 +00:00
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { method }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Returns the identifier for the resource.
|
|
|
|
#
|
|
|
|
# @return [String] The resource identifier.
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the parsed section of the discovery document that applies to
|
|
|
|
# this resource.
|
|
|
|
#
|
|
|
|
# @return [Hash] The resource description.
|
|
|
|
attr_reader :description
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the base URI for this resource.
|
|
|
|
#
|
|
|
|
# @return [Addressable::URI] The base URI that methods are joined to.
|
2011-05-12 20:14:15 +00:00
|
|
|
attr_reader :method_base
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2010-10-22 22:56:01 +00:00
|
|
|
##
|
|
|
|
# Updates the hierarchy of resources and methods with the new base.
|
|
|
|
#
|
|
|
|
# @param [Addressable::URI, #to_str, String] new_base
|
|
|
|
# The new base URI to use for the resource.
|
2011-05-12 20:14:15 +00:00
|
|
|
def method_base=(new_method_base)
|
|
|
|
@method_base = Addressable::URI.parse(new_method_base)
|
2010-10-22 22:56:01 +00:00
|
|
|
self.resources.each do |resource|
|
2011-05-12 20:14:15 +00:00
|
|
|
resource.method_base = @method_base
|
2010-10-22 22:56:01 +00:00
|
|
|
end
|
|
|
|
self.methods.each do |method|
|
2011-05-12 20:14:15 +00:00
|
|
|
method.method_base = @method_base
|
2010-10-22 22:56:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# A list of sub-resources available on this resource.
|
|
|
|
#
|
|
|
|
# @return [Array] A list of {Google::APIClient::Resource} objects.
|
2010-09-16 19:12:52 +00:00
|
|
|
def resources
|
|
|
|
return @resources ||= (
|
2011-05-04 11:44:35 +00:00
|
|
|
(@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
|
2011-07-29 22:07:04 +00:00
|
|
|
accu << Google::APIClient::Resource.new(self.method_base, k, v)
|
2010-09-16 19:12:52 +00:00
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# A list of methods available on this resource.
|
|
|
|
#
|
|
|
|
# @return [Array] A list of {Google::APIClient::Method} objects.
|
2010-09-16 19:12:52 +00:00
|
|
|
def methods
|
|
|
|
return @methods ||= (
|
2011-05-04 11:44:35 +00:00
|
|
|
(@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
|
2011-07-29 22:07:04 +00:00
|
|
|
accu << Google::APIClient::Method.new(self.method_base, k, v)
|
2010-09-16 19:12:52 +00:00
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Converts the resource to a flat mapping of RPC names and method
|
|
|
|
# objects.
|
|
|
|
#
|
|
|
|
# @return [Hash] All methods available on the resource.
|
2010-09-16 23:40:08 +00:00
|
|
|
def to_h
|
|
|
|
return @hash ||= (begin
|
|
|
|
methods_hash = {}
|
|
|
|
self.methods.each do |method|
|
2011-05-12 20:14:15 +00:00
|
|
|
methods_hash[method.id] = method
|
2010-09-16 23:40:08 +00:00
|
|
|
end
|
|
|
|
self.resources.each do |resource|
|
|
|
|
methods_hash.merge!(resource.to_h)
|
|
|
|
end
|
|
|
|
methods_hash
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-09-16 19:12:52 +00:00
|
|
|
##
|
|
|
|
# Returns a <code>String</code> representation of the resource's state.
|
|
|
|
#
|
|
|
|
# @return [String] The resource's state, as a <code>String</code>.
|
|
|
|
def inspect
|
|
|
|
sprintf(
|
|
|
|
"#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# A method that has been described by a discovery document.
|
2010-09-16 19:12:52 +00:00
|
|
|
class Method
|
2010-10-07 00:02:22 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a description of a particular method.
|
|
|
|
#
|
2011-05-12 20:14:15 +00:00
|
|
|
# @param [Addressable::URI] method_base
|
2010-10-07 00:02:22 +00:00
|
|
|
# The base URI for the service.
|
|
|
|
# @param [String] method_name
|
|
|
|
# The identifier for the method.
|
|
|
|
# @param [Hash] method_description
|
|
|
|
# The section of the discovery document that applies to this method.
|
|
|
|
#
|
|
|
|
# @return [Google::APIClient::Method] The constructed method object.
|
2011-05-12 20:14:15 +00:00
|
|
|
def initialize(method_base, method_name, discovery_document)
|
|
|
|
@method_base = method_base
|
2010-09-16 19:12:52 +00:00
|
|
|
@name = method_name
|
2011-05-04 11:44:35 +00:00
|
|
|
@discovery_document = discovery_document
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Returns the identifier for the method.
|
|
|
|
#
|
|
|
|
# @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.
|
|
|
|
#
|
|
|
|
# @return [Addressable::URI]
|
|
|
|
# The base URI that this method will be joined to.
|
2011-05-12 20:14:15 +00:00
|
|
|
attr_reader :method_base
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2010-10-22 22:56:01 +00:00
|
|
|
##
|
|
|
|
# Updates the method with the new base.
|
|
|
|
#
|
|
|
|
# @param [Addressable::URI, #to_str, String] new_base
|
|
|
|
# The new base URI to use for the method.
|
2011-05-12 20:14:15 +00:00
|
|
|
def method_base=(new_method_base)
|
|
|
|
@method_base = Addressable::URI.parse(new_method_base)
|
2010-10-22 22:56:01 +00:00
|
|
|
@uri_template = nil
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
2011-05-12 20:14:15 +00:00
|
|
|
# Returns the method ID.
|
2010-10-07 00:02:22 +00:00
|
|
|
#
|
2011-05-12 20:14:15 +00:00
|
|
|
# @return [String] The method identifier.
|
|
|
|
def id
|
|
|
|
return @discovery_document['id']
|
2010-09-16 23:40:08 +00:00
|
|
|
end
|
|
|
|
|
2011-07-29 22:07:04 +00:00
|
|
|
##
|
|
|
|
# Returns the HTTP method or 'GET' if none is specified.
|
|
|
|
#
|
|
|
|
# @return [String] The HTTP method that will be used in the request.
|
|
|
|
def http_method
|
|
|
|
return @discovery_document['httpMethod'] || 'GET'
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Returns the URI template for the method. A parameter list can be
|
|
|
|
# used to expand this into a URI.
|
|
|
|
#
|
|
|
|
# @return [Addressable::Template] The URI template.
|
2010-09-16 19:12:52 +00:00
|
|
|
def uri_template
|
2010-09-28 23:09:07 +00:00
|
|
|
# TODO(bobaman) We shouldn't be calling #to_s here, this should be
|
|
|
|
# a join operation on a URI, but we have to treat these as Strings
|
|
|
|
# because of the way the discovery document provides the URIs.
|
2010-10-07 00:02:22 +00:00
|
|
|
# This should be fixed soon.
|
2011-05-04 11:44:35 +00:00
|
|
|
return @uri_template ||= Addressable::Template.new(
|
2011-05-12 20:14:15 +00:00
|
|
|
self.method_base + @discovery_document['path']
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Normalizes parameters, converting to the appropriate types.
|
|
|
|
#
|
|
|
|
# @param [Hash, Array] parameters
|
|
|
|
# The parameters to normalize.
|
|
|
|
#
|
|
|
|
# @return [Hash] The normalized parameters.
|
2010-09-16 23:40:08 +00:00
|
|
|
def normalize_parameters(parameters={})
|
2010-09-16 19:12:52 +00:00
|
|
|
# Convert keys to Strings when appropriate
|
2010-09-16 23:40:08 +00:00
|
|
|
if parameters.kind_of?(Hash) || parameters.kind_of?(Array)
|
2010-10-07 00:02:22 +00:00
|
|
|
# Is a Hash or an Array a better return type? Do we ever need to
|
|
|
|
# worry about the same parameter being sent twice with different
|
|
|
|
# values?
|
2010-09-16 23:40:08 +00:00
|
|
|
parameters = parameters.inject({}) do |accu, (k, v)|
|
|
|
|
k = k.to_s if k.kind_of?(Symbol)
|
|
|
|
k = k.to_str if k.respond_to?(:to_str)
|
|
|
|
unless k.kind_of?(String)
|
|
|
|
raise TypeError, "Expected String, got #{k.class}."
|
|
|
|
end
|
|
|
|
accu[k] = v
|
|
|
|
accu
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
2010-09-16 23:40:08 +00:00
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected Hash or Array, got #{parameters.class}."
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
2010-09-16 23:40:08 +00:00
|
|
|
return parameters
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Expands the method's URI template using a parameter list.
|
|
|
|
#
|
|
|
|
# @param [Hash, Array] parameters
|
|
|
|
# The parameter list to use.
|
|
|
|
#
|
|
|
|
# @return [Addressable::URI] The URI after expansion.
|
2010-09-16 23:40:08 +00:00
|
|
|
def generate_uri(parameters={})
|
|
|
|
parameters = self.normalize_parameters(parameters)
|
|
|
|
self.validate_parameters(parameters)
|
2010-09-16 19:12:52 +00:00
|
|
|
template_variables = self.uri_template.variables
|
|
|
|
uri = self.uri_template.expand(parameters)
|
|
|
|
query_parameters = parameters.reject do |k, v|
|
|
|
|
template_variables.include?(k)
|
|
|
|
end
|
|
|
|
if query_parameters.size > 0
|
|
|
|
uri.query_values = (uri.query_values || {}).merge(query_parameters)
|
|
|
|
end
|
|
|
|
# Normalization is necessary because of undesirable percent-escaping
|
|
|
|
# during URI template expansion
|
|
|
|
return uri.normalize
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Generates an HTTP request for this method.
|
|
|
|
#
|
|
|
|
# @param [Hash, Array] parameters
|
|
|
|
# The parameters to send.
|
2010-10-09 02:41:38 +00:00
|
|
|
# @param [String, StringIO] body The body for the HTTP request.
|
2010-10-07 00:02:22 +00:00
|
|
|
# @param [Hash, Array] headers The HTTP headers for the request.
|
|
|
|
#
|
|
|
|
# @return [Array] The generated HTTP request.
|
2010-09-16 23:40:08 +00:00
|
|
|
def generate_request(parameters={}, body='', headers=[])
|
2010-10-09 02:41:38 +00:00
|
|
|
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
|
2011-07-29 22:07:04 +00:00
|
|
|
method = self.http_method
|
2010-09-16 23:40:08 +00:00
|
|
|
uri = self.generate_uri(parameters)
|
2010-10-09 02:41:38 +00:00
|
|
|
headers = headers.to_a if headers.kind_of?(Hash)
|
2010-09-16 23:40:08 +00:00
|
|
|
return [method, uri.to_str, headers, [body]]
|
|
|
|
end
|
|
|
|
|
2010-10-09 02:41:38 +00:00
|
|
|
##
|
|
|
|
# Returns a <code>Hash</code> of the parameter descriptions for
|
|
|
|
# this method.
|
|
|
|
#
|
|
|
|
# @return [Hash] The parameter descriptions.
|
|
|
|
def parameter_descriptions
|
2010-10-13 21:16:07 +00:00
|
|
|
@parameter_descriptions ||= (
|
2011-05-04 11:44:35 +00:00
|
|
|
@discovery_document['parameters'] || {}
|
2010-10-13 21:16:07 +00:00
|
|
|
).inject({}) { |h,(k,v)| h[k]=v; h }
|
2010-10-09 02:41:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns an <code>Array</code> of the parameters for this method.
|
|
|
|
#
|
|
|
|
# @return [Array] The parameters.
|
|
|
|
def parameters
|
2010-10-13 21:16:07 +00:00
|
|
|
@parameters ||= ((
|
2011-05-04 11:44:35 +00:00
|
|
|
@discovery_document['parameters'] || {}
|
2010-10-13 21:16:07 +00:00
|
|
|
).inject({}) { |h,(k,v)| h[k]=v; h }).keys
|
2010-10-09 02:41:38 +00:00
|
|
|
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
|
2010-10-13 21:16:07 +00:00
|
|
|
@required_parameters ||= ((self.parameter_descriptions.select do |k, v|
|
2010-10-09 02:41:38 +00:00
|
|
|
v['required']
|
2010-10-13 21:16:07 +00:00
|
|
|
end).inject({}) { |h,(k,v)| h[k]=v; h }).keys
|
2010-10-09 02:41:38 +00:00
|
|
|
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
|
2010-10-13 21:16:07 +00:00
|
|
|
@optional_parameters ||= ((self.parameter_descriptions.reject do |k, v|
|
2010-10-09 02:41:38 +00:00
|
|
|
v['required']
|
2010-10-13 21:16:07 +00:00
|
|
|
end).inject({}) { |h,(k,v)| h[k]=v; h }).keys
|
2010-10-09 02:41:38 +00:00
|
|
|
end
|
|
|
|
|
2010-10-07 00:02:22 +00:00
|
|
|
##
|
|
|
|
# Verifies that the parameters are valid for this method. Raises an
|
|
|
|
# exception if validation fails.
|
|
|
|
#
|
|
|
|
# @param [Hash, Array] parameters
|
|
|
|
# The parameters to verify.
|
|
|
|
#
|
|
|
|
# @return [NilClass] <code>nil</code> if validation passes.
|
2010-09-16 23:40:08 +00:00
|
|
|
def validate_parameters(parameters={})
|
|
|
|
parameters = self.normalize_parameters(parameters)
|
2010-10-13 21:16:07 +00:00
|
|
|
required_variables = ((self.parameter_descriptions.select do |k, v|
|
2010-09-16 23:40:08 +00:00
|
|
|
v['required']
|
2010-10-13 21:16:07 +00:00
|
|
|
end).inject({}) { |h,(k,v)| h[k]=v; h }).keys
|
2010-09-16 23:40:08 +00:00
|
|
|
missing_variables = required_variables - parameters.keys
|
|
|
|
if missing_variables.size > 0
|
|
|
|
raise ArgumentError,
|
|
|
|
"Missing required parameters: #{missing_variables.join(', ')}."
|
|
|
|
end
|
|
|
|
parameters.each do |k, v|
|
2010-10-09 02:41:38 +00:00
|
|
|
if self.parameter_descriptions[k]
|
2011-05-04 11:44:35 +00:00
|
|
|
enum = self.parameter_descriptions[k]['enum']
|
|
|
|
if enum && !enum.include?(v)
|
|
|
|
raise ArgumentError,
|
|
|
|
"Parameter '#{k}' has an invalid value: #{v}. " +
|
|
|
|
"Must be one of #{enum.inspect}."
|
|
|
|
end
|
2010-10-09 02:41:38 +00:00
|
|
|
pattern = self.parameter_descriptions[k]['pattern']
|
2010-09-16 23:40:08 +00:00
|
|
|
if pattern
|
|
|
|
regexp = Regexp.new("^#{pattern}$")
|
|
|
|
if v !~ regexp
|
|
|
|
raise ArgumentError,
|
|
|
|
"Parameter '#{k}' has an invalid value: #{v}. " +
|
|
|
|
"Must match: /^#{pattern}$/."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2010-09-16 19:12:52 +00:00
|
|
|
##
|
|
|
|
# Returns a <code>String</code> representation of the method's state.
|
|
|
|
#
|
|
|
|
# @return [String] The method's state, as a <code>String</code>.
|
|
|
|
def inspect
|
|
|
|
sprintf(
|
2011-05-12 20:14:15 +00:00
|
|
|
"#<%s:%#0x ID:%s>",
|
|
|
|
self.class.to_s, self.object_id, self.id
|
2010-09-16 19:12:52 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|