Import refactored library. See MIGRATING.MD for details
This commit is contained in:
1 parent
9b7809174d
commit
f584a2cec8
373 files changed
+255715
-474
No files matched your search
@@ -0,0 +1,271 @@
|
||||
# Copyright 2015 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 'logger'
|
||||
require 'erb'
|
||||
require 'yaml'
|
||||
require 'multi_json'
|
||||
require 'active_support/inflector'
|
||||
require 'google/apis/core/logging'
|
||||
require 'google/apis/generator/template'
|
||||
require 'google/apis/generator/model'
|
||||
require 'google/apis/generator/helpers'
|
||||
require 'addressable/uri'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
# @private
|
||||
class Generator
|
||||
# Helper for picking names for methods, properties, types, etc. Performs various normaliations
|
||||
# as well as allows for overriding individual names from a configuration file for cases
|
||||
# where algorithmic approaches produce poor APIs.
|
||||
class Names
|
||||
include Google::Apis::Core::Logging
|
||||
include NameHelpers
|
||||
|
||||
def initialize(file_path = nil)
|
||||
if file_path
|
||||
logger.info { sprintf('Loading API names from %s', file_path) }
|
||||
@names = YAML.load(File.read(file_path)) || {}
|
||||
else
|
||||
@names = {}
|
||||
end
|
||||
@path = []
|
||||
end
|
||||
|
||||
def with_path(path)
|
||||
@path.push(path)
|
||||
begin
|
||||
yield
|
||||
ensure
|
||||
@path.pop
|
||||
end
|
||||
end
|
||||
|
||||
def infer_parameter_name
|
||||
pick_name(normalize_param_name(@path.last))
|
||||
end
|
||||
|
||||
# Determine the ruby method name to generate for a given method in discovery.
|
||||
# @param [Google::Apis::DiscoveryV1::RestMethod] method
|
||||
# Fragment of the discovery doc describing the method
|
||||
def infer_method_name(method)
|
||||
pick_name(infer_method_name_for_rpc(method) || infer_method_name_from_id(method))
|
||||
end
|
||||
|
||||
def infer_property_name
|
||||
pick_name(normalize_property_name(@path.last))
|
||||
end
|
||||
|
||||
def pick_name(alt_name)
|
||||
preferred_name = @names[key]
|
||||
if preferred_name && preferred_name == alt_name
|
||||
logger.warn { sprintf("Unnecessary name override '%s': %s", key, alt_name) }
|
||||
elsif preferred_name.nil?
|
||||
preferred_name = @names[key] = alt_name
|
||||
end
|
||||
preferred_name
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
@names[key] = value
|
||||
end
|
||||
|
||||
def dump
|
||||
YAML.dump(@names)
|
||||
end
|
||||
|
||||
def key
|
||||
@path.reduce('') { |a, e| a + '/' + e }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# For RPC style methods, pick a name based off the request objects.
|
||||
# @param [Google::Apis::DiscoveryV1::RestMethod] method
|
||||
# Fragment of the discovery doc describing the method
|
||||
def infer_method_name_for_rpc(method)
|
||||
return nil if method.request.nil?
|
||||
verb = ActiveSupport::Inflector.underscore(method.id.split('.').last)
|
||||
match = method.request._ref.match(/(.*)(?i:request)/)
|
||||
return nil if match.nil?
|
||||
name = ActiveSupport::Inflector.underscore(match[1])
|
||||
return nil unless name == verb || name.start_with?(verb + '_')
|
||||
name
|
||||
end
|
||||
|
||||
# For REST style methods, build a method name from the verb/resource(s) in the method
|
||||
# id. IDs are in the form <api>.<resource>.<verb>
|
||||
# @param [Google::Apis::DiscoveryV1::RestMethod] method
|
||||
# Fragment of the discovery doc describing the method
|
||||
def infer_method_name_from_id(method)
|
||||
parts = method.id.split('.')
|
||||
parts.shift
|
||||
verb = parts.pop
|
||||
return ActiveSupport::Inflector.underscore(verb) if parts.empty?
|
||||
resource_name = parts.pop
|
||||
method_name = verb + '_'
|
||||
method_name += parts.map { |p| ActiveSupport::Inflector.singularize(p) }.join('_') + '_' unless parts.empty?
|
||||
if pluralize_method?(verb)
|
||||
method_name += ActiveSupport::Inflector.pluralize(resource_name)
|
||||
else
|
||||
method_name += ActiveSupport::Inflector.singularize(resource_name)
|
||||
end
|
||||
ActiveSupport::Inflector.underscore(method_name)
|
||||
end
|
||||
end
|
||||
|
||||
# Modifies an API description to support ruby code generation. Primarily does:
|
||||
# - Ensure all names follow appopriate ruby conventions
|
||||
# - Maps types to ruby types, classes, and resolves $refs
|
||||
# - Attempts to simplify names where possible to make APIs more sensible
|
||||
class Annotator
|
||||
include NameHelpers
|
||||
include Google::Apis::Core::Logging
|
||||
|
||||
# Don't expose these in the API directly.
|
||||
PARAMETER_BLACKLIST = %w(alt access_token bearer_token oauth_token pp prettyPrint
|
||||
$.xgafv callback upload_protocol uploadType)
|
||||
|
||||
# Prepare the API for the templates.
|
||||
# @param [Google::Apis::DiscoveryV1::RestDescription] description
|
||||
# API Description
|
||||
def self.process(description, api_names = nil)
|
||||
Annotator.new(description, api_names).annotate_api
|
||||
end
|
||||
|
||||
# @param [Google::Apis::DiscoveryV1::RestDescription] description
|
||||
# API Description
|
||||
# @param [Google::Api::Generator::Names] api_names
|
||||
# Name helper instanace
|
||||
def initialize(description, api_names = nil)
|
||||
api_names = Names.new if api_names.nil?
|
||||
@names = api_names
|
||||
@rest_description = description
|
||||
@registered_types = []
|
||||
@deferred_types = []
|
||||
@strip_prefixes = []
|
||||
@all_methods = {}
|
||||
@path = []
|
||||
end
|
||||
|
||||
def annotate_api
|
||||
@names.with_path(@rest_description.id) do
|
||||
@strip_prefixes << @rest_description.name
|
||||
if @rest_description.auth
|
||||
@rest_description.auth.oauth2.scopes.each do |key, value|
|
||||
value.constant = constantize_scope(key)
|
||||
end
|
||||
end
|
||||
@rest_description.parameters.reject! { |k, _v| PARAMETER_BLACKLIST.include?(k) }
|
||||
annotate_parameters(@rest_description.parameters)
|
||||
annotate_resource(@rest_description.name, @rest_description)
|
||||
@rest_description.schemas.each do |k, v|
|
||||
annotate_type(k, v, @rest_description)
|
||||
end unless @rest_description.schemas.nil?
|
||||
end
|
||||
resolve_type_references
|
||||
resolve_variants
|
||||
end
|
||||
|
||||
def annotate_type(name, type, parent)
|
||||
@names.with_path(name) do
|
||||
type.name = name
|
||||
type.path = @names.key
|
||||
type.generated_name = @names.infer_property_name
|
||||
if type.type == 'object'
|
||||
type.generated_class_name = ActiveSupport::Inflector.camelize(type.generated_name)
|
||||
@registered_types << type
|
||||
end
|
||||
type.parent = parent
|
||||
@deferred_types << type if type._ref
|
||||
type.properties.each do |k, v|
|
||||
annotate_type(k, v, type)
|
||||
end unless type.properties.nil?
|
||||
if type.additional_properties
|
||||
type.type = 'hash'
|
||||
annotate_type(ActiveSupport::Inflector.singularize(type.generated_name), type.additional_properties,
|
||||
parent)
|
||||
end
|
||||
annotate_type(ActiveSupport::Inflector.singularize(type.generated_name), type.items, parent) if type.items
|
||||
end
|
||||
end
|
||||
|
||||
def annotate_resource(name, resource, parent_resource = nil)
|
||||
@strip_prefixes << name
|
||||
resource.parent = parent_resource unless parent_resource.nil?
|
||||
resource.api_methods.each do |_k, v|
|
||||
annotate_method(v, resource)
|
||||
end unless resource.api_methods.nil?
|
||||
|
||||
resource.resources.each do |k, v|
|
||||
annotate_resource(k, v, resource)
|
||||
end unless resource.resources.nil?
|
||||
end
|
||||
|
||||
def annotate_method(method, parent_resource = nil)
|
||||
@names.with_path(method.id) do
|
||||
method.parent = parent_resource
|
||||
method.generated_name = @names.infer_method_name(method)
|
||||
check_duplicate_method(method)
|
||||
annotate_parameters(method.parameters)
|
||||
end
|
||||
end
|
||||
|
||||
def annotate_parameters(parameters)
|
||||
parameters.each do |key, value|
|
||||
@names.with_path(key) do
|
||||
value.name = key
|
||||
value.generated_name = @names.infer_parameter_name
|
||||
@deferred_types << value if value._ref
|
||||
end
|
||||
end unless parameters.nil?
|
||||
end
|
||||
|
||||
def resolve_type_references
|
||||
@deferred_types.each do |type|
|
||||
if type._ref
|
||||
ref = @rest_description.schemas[type._ref]
|
||||
ivars = ref.instance_variables - [:@name, :@generated_name]
|
||||
(ivars).each do |var|
|
||||
type.instance_variable_set(var, ref.instance_variable_get(var))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def resolve_variants
|
||||
@deferred_types.each do |type|
|
||||
if type.variant
|
||||
type.variant.map.each do |v|
|
||||
ref = @rest_description.schemas[v._ref]
|
||||
ref.base_ref = type
|
||||
ref.discriminant = type.variant.discriminant
|
||||
ref.discriminant_value = v.type_value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def check_duplicate_method(m)
|
||||
if @all_methods.include?(m.generated_name)
|
||||
logger.error { sprintf('Duplicate method %s generated', m.generated_name) }
|
||||
fail 'Duplicate name generated'
|
||||
end
|
||||
@all_methods[m.generated_name] = m
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
module Google
|
||||
module Apis
|
||||
# @private
|
||||
class Generator
|
||||
# Methods for validating & normalizing symbols
|
||||
module NameHelpers
|
||||
KEYWORDS = %w(__ENCODING__ def in self __LINE__ defined? module super __FILE__ do next then BEGIN
|
||||
else nil true END elsif not undef alias end or unless and ensure redo until begin
|
||||
false rescue when break for retry while case if return yield class)
|
||||
PLURAL_METHODS = %w(list search)
|
||||
|
||||
# Check to see if the method name should be plauralized
|
||||
# @return [Boolean]
|
||||
def pluralize_method?(method_name)
|
||||
PLURAL_METHODS.include?(method_name)
|
||||
end
|
||||
|
||||
# Check to see if the method is either a keyword or built-in method on object
|
||||
# @return [Boolean]
|
||||
def reserved?(name)
|
||||
keyword?(name) || object_method?(name)
|
||||
end
|
||||
|
||||
# Check to see if the name is a ruby keyword
|
||||
# @return [Boolean]
|
||||
def keyword?(name)
|
||||
KEYWORDS.include?(name)
|
||||
end
|
||||
|
||||
# Check to see if the method already exists on ruby objects
|
||||
# @return [Boolean]
|
||||
def object_method?(name)
|
||||
Object.new.respond_to?(name)
|
||||
end
|
||||
|
||||
# Convert a parameter name to ruby conventions
|
||||
# @param [String] name
|
||||
# @return [String] updated param name
|
||||
def normalize_param_name(name)
|
||||
name = ActiveSupport::Inflector.underscore(name.gsub(/\W/, '_'))
|
||||
if reserved?(name)
|
||||
logger.warn { sprintf('Found reserved keyword \'%1$s\'', name) }
|
||||
name += '_'
|
||||
logger.warn { sprintf('Changed to \'%1$s\'', name) }
|
||||
end
|
||||
name
|
||||
end
|
||||
|
||||
# Convert a property name to ruby conventions
|
||||
# @param [String] name
|
||||
# @return [String]
|
||||
def normalize_property_name(name)
|
||||
name = ActiveSupport::Inflector.underscore(name.gsub(/\W/, '_'))
|
||||
if object_method?(name)
|
||||
logger.warn { sprintf('Found reserved property \'%1$s\'', name) }
|
||||
name += '_prop'
|
||||
logger.warn { sprintf('Changed to \'%1$s\'', name) }
|
||||
end
|
||||
name
|
||||
end
|
||||
|
||||
# Converts a scope string into a ruby constant
|
||||
# @param [String] url
|
||||
# Url to convert
|
||||
# @return [String]
|
||||
def constantize_scope(url)
|
||||
scope = Addressable::URI.parse(url).path[1..-1].upcase.gsub(/\W/, '_')
|
||||
scope = 'AUTH_SCOPE' if scope.nil? || scope.empty?
|
||||
scope
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,130 @@
|
||||
# Copyright 2015 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 'active_support/inflector'
|
||||
require 'google/apis/discovery_v1'
|
||||
|
||||
# Extend the discovery API classes with additional data needed to make
|
||||
# code generation produce better results
|
||||
module Google
|
||||
module Apis
|
||||
module DiscoveryV1
|
||||
TYPE_MAP = {
|
||||
'string' => 'String',
|
||||
'boolean' => 'Boolean',
|
||||
'number' => 'Float',
|
||||
'integer' => 'Fixnum',
|
||||
'any' => 'Object'
|
||||
}
|
||||
|
||||
class JsonSchema
|
||||
attr_accessor :name
|
||||
attr_accessor :generated_name
|
||||
attr_accessor :generated_class_name
|
||||
attr_accessor :base_ref
|
||||
attr_accessor :parent
|
||||
attr_accessor :discriminant
|
||||
attr_accessor :discriminant_value
|
||||
attr_accessor :path
|
||||
|
||||
def properties
|
||||
@properties ||= {}
|
||||
end
|
||||
|
||||
def qualified_name
|
||||
parent.qualified_name + '::' + generated_class_name
|
||||
end
|
||||
|
||||
def generated_type
|
||||
case type
|
||||
when 'string', 'boolean', 'number', 'integer', 'any'
|
||||
return 'DateTime' if format == 'date-time'
|
||||
return 'Date' if format == 'date'
|
||||
return TYPE_MAP[type]
|
||||
when 'array'
|
||||
return sprintf('Array<%s>', items.generated_type)
|
||||
when 'hash'
|
||||
return sprintf('Hash<String,%s>', additional_properties.generated_type)
|
||||
when 'object'
|
||||
return qualified_name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class RestMethod
|
||||
attr_accessor :generated_name
|
||||
attr_accessor :parent
|
||||
|
||||
def path_parameters
|
||||
return [] if parameter_order.nil? || parameters.nil?
|
||||
parameter_order.map { |name| parameters[name] }.select { |param| param.location == 'path' }
|
||||
end
|
||||
|
||||
def query_parameters
|
||||
return [] if parameters.nil?
|
||||
parameters.values.select { |param| param.location == 'query' }
|
||||
end
|
||||
end
|
||||
|
||||
class RestResource
|
||||
attr_accessor :parent
|
||||
|
||||
def all_methods
|
||||
m = []
|
||||
m << api_methods.values unless api_methods.nil?
|
||||
m << resources.map { |_k, r| r.all_methods } unless resources.nil?
|
||||
m.flatten
|
||||
end
|
||||
end
|
||||
|
||||
class RestDescription
|
||||
def version
|
||||
ActiveSupport::Inflector.camelize(@version.gsub(/\W/, '-')).gsub(/-/, '_')
|
||||
end
|
||||
|
||||
def name
|
||||
ActiveSupport::Inflector.camelize(@name)
|
||||
end
|
||||
|
||||
def module_name
|
||||
name + version
|
||||
end
|
||||
|
||||
def qualified_name
|
||||
sprintf('Google::Apis::%s', module_name)
|
||||
end
|
||||
|
||||
def service_name
|
||||
class_name = (canonical_name || name).gsub(/\W/, '')
|
||||
ActiveSupport::Inflector.camelize(sprintf('%sService', class_name))
|
||||
end
|
||||
|
||||
def all_methods
|
||||
m = []
|
||||
m << api_methods.values unless api_methods.nil?
|
||||
m << resources.map { |_k, r| r.all_methods } unless resources.nil?
|
||||
m.flatten
|
||||
end
|
||||
|
||||
class Auth
|
||||
class Oauth2
|
||||
class Scope
|
||||
attr_accessor :constant
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,124 @@
|
||||
# Copyright 2015 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 'active_support/inflector'
|
||||
require 'erb'
|
||||
require 'ostruct'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
# @private
|
||||
class Generator
|
||||
# Directory containing ERB templates
|
||||
TEMPLATE_DIR = File.expand_path('../templates', __FILE__)
|
||||
|
||||
# Helpers used in ERB templates
|
||||
module TemplateHelpers
|
||||
# Get the include path for a ruby module/class
|
||||
#
|
||||
# @param [String] module_name
|
||||
# Fully qualified module/class name
|
||||
# @return [String]
|
||||
# Path to file
|
||||
def to_path(module_name)
|
||||
ActiveSupport::Inflector.underscore(module_name)
|
||||
end
|
||||
|
||||
# Render a block comment
|
||||
#
|
||||
# @param [String] str
|
||||
# Comment string
|
||||
# @param [Fixnum] spaces_before
|
||||
# Number of spaces to indent the comment hash
|
||||
# @param [Fixnum] spaces_after
|
||||
# Number of spaces to indent after the comment hash for subsequent lines
|
||||
# @return [String] formatted comment
|
||||
def block_comment(str, spaces_before = 0, spaces_after = 0)
|
||||
return '' if str.nil?
|
||||
pre = ' ' * spaces_before
|
||||
post = ' ' * spaces_after
|
||||
lines = str.gsub(/([{}])/, '`').scan(/.{1,78}(?:\W|$)/).map(&:strip)
|
||||
lines.join("\n" + pre + '#' + post)
|
||||
end
|
||||
|
||||
# Indent a block of text
|
||||
#
|
||||
# @param [String] str
|
||||
# Content to indent
|
||||
# @param [Fixnum] spaces
|
||||
# Number of spaces to indent
|
||||
# @return [String] formatted content
|
||||
def indent(str, spaces)
|
||||
pre = ' ' * spaces
|
||||
str = pre + str.split(/\n/).join("\n" + pre) + "\n"
|
||||
return str unless str.strip.empty?
|
||||
nil
|
||||
end
|
||||
|
||||
# Include a partial inside a template.
|
||||
#
|
||||
# @private
|
||||
# @param [String] partial
|
||||
# Name of the template
|
||||
# @param [Hash] context
|
||||
# Context used to render
|
||||
# @return [String] rendered content
|
||||
def include(partial, context)
|
||||
template = Template.new(sprintf('_%s.tmpl', partial))
|
||||
template.render(context)
|
||||
end
|
||||
end
|
||||
|
||||
# Holds local vars/helpers for template rendering
|
||||
class Context < OpenStruct
|
||||
include TemplateHelpers
|
||||
|
||||
# Get the context for ERB evaluation
|
||||
# @return [Binding]
|
||||
def to_binding
|
||||
binding
|
||||
end
|
||||
end
|
||||
|
||||
# ERB template for the code generator
|
||||
class Template
|
||||
# Loads a template from the template dir. Automatically
|
||||
# appends the .tmpl suffix
|
||||
#
|
||||
# @param [String] template_name
|
||||
# Name of the template file
|
||||
def self.load(template_name)
|
||||
Template.new(sprintf('%s.tmpl', template_name))
|
||||
end
|
||||
|
||||
# @param [String] template_name
|
||||
# Name of the template file
|
||||
def initialize(template_name)
|
||||
file = File.join(TEMPLATE_DIR, template_name)
|
||||
@erb = ERB.new(File.read(file), nil, '-')
|
||||
end
|
||||
|
||||
# Render the template
|
||||
#
|
||||
# @param [Hash] context
|
||||
# Variables to set when rendering the template
|
||||
# @return [String] rendered template
|
||||
def render(context)
|
||||
ctx = Context.new(context)
|
||||
@erb.result(ctx.to_binding)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
<% if cls.type == 'object' -%>
|
||||
|
||||
# <%= block_comment(cls.description, 0, 1) %>
|
||||
class <%= cls.generated_class_name %><% if cls.base_ref %> < <%= cls.base_ref.generated_type %><% end %>
|
||||
include Google::Apis::Core::Hashable
|
||||
<% for property in cls.properties.values -%>
|
||||
|
||||
# <%= block_comment(property.description, 2, 1) %>
|
||||
# Corresponds to the JSON property `<%= property.name %>`
|
||||
# @return [<%= property.generated_type %>]
|
||||
attr_accessor :<%= property.generated_name %>
|
||||
<% if property.type == 'boolean' -%>
|
||||
alias_method :<%= property.generated_name %>?, :<%= property.generated_name %>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
|
||||
def initialize(**args)
|
||||
<% if cls.discriminant -%>
|
||||
@<%= cls.properties[cls.discriminant].generated_name %> = '<%= cls.discriminant_value %>'
|
||||
<% end -%>
|
||||
update!(**args)
|
||||
end
|
||||
|
||||
# Update properties of this object
|
||||
def update!(**args)
|
||||
<% for property in cls.properties.values -%>
|
||||
@<%= property.generated_name %> = args[:<%= property.generated_name %>] unless args[:<%= property.generated_name %>].nil?
|
||||
<% end -%>
|
||||
end
|
||||
<% for child_class in cls.properties.values -%>
|
||||
<% if child_class._ref.nil? -%>
|
||||
<%= indent(include('class', :cls => child_class, :api => api), 2) -%>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
end
|
||||
<% elsif cls.items && cls.items._ref.nil? -%>
|
||||
<%= include('class', :cls => cls.items, :api => api) -%>
|
||||
<% elsif cls.additional_properties && cls.additional_properties._ref.nil? -%>
|
||||
<%= include('class', :cls => cls.additional_properties, :api => api) -%>
|
||||
<% end -%>
|
||||
@@ -0,0 +1,90 @@
|
||||
|
||||
# <%= block_comment(api_method.description, 0, 1) %>
|
||||
<% for param in api_method.path_parameters -%>
|
||||
# @param [<% if param.repeated? %>Array<<%= param.generated_type %>>, <% end %><%= param.generated_type %>] <%= param.generated_name %>
|
||||
<% if param.description -%>
|
||||
# <%= block_comment(param.description, 0, 3) %>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
<% if api_method.request -%>
|
||||
# @param [<%= api.schemas[api_method.request._ref].generated_type %>] <%= api.schemas[api_method.request._ref].generated_name %>_object
|
||||
<% end -%>
|
||||
<% for param in api_method.query_parameters -%>
|
||||
# @param [<% if param.repeated? %>Array<<%= param.generated_type %>>, <% end %><%= param.generated_type %>] <%= param.generated_name %>
|
||||
<% if param.description -%>
|
||||
# <%= block_comment(param.description, 0, 3) %>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
<% for param in api.parameters.values.reject {|p| p.name == 'key'} -%>
|
||||
# @param [<% if param.repeated? %>Array<<%= param.generated_type %>>, <% end %><%= param.generated_type %>] <%= param.generated_name %>
|
||||
<% if param.description -%>
|
||||
# <%= block_comment(param.description, 0, 3) %>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
<% if api_method.supports_media_upload? -%>
|
||||
# @param [IO, String] upload_source
|
||||
# IO stream or filename containing content to upload
|
||||
# @param [String] content_type
|
||||
# Content type of the uploaded content.
|
||||
<% elsif api_method.supports_media_download? -%>
|
||||
# @param [IO, String] download_dest
|
||||
# IO stream or filename to receive content download
|
||||
<% end -%>
|
||||
# @param [Google::Apis::RequestOptions] options
|
||||
# Request-specific options
|
||||
#
|
||||
# @yield [result, err] Result & error if block supplied
|
||||
<% if api_method.response -%>
|
||||
# @yieldparam result [<%= api.schemas[api_method.response._ref].generated_type %>] parsed result object
|
||||
# @yieldparam err [StandardError] error object if request failed
|
||||
#
|
||||
# @return [<%= api.schemas[api_method.response._ref].generated_type %>]
|
||||
<% else -%>
|
||||
# @yieldparam result [NilClass] No result returned for this method
|
||||
# @yieldparam err [StandardError] error object if request failed
|
||||
#
|
||||
# @return [void]
|
||||
<% end -%>
|
||||
#
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def <%= api_method.generated_name %>(<% for param in api_method.path_parameters %><%= param.generated_name %>, <% end %><% if api_method.request %><%= api.schemas[api_method.request._ref].generated_name %>_object = nil, <% end %><% for param in api_method.query_parameters %><%= param.generated_name %>: nil, <% end %><% for param in api.parameters.values.reject {|p| p.name == 'key'} %><%= param.generated_name %>: nil, <% end %><% if api_method.supports_media_upload? %>upload_source: nil, content_type: nil, <% elsif api_method.supports_media_download? %>download_dest: nil, <% end %>options: nil, &block)
|
||||
path = '<%= api_method.path %>'
|
||||
<% if api_method.supports_media_upload? -%>
|
||||
if upload_source.nil?
|
||||
command = make_simple_command(:<%= api_method.http_method.downcase %>, path, options)
|
||||
else
|
||||
command = make_upload_command(:<%= api_method.http_method.downcase %>, path, options)
|
||||
command.upload_source = upload_source
|
||||
command.upload_content_type = content_type
|
||||
end
|
||||
<% elsif api_method.supports_media_download? -%>
|
||||
if download_dest.nil?
|
||||
command = make_simple_command(:<%= api_method.http_method.downcase %>, path, options)
|
||||
else
|
||||
command = make_download_command(:<%= api_method.http_method.downcase %>, path, options)
|
||||
command.download_dest = download_dest
|
||||
end
|
||||
<% else -%>
|
||||
command = make_simple_command(:<%= api_method.http_method.downcase %>, path, options)
|
||||
<% end -%>
|
||||
<% if api_method.request -%>
|
||||
command.request_representation = <%= api.schemas[api_method.request._ref].generated_type %>::Representation
|
||||
command.request_object = <%= api.schemas[api_method.request._ref].generated_name %>_object
|
||||
<% end -%>
|
||||
<% if api_method.response -%>
|
||||
command.response_representation = <%= api.schemas[api_method.response._ref].generated_type %>::Representation
|
||||
command.response_class = <%= api.schemas[api_method.response._ref].generated_type %>
|
||||
<% end -%>
|
||||
<% for param in api_method.path_parameters -%>
|
||||
command.params['<%= param.name %>'] = <%= param.generated_name %> unless <%= param.generated_name %>.nil?
|
||||
<% end -%>
|
||||
<% for param in api_method.query_parameters -%>
|
||||
command.query['<%= param.name %>'] = <%= param.generated_name %> unless <%= param.generated_name %>.nil?
|
||||
<% end -%>
|
||||
<% for param in api.parameters.values.reject {|p| p.name == 'key'} -%>
|
||||
command.query['<%= param.name %>'] = <%= param.generated_name %> unless <%= param.generated_name %>.nil?
|
||||
<% end -%>
|
||||
execute_or_queue_command(command, &block)
|
||||
end
|
||||
@@ -0,0 +1,51 @@
|
||||
<% if cls.type == 'object' -%>
|
||||
|
||||
# @private
|
||||
class <%= cls.generated_class_name %>
|
||||
class Representation < Google::Apis::Core::JsonRepresentation
|
||||
<% if api.features && api.features.include?('dataWrapper') -%>
|
||||
self.representation_wrap = lambda { |args| :data if args[:unwrap] == <%= cls.generated_type %> }
|
||||
<% end -%>
|
||||
<% if cls.variant -%>
|
||||
def from_hash(hash, *args)
|
||||
case hash['<%= cls.variant.discriminant %>']
|
||||
<% for v in cls.variant.map -%>
|
||||
<% ref = api.schemas[v._ref] %>
|
||||
when '<%= v.type_value %>'
|
||||
<%= ref.generated_type %>::Representation.new(<%= ref.generated_type %>.new).from_hash(hash, *args)
|
||||
<% end -%>
|
||||
end
|
||||
end
|
||||
|
||||
def to_hash(*args)
|
||||
case represented
|
||||
<% for v in cls.variant.map -%>
|
||||
<% ref = api.schemas[v._ref] %>
|
||||
when <%= ref.generated_type %>
|
||||
<%= ref.generated_type %>::Representation.new(represented).to_hash(*args)
|
||||
<% end -%>
|
||||
end
|
||||
end
|
||||
<% else -%>
|
||||
<% for property in cls.properties.values -%>
|
||||
<% if property.type == 'hash' -%>
|
||||
hash :<%= property.generated_name %>, as: '<%= property.name %>'<%= include('representation_type', :lead => ', ', :type => property.additional_properties, :api => api) %>
|
||||
<% elsif property.type == 'array' -%>
|
||||
collection :<%= property.generated_name %>, as: '<%= property.name %>'<%= include('representation_type', :lead => ', ', :type => property.items, :api => api) %>
|
||||
<% else -%>
|
||||
property :<%= property.generated_name %>,<% if property.format == 'byte' %> :base64 => true,<%end%> as: '<%= property.name %>'<%= include('representation_type', :lead => ', ', :type => property, :api => api) %>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
end
|
||||
<% for child_class in cls.properties.values -%>
|
||||
<% if child_class._ref.nil? -%>
|
||||
<%= indent(include('representation', :cls => child_class, :api => api), 2) -%>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
end
|
||||
<% elsif cls.items && cls.items._ref.nil? -%>
|
||||
<%= include('representation', :cls => cls.items, :api => api) -%>
|
||||
<% elsif cls.additional_properties && cls.additional_properties._ref.nil? -%>
|
||||
<%= include('representation', :cls => cls.additional_properties, :api => api) -%>
|
||||
<% end -%>
|
||||
@@ -0,0 +1,15 @@
|
||||
<% if cls.type == 'object' -%>
|
||||
|
||||
class <%= cls.generated_class_name %>
|
||||
class Representation < Google::Apis::Core::JsonRepresentation; end
|
||||
<% for child_class in cls.properties.values -%>
|
||||
<% if child_class._ref.nil? -%>
|
||||
<%= indent(include('representation_stub', :cls => child_class), 2) -%>
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
end
|
||||
<% elsif cls.items && cls.items._ref.nil? -%>
|
||||
<%= include('representation_stub', :cls => cls.items, :api => api) -%>
|
||||
<% elsif cls.additional_properties && cls.additional_properties._ref.nil? -%>
|
||||
<%= include('representation_stub', :cls => cls.additional_properties, :api => api) -%>
|
||||
<% end -%>
|
||||
@@ -0,0 +1,10 @@
|
||||
<% if type.type == 'array' -%>
|
||||
<%= lead %>:class => Array do
|
||||
include Representable::JSON::Collection
|
||||
items<%= include('representation_type', :lead => ' ' , :type => type.items, :api => api) %>
|
||||
end
|
||||
<% elsif ['date', 'date-time'].include?(type.format) -%>
|
||||
<%= lead %>type: <%= type.generated_type %>
|
||||
<% elsif type.type == 'object' -%>
|
||||
<%= lead %>class: <%= type.generated_type %>, decorator: <%= type.generated_type %>::Representation
|
||||
<% end -%>
|
||||
@@ -0,0 +1,29 @@
|
||||
# Copyright 2015 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 'date'
|
||||
require 'google/apis/core/base_service'
|
||||
require 'google/apis/core/json_representation'
|
||||
require 'google/apis/core/hashable'
|
||||
require 'google/apis/errors'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module <%= api.module_name %>
|
||||
<% for cls in api.schemas.values.partition(&:variant).flatten -%>
|
||||
<%= indent(include('class', :cls => cls, :api => api), 6) -%>
|
||||
<% end -%>
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
# Copyright 2015 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 '<%= to_path(api.qualified_name) %>/service.rb'
|
||||
require '<%= to_path(api.qualified_name) %>/classes.rb'
|
||||
require '<%= to_path(api.qualified_name) %>/representations.rb'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
# <%= api.title %>
|
||||
#
|
||||
# <%= block_comment(api.description, 4, 1) %>
|
||||
#
|
||||
<% if api.documentation_link -%>
|
||||
# @see <%= api.documentation_link %>
|
||||
<% end -%>
|
||||
module <%= api.module_name %>
|
||||
VERSION = '<%= api.version %>'
|
||||
REVISION = '<%= api.revision %>'
|
||||
<% if api.auth && api.auth.oauth2 -%>
|
||||
<% for scope_string, scope in api.auth.oauth2.scopes -%>
|
||||
|
||||
# <%= scope.description %>
|
||||
<%= scope.constant %> = '<%= scope_string %>'
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
# Copyright 2015 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 'date'
|
||||
require 'google/apis/core/base_service'
|
||||
require 'google/apis/core/json_representation'
|
||||
require 'google/apis/core/hashable'
|
||||
require 'google/apis/errors'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module <%= api.module_name %>
|
||||
<% for cls in api.schemas.values.partition(&:variant).flatten -%>
|
||||
<%= indent(include('representation_stub', :cls => cls), 6) -%>
|
||||
<% end -%>
|
||||
<% for cls in api.schemas.values.partition(&:variant).flatten -%>
|
||||
<%= indent(include('representation', :cls => cls, :api => api), 6) -%>
|
||||
<% end -%>
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,60 @@
|
||||
# Copyright 2015 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 'google/apis/core/base_service'
|
||||
require 'google/apis/core/json_representation'
|
||||
require 'google/apis/core/hashable'
|
||||
require 'google/apis/errors'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module <%= api.module_name %>
|
||||
# <%= api.title %>
|
||||
#
|
||||
# <%= block_comment(api.description, 6, 2) %>
|
||||
#
|
||||
# @example
|
||||
# require '<%= to_path(api.qualified_name) %>'
|
||||
#
|
||||
# <%= api.name %> = <%= api.qualified_name %> # Alias the module
|
||||
# service = <%= api.name %>::<%= api.service_name %>.new
|
||||
#
|
||||
<% if api.documentation_link -%>
|
||||
# @see <%= api.documentation_link %>
|
||||
<% end -%>
|
||||
class <%= api.service_name %> < Google::Apis::Core::BaseService
|
||||
<% for param in api.parameters.values.reject {|p| p.name == 'fields'} -%>
|
||||
# @return [<%= param.generated_type %>]
|
||||
# <%= block_comment(param.description, 8, 2) %>
|
||||
attr_accessor :<%= param.generated_name %>
|
||||
|
||||
<% end -%>
|
||||
def initialize
|
||||
super('<%= api.root_url %>', '<%= api.service_path %>')
|
||||
end
|
||||
<% for api_method in api.all_methods -%>
|
||||
<%= indent(include('method', :api_method => api_method, :api => api), 8) -%>
|
||||
<% end -%>
|
||||
|
||||
protected
|
||||
|
||||
def apply_command_defaults(command)
|
||||
<% for param in api.parameters.values.reject {|p| p.name == 'fields'} -%>
|
||||
command.query['<%= param.name %>'] = <%= param.generated_name %> unless <%= param.generated_name %>.nil?
|
||||
<% end -%>
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user