Class: Gapic::PackageSnippets

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/package_snippets.rb

Overview

An object that collects snippets for a package and generates the metadata content.

Instance Method Summary collapse

Constructor Details

#initialize(proto_package:, gem_name:, snippet_dir: "snippets") ⇒ PackageSnippets

Start collecting snippets for a package

Parameters:

  • proto_package (String)

    Fully qualified proto package name including API version, e.g. google.cloud.translate.v3. Required.

  • gem_name (String)

    Name of the gem being generated, e.g. google-cloud-translate-v3. Required.

  • snippet_dir (String) (defaults to: "snippets")

    Directory where snippets are generated. Optional. Defaults to snippets.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gapic/package_snippets.rb', line 38

def initialize proto_package:, gem_name:, snippet_dir: "snippets"
  api = SnippetIndex::Api.new(
    id: proto_package,
    version: proto_package.split(".").last
  )
  client_library_object = SnippetIndex::ClientLibrary.new(
    name: gem_name,
    version: "",
    language: "RUBY",
    apis: [api]
  )
  @index_object = SnippetIndex::Index.new(
    client_library: client_library_object
  )
  @metadata_filename = "#{snippet_dir}/snippet_metadata_#{proto_package}.json"
  @snippet_files = []
end

Instance Method Details

#add(method_presenter:, snippet_presenter:, snippet_file:) ⇒ self

Add a snippet to the collection

Parameters:

Returns:

  • (self)


64
65
66
67
68
69
70
71
# File 'lib/gapic/package_snippets.rb', line 64

def add method_presenter:, snippet_presenter:, snippet_file:
  service_presenter = method_presenter.service
  snippet_lines = [""] + snippet_file.content.split("\n")
  @index_object.snippets <<
    build_snippet_object(snippet_presenter, method_presenter, service_presenter, snippet_lines)
  @snippet_files << snippet_file
  self
end

#filesArray<Google::Protobuf::Compiler::CodeGeneratorResponse::File>

Return a list of files to render for this package. This includes all the snippet files themselves, and the metadata file.

Returns:

  • (Array<Google::Protobuf::Compiler::CodeGeneratorResponse::File>)


79
80
81
82
83
84
85
86
# File 'lib/gapic/package_snippets.rb', line 79

def files
   = JSON.pretty_generate json_value_for @index_object
   = Google::Protobuf::Compiler::CodeGeneratorResponse::File.new(
    name: @metadata_filename,
    content: 
  )
  @snippet_files + []
end