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.
|
|
|
|
|
|
|
|
require 'json'
|
|
|
|
require 'addressable/uri'
|
|
|
|
require 'addressable/template'
|
|
|
|
require 'extlib/inflection'
|
|
|
|
|
|
|
|
module Google #:nodoc:
|
|
|
|
class APIClient #:nodoc:
|
|
|
|
class ValidationError < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
class Service
|
|
|
|
def initialize(service_name, service_version, service_description)
|
|
|
|
@name = service_name
|
|
|
|
@version = service_version
|
|
|
|
@description = service_description
|
|
|
|
metaclass = (class <<self; self; end)
|
|
|
|
self.resources.each do |resource|
|
|
|
|
method_name = Extlib::Inflection.underscore(resource.name).to_sym
|
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { resource }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.methods.each do |method|
|
|
|
|
method_name = Extlib::Inflection.underscore(method.name).to_sym
|
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { method }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :name, :version, :description
|
|
|
|
|
|
|
|
def base
|
|
|
|
return @base ||= Addressable::URI.parse(self.description['baseUrl'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def resources
|
|
|
|
return @resources ||= (
|
|
|
|
(self.description['resources'] || []).inject([]) do |accu, (k, v)|
|
|
|
|
accu << ::Google::APIClient::Resource.new(self.base, k, v)
|
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def methods
|
|
|
|
return @methods ||= (
|
|
|
|
(self.description['methods'] || []).inject([]) do |accu, (k, v)|
|
|
|
|
accu << ::Google::APIClient::Method.new(self.base, k, v)
|
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-09-16 23:40:08 +00:00
|
|
|
def to_h
|
|
|
|
return @hash ||= (begin
|
|
|
|
methods_hash = {}
|
|
|
|
self.methods.each do |method|
|
|
|
|
methods_hash[method.rpc_name] = method
|
|
|
|
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(
|
|
|
|
"#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Resource
|
|
|
|
def initialize(base, resource_name, resource_description)
|
|
|
|
@base = base
|
|
|
|
@name = resource_name
|
|
|
|
@description = resource_description
|
|
|
|
metaclass = (class <<self; self; end)
|
|
|
|
self.resources.each do |resource|
|
|
|
|
method_name = Extlib::Inflection.underscore(resource.name).to_sym
|
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { resource }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.methods.each do |method|
|
|
|
|
method_name = Extlib::Inflection.underscore(method.name).to_sym
|
|
|
|
if !self.respond_to?(method_name)
|
|
|
|
metaclass.send(:define_method, method_name) { method }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :name, :description, :base
|
|
|
|
|
|
|
|
def resources
|
|
|
|
return @resources ||= (
|
|
|
|
(self.description['resources'] || []).inject([]) do |accu, (k, v)|
|
|
|
|
accu << ::Google::APIClient::Resource.new(self.base, k, v)
|
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def methods
|
|
|
|
return @methods ||= (
|
|
|
|
(self.description['methods'] || []).inject([]) do |accu, (k, v)|
|
|
|
|
accu << ::Google::APIClient::Method.new(self.base, k, v)
|
|
|
|
accu
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2010-09-16 23:40:08 +00:00
|
|
|
def to_h
|
|
|
|
return @hash ||= (begin
|
|
|
|
methods_hash = {}
|
|
|
|
self.methods.each do |method|
|
|
|
|
methods_hash[method.rpc_name] = method
|
|
|
|
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
|
|
|
|
|
|
|
|
class Method
|
|
|
|
def initialize(base, method_name, method_description)
|
|
|
|
@base = base
|
|
|
|
@name = method_name
|
|
|
|
@description = method_description
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :name, :description, :base
|
|
|
|
|
2010-09-16 23:40:08 +00:00
|
|
|
def rpc_name
|
|
|
|
return self.description['rpcName']
|
|
|
|
end
|
|
|
|
|
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-09-16 23:40:08 +00:00
|
|
|
return @uri_template ||=
|
2010-09-28 23:09:07 +00:00
|
|
|
Addressable::Template.new(base.to_s + self.description['pathUrl'])
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
|
|
|
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-09-16 23:40:08 +00:00
|
|
|
def generate_request(parameters={}, body='', headers=[])
|
|
|
|
method = self.description['httpMethod'] || 'GET'
|
|
|
|
uri = self.generate_uri(parameters)
|
|
|
|
return [method, uri.to_str, headers, [body]]
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_parameters(parameters={})
|
|
|
|
parameters = self.normalize_parameters(parameters)
|
|
|
|
parameter_description = self.description['parameters'] || {}
|
|
|
|
required_variables = Hash[parameter_description.select do |k, v|
|
|
|
|
v['required']
|
|
|
|
end].keys
|
|
|
|
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|
|
|
|
|
if parameter_description[k]
|
|
|
|
pattern = parameter_description[k]['pattern']
|
|
|
|
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(
|
2010-09-16 23:40:08 +00:00
|
|
|
"#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.rpc_name
|
2010-09-16 19:12:52 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|