class Gapic::Presenters::ServicePresenter

A presenter for proto services.

Attributes

rest[R]

@return [Gapic::Presenters::ServiceRestPresenter]

Public Class Methods

new(gem_presenter, api, service, parent_service: nil) click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 33
def initialize gem_presenter, api, service, parent_service: nil
  @gem_presenter = gem_presenter
  @api = api
  @service = service
  @parent_service = parent_service
  @rest = ServiceRestPresenter.new self, api
end

Public Instance Methods

address() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 67
def address
  @service.address
end
client_endpoint() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 202
def client_endpoint
  return nil if generic_endpoint?
  @parent_service&.client_endpoint ||
    common_service_delegate&.client_endpoint ||
    @service.host ||
    default_config(:default_host) ||
    "localhost"
end
client_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 198
def client_file_name
  client_file_path.split("/").last
end
client_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 194
def client_file_path
  "#{client_require}.rb"
end
client_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 174
def client_name
  "Client"
end
client_name_full() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 178
def client_name_full
  fix_namespace @api, "#{service_name_full}::#{client_name}"
end
client_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 190
def client_require
  ruby_file_path @api, client_name_full
end
client_scopes() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 215
def client_scopes
  common_service_delegate&.client_scopes ||
    @service.scopes ||
    default_config(:oauth_scopes) ||
    []
end
common_service_delegate() click to toggle source

Returns a presenter for this service's delegate (if it is a common service) otherwise returns `nil`.

# File lib/gapic/presenters/service_presenter.rb, line 73
def common_service_delegate
  unless defined? @common_service_delegate
    delegate = @api.delegate_service_for @service
    @common_service_delegate = delegate ? ServicePresenter.new(@gem_presenter, @api, delegate) : nil
  end
  @common_service_delegate
end
config_channel_args() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 390
def config_channel_args
  { "grpc.service_config_disable_resolution" => 1 }
end
configure_client_call() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 186
def configure_client_call
  "#{client_name_full}.configure"
end
create_client_call() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 182
def create_client_call
  "#{client_name_full}.new"
end
credentials_class_xref() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 230
def credentials_class_xref
  "{#{credentials_name_full}}"
end
credentials_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 238
def credentials_file_name
  credentials_file_path.split("/").last
end
credentials_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 234
def credentials_file_path
  "#{credentials_require}.rb"
end
credentials_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 222
def credentials_name
  "Credentials"
end
credentials_name_full() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 226
def credentials_name_full
  fix_namespace @api, "#{service_name_full}::#{credentials_name}"
end
credentials_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 242
def credentials_require
  ruby_file_path @api, credentials_name_full
end
deduped_references() click to toggle source

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.

# File lib/gapic/presenters/service_presenter.rb, line 271
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
doc_description(disable_xrefs: false) click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 100
def doc_description disable_xrefs: false
  @service.docs_leading_comments disable_xrefs: disable_xrefs
end
drift_manifest() click to toggle source

Returns a hash with a drift_manifest of this service, describing correspondence between the proto description of the service with the generated code for the service. See github.com/googleapis/googleapis/blob/master/gapic/metadata/gapic_metadata.proto

@return [Hash]

# File lib/gapic/presenters/service_presenter.rb, line 422
def drift_manifest
  {
    clients: {
      grpc: {
        libraryClient: client_name_full,
        # The methods should grouped by grpc_method_name and then
        # their names are returned together in an array.
        # For Ruby currently we have 1:1 proto to code
        # correspondence for methods, so our generation is easier
        rpcs:       methods.map { |m| [m.grpc_method_name, m.drift_manifest] }.to_h
      }
    }
  }
end
file() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 45
def file
  FilePresenter.new @api, @service.parent
end
gem() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 41
def gem
  @gem_presenter
end
generate_grpc_clients?() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 318
def generate_grpc_clients?
  @api.generate_grpc_clients?
end
generate_rest_clients?() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 314
def generate_rest_clients?
  @api.generate_rest_clients?
end
generic_endpoint?() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 211
def generic_endpoint?
  gem.generic_endpoint?
end
grpc_client_designation() click to toggle source

How comments in the generated libraries refer to the GRPC client if no REST code is generated, this should just be “client”, if REST code is generated, this should be disambiguated into the “GRPC client”

@return [String]

# File lib/gapic/presenters/service_presenter.rb, line 443
def grpc_client_designation
  generate_rest_clients? ? "GRPC client" : "client"
end
grpc_full_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 411
def grpc_full_name
  @service.address.join "."
end
grpc_service_config() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 394
def grpc_service_config
  return unless @api.grpc_service_config&.service_level_configs&.key? grpc_full_name
  @api.grpc_service_config.service_level_configs[grpc_full_name]
end
grpc_service_name() click to toggle source

The short proto name for this service

@return [String]

# File lib/gapic/presenters/service_presenter.rb, line 407
def grpc_service_name
  @service.name
end
helpers_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 250
def helpers_file_name
  "helpers.rb"
end
helpers_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 246
def helpers_file_path
  "#{helpers_require}.rb"
end
helpers_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 254
def helpers_require
  ruby_file_path @api, "#{service_name_full}::Helpers"
end
is_deprecated?() click to toggle source

@return [Boolean]

# File lib/gapic/presenters/service_presenter.rb, line 56
def is_deprecated?
  @service.is_deprecated?
end
lro?() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 353
def lro?
  methods.find(&:lro?)
end
lro_client_ivar() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 361
def lro_client_ivar
  "@#{lro_client_var}"
end
lro_client_var() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 357
def lro_client_var
  "operations_client"
end
lro_service() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 385
def lro_service
  lro = @service.parent.parent.files.find { |file| file.name == "google/longrunning/operations.proto" }
  return ServicePresenter.new @gem_presenter, @api, lro.services.first, parent_service: self unless lro.nil?
end
methods() click to toggle source

@return [Enumerable<Gapic::Presenters::MethodPresenter>]

# File lib/gapic/presenters/service_presenter.rb, line 63
def methods
  @methods ||= @service.methods.map { |m| MethodPresenter.new self, @api, m }
end
methods_rest_bindings() click to toggle source

@return [Enumerable<Gapic::Presenters::MethodPresenter>]

List of mods for which REST bindings are present and REST methods can be generated
# File lib/gapic/presenters/service_presenter.rb, line 333
def methods_rest_bindings
  methods.select { |method| method.rest.path? && method.rest.verb? }
end
methods_rest_bindings?() click to toggle source

@return [Boolean] whether this service contains any methods with REST bindings

# File lib/gapic/presenters/service_presenter.rb, line 325
def methods_rest_bindings?
  methods_rest_bindings.any?
end
module_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 122
def module_name
  service_name_full.split("::").last
end
name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 104
def name
  @api.fix_service_name @service.name
end
namespace() click to toggle source

The namespace of the client. Normally this is the version module. This may be different from the proto namespace for a common service.

# File lib/gapic/presenters/service_presenter.rb, line 83
def namespace
  # If this service is a common service, its client should go into its
  # delegate's namespace rather than its own. For example, KMS includes
  # the common IAMPolicy service, but that service's client should go
  # into the KMS namespace.
  return common_service_delegate.namespace if common_service_delegate

  return ensure_absolute_namespace @service.ruby_package if @service.ruby_package.present?

  namespace = ruby_namespace_for_address @service.address[0...-1]
  fix_namespace @api, namespace
end
operations_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 377
def operations_file_name
  operations_file_path.split("/").last
end
operations_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 373
def operations_file_path
  "#{operations_require}.rb"
end
operations_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 365
def operations_name
  "Operations"
end
operations_name_full() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 369
def operations_name_full
  fix_namespace @api, "#{service_name_full}::#{operations_name}"
end
operations_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 381
def operations_require
  ruby_file_path @api, "#{service_name_full}::#{operations_name}"
end
package() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 49
def package
  PackagePresenter.new @gem_presenter, @api, @service.parent.package
end
paths?() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 290
def paths?
  references.any?
end
paths_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 306
def paths_file_name
  paths_file_path.split("/").last
end
paths_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 302
def paths_file_path
  "#{paths_require}.rb"
end
paths_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 294
def paths_name
  "Paths"
end
paths_name_full() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 298
def paths_name_full
  fix_namespace @api, "#{service_name_full}::#{paths_name}"
end
paths_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 310
def paths_require
  ruby_file_path @api, "#{service_name_full}::#{paths_name}"
end
proto_namespace() click to toggle source

The namespace of the protos. This may be different from the client namespace for a common service.

# File lib/gapic/presenters/service_presenter.rb, line 110
def proto_namespace
  return ensure_absolute_namespace @service.ruby_package if @service.ruby_package.present?

  namespace = ruby_namespace_for_address @service.address[0...-1]
  @api.override_proto_namespaces? ? fix_namespace(@api, namespace) : namespace
end
proto_service_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 130
def proto_service_file_name
  proto_service_file_path.split("/").last
end
proto_service_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 126
def proto_service_file_path
  @service.parent.name.sub ".proto", "_pb.rb"
end
proto_service_name_full() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 117
def proto_service_name_full
  name_full = "#{proto_namespace}::#{@service.name}"
  @api.override_proto_namespaces? ? fix_namespace(@api, name_full) : name_full
end
proto_service_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 134
def proto_service_require
  proto_service_file_path.sub ".rb", ""
end
proto_service_stub_name_full() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 150
def proto_service_stub_name_full
  "#{proto_service_name_full}::Stub"
end
proto_services_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 142
def proto_services_file_name
  proto_services_file_path.split("/").last
end
proto_services_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 138
def proto_services_file_path
  @service.parent.name.sub ".proto", "_services_pb.rb"
end
proto_services_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 146
def proto_services_require
  proto_services_file_path.sub ".rb", ""
end
quick_start_method() click to toggle source

The method to use for quick start samples. Normally this is simply the first non-client-streaming method defined, but it can be overridden via a gem config.

@return [Gapic::Presenters::MethodPresenter]

# File lib/gapic/presenters/service_presenter.rb, line 454
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
references() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 258
def references
  @references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name)
end
service_config_presenter() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 399
def service_config_presenter
  ServiceConfigPresenter.new grpc_service_config
end
service_directory_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 166
def service_directory_name
  service_require.split("/").last
end
service_file_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 162
def service_file_name
  service_file_path.split("/").last
end
service_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 158
def service_file_path
  "#{service_require}.rb"
end
service_name_full() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 154
def service_name_full
  fix_namespace @api, "#{namespace}::#{name}"
end
service_require() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 170
def service_require
  ruby_file_path @api, service_name_full
end
stub_name() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 349
def stub_name
  "#{ActiveSupport::Inflector.underscore name}_stub"
end
test_client_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 337
def test_client_file_path
  service_file_path.sub ".rb", "_test.rb"
end
test_client_operations_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 345
def test_client_operations_file_path
  service_file_path.sub ".rb", "_operations_test.rb"
end
test_paths_file_path() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 341
def test_paths_file_path
  service_file_path.sub ".rb", "_paths_test.rb"
end
usable_service_presenter() click to toggle source

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]

# File lib/gapic/presenters/service_presenter.rb, line 469
def usable_service_presenter
  if @api.generate_grpc_clients?
    self
  elsif @api.generate_rest_clients? && methods_rest_bindings?
    rest
  end
end
version() click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 96
def version
  ActiveSupport::Inflector.camelize @service.address[-2]
end

Private Instance Methods

default_config(key) click to toggle source
# File lib/gapic/presenters/service_presenter.rb, line 479
def default_config key
  return unless @service.parent.parent.configuration[:defaults]
  return unless @service.parent.parent.configuration[:defaults][:service]

  @service.parent.parent.configuration[:defaults][:service][key]
end