lib/gapic/presenters/service_presenter.rb in gapic-generator-0.9.1 vs lib/gapic/presenters/service_presenter.rb in gapic-generator-0.10.0
- old
+ new
@@ -49,10 +49,17 @@
def package
PackagePresenter.new @gem_presenter, @api, @service.parent.package
end
##
+ # @return [Boolean]
+ #
+ def is_deprecated?
+ @service.is_deprecated?
+ end
+
+ ##
# @return [Enumerable<Gapic::Presenters::MethodPresenter>]
#
def methods
@methods ||= @service.methods.map { |m| MethodPresenter.new self, @api, m }
end
@@ -250,10 +257,38 @@
def references
@references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name)
end
+ ##
+ # Deduplicate resource presenters by combining resources with the same
+ # name. If multiple resources have the same name (though possibly
+ # different namespaces, e.g. `location.googleapis.com/Location` vs
+ # `documentai.googleapis.com/Location`), this combines (and dedups) their
+ # patterns into a single resource presenter.
+ #
+ # Used for generating path helpers while avoiding duplicate method names.
+ #
+ def deduped_references
+ @deduped_references ||= begin
+ hash = {}
+ references.each do |resource|
+ if hash.key? resource.name
+ existing = hash[resource.name]
+ resource.patterns.each do |pat|
+ unless existing.patterns.any? { |epat| epat.pattern_template == pat.pattern_template }
+ existing.patterns << pat
+ end
+ end
+ else
+ hash[resource.name] = resource.dup
+ end
+ end
+ hash.values
+ end
+ end
+
def paths?
references.any?
end
def paths_name
@@ -419,9 +454,25 @@
def quick_start_method
gem_config = @api.configuration[:gem]
preferred_method = gem_config[:quick_start_method] if gem_config
result = methods.find { |meth| meth.name == preferred_method } if preferred_method
result || methods.find { |meth| !meth.client_streaming? }
+ end
+
+ ##
+ # Returns this service presenter if there is a grpc client. Otherwise,
+ # returns the corresponding rest service presenter if there isn't a grpc
+ # client but there is a rest client. Otherwise, returns nil if there is
+ # neither client.
+ #
+ # @return [ServicePresenter,ServiceRestPresenter,nil]
+ #
+ def usable_service_presenter
+ if @api.generate_grpc_clients?
+ self
+ elsif @api.generate_rest_clients? && methods_rest_bindings?
+ rest
+ end
end
private
def default_config key