Class: Gapic::Presenters::SnippetPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/presenters/snippet_presenter.rb,
lib/gapic/presenters/snippet/type_presenter.rb,
lib/gapic/presenters/snippet/iteration_presenter.rb,
lib/gapic/presenters/snippet/parameter_presenter.rb,
lib/gapic/presenters/snippet/statement_presenter.rb,
lib/gapic/presenters/snippet/expression_presenter.rb,
lib/gapic/presenters/snippet/client_call_presenter.rb,
lib/gapic/presenters/snippet/declaration_presenter.rb,
lib/gapic/presenters/snippet/response_handling_presenters.rb,
lib/gapic/presenters/snippet/client_initialization_presenter.rb,
lib/gapic/presenters/snippet/request_initialization_presenters.rb

Overview

A presenter for snippets.

Defined Under Namespace

Modules: ResponseHandlingPresenterCommon Classes: ClientCallPresenter, ClientInitializationPresenter, DeclarationPresenter, ExpressionPresenter, IterationPresenter, LroResponseHandlingPresenter, PaginatedResponseHandlingPresenter, ParameterPresenter, SimpleRequestInitializationPresenter, SimpleResponseHandlingPresenter, StatementPresenter, StreamingRequestInitializationPresenter, StreamingResponseHandlingPresenter, TypePresenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_presenter, api, config: nil, transport: nil) ⇒ SnippetPresenter

Returns a new instance of SnippetPresenter.



36
37
38
39
40
41
42
# File 'lib/gapic/presenters/snippet_presenter.rb', line 36

def initialize method_presenter, api, config: nil, transport: nil
  @method_presenter = method_presenter
  @api = api
  @config = config
  @transport = transport || @api.default_transport
  analyze_config
end

Instance Attribute Details

#client_callObject (readonly)

Returns the value of attribute client_call.



155
156
157
# File 'lib/gapic/presenters/snippet_presenter.rb', line 155

def client_call
  @client_call
end

#client_initializationObject (readonly)

Returns the value of attribute client_initialization.



153
154
155
# File 'lib/gapic/presenters/snippet_presenter.rb', line 153

def client_initialization
  @client_initialization
end

#configObject (readonly)

Returns the value of attribute config.



44
45
46
# File 'lib/gapic/presenters/snippet_presenter.rb', line 44

def config
  @config
end

#final_statementsObject (readonly)

Returns the value of attribute final_statements.



157
158
159
# File 'lib/gapic/presenters/snippet_presenter.rb', line 157

def final_statements
  @final_statements
end

#request_initializationObject (readonly)

Returns the value of attribute request_initialization.



154
155
156
# File 'lib/gapic/presenters/snippet_presenter.rb', line 154

def request_initialization
  @request_initialization
end

#response_handlingObject (readonly)

Returns the value of attribute response_handling.



156
157
158
# File 'lib/gapic/presenters/snippet_presenter.rb', line 156

def response_handling
  @response_handling
end

#transportObject (readonly)

Returns the value of attribute transport.



45
46
47
# File 'lib/gapic/presenters/snippet_presenter.rb', line 45

def transport
  @transport
end

Instance Method Details

#base_response_typeObject



102
103
104
# File 'lib/gapic/presenters/snippet_presenter.rb', line 102

def base_response_type
  @method_presenter.return_type
end

#client_streaming?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/gapic/presenters/snippet_presenter.rb', line 51

def client_streaming?
  @method_presenter.client_streaming?
end

#client_typeObject



79
80
81
82
83
# File 'lib/gapic/presenters/snippet_presenter.rb', line 79

def client_type
  service = @method_presenter.service
  service = service.rest if transport == :rest
  service.client_name_full.sub(/^::/, "")
end

#config?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/gapic/presenters/snippet_presenter.rb', line 47

def config?
  !config.nil?
end

#descriptionObject



121
122
123
124
# File 'lib/gapic/presenters/snippet_presenter.rb', line 121

def description
  @config&.&.snippet_description ||
    "This is an auto-generated example demonstrating basic usage of #{client_type}##{method_name}."
end

#disclaimerObject



126
127
128
129
130
131
# File 'lib/gapic/presenters/snippet_presenter.rb', line 126

def disclaimer
  "This snippet has been automatically generated and should be regarded as a code template only. " \
    "It will require modifications to work:\n" \
    "- It may require correct/in-range values for request initialization.\n" \
    "- It may require specifying regional endpoints when creating the service client as shown in https://cloud.google.com/ruby/docs/reference."
end

#method_nameObject

TODO: Determine type of LRO response



108
109
110
# File 'lib/gapic/presenters/snippet_presenter.rb', line 108

def method_name
  @method_presenter.name
end

#paged_response_typeObject



98
99
100
# File 'lib/gapic/presenters/snippet_presenter.rb', line 98

def paged_response_type
  @method_presenter.paged_response_type
end

#region_tagObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/gapic/presenters/snippet_presenter.rb', line 159

def region_tag
  gem_presenter = @method_presenter.service.gem
  prefix = gem_presenter.doc_tag_prefix
  names = gem_presenter.name.split "-"
  final_name = names.pop
  if final_name =~ /^v\d/
    api_version = final_name
    prefix ||= names.last
  else
    prefix ||= final_name
    api_version = "v0"
  end
  prefix = prefix.downcase.gsub(/[^a-z0-9]/, "")
  service_name = @method_presenter.service.module_name
  method_name = @method_presenter.method.name
  type = config? ? "config" : "generated"
  config_id = config? ? "#{@config..config_id}_" : ""
  "#{prefix}_#{api_version}_#{type}_#{service_name}_#{method_name}_#{config_id}sync"
end

#request_typeObject



89
90
91
# File 'lib/gapic/presenters/snippet_presenter.rb', line 89

def request_type
  @method_presenter.request_type.sub(/^::/, "")
end

#require_pathObject



75
76
77
# File 'lib/gapic/presenters/snippet_presenter.rb', line 75

def require_path
  @method_presenter.service.package.package_require
end

#response_kindObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gapic/presenters/snippet_presenter.rb', line 59

def response_kind
  if @method_presenter.server_streaming?
    :streaming
  elsif @method_presenter.paged?
    :paged
  elsif @method_presenter.lro?
    :lro
  else
    :simple
  end
end

#return_typeObject



93
94
95
96
# File 'lib/gapic/presenters/snippet_presenter.rb', line 93

def return_type
  base_type = @method_presenter.return_type.sub(/^::/, "")
  @method_presenter.server_streaming? ? "Enumerable<#{base_type}>" : base_type
end

#server_streaming?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/gapic/presenters/snippet_presenter.rb', line 55

def server_streaming?
  @method_presenter.server_streaming?
end

#service_name_shortObject



85
86
87
# File 'lib/gapic/presenters/snippet_presenter.rb', line 85

def service_name_short
  @method_presenter.service.module_name
end

#snippet_file_pathObject



71
72
73
# File 'lib/gapic/presenters/snippet_presenter.rb', line 71

def snippet_file_path
  "#{@method_presenter.service.service_require.split('/').last}/#{snippet_method_name}.rb"
end

#snippet_method_nameObject



112
113
114
# File 'lib/gapic/presenters/snippet_presenter.rb', line 112

def snippet_method_name
  config? ? "#{method_name}_#{snake_config_id}" : method_name
end

#snippet_method_parametersObject



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gapic/presenters/snippet_presenter.rb', line 133

def snippet_method_parameters
  @snippet_method_parameters ||= begin
    parameters_proto = @config&.signature&.parameters
    if parameters_proto
      parameters_json = @config.json_representation["signature"]["parameters"]
      parameters_proto.each_with_index.map do |param_proto, index|
        ParameterPresenter.new param_proto, parameters_json[index]
      end
    else
      []
    end
  end
end

#snippet_method_parameters_renderObject



147
148
149
150
151
# File 'lib/gapic/presenters/snippet_presenter.rb', line 147

def snippet_method_parameters_render
  return "" if snippet_method_parameters.empty?
  names = snippet_method_parameters.map { |param| "#{param.name}:" }.join ", "
  "(#{names})"
end

#snippet_nameObject



116
117
118
119
# File 'lib/gapic/presenters/snippet_presenter.rb', line 116

def snippet_name
  @config&.&.snippet_name ||
    "Snippet for the #{method_name} call in the #{service_name_short} service"
end