class HaveAPI::GoClient::Generator

Attributes

api[R]
dst[R]

Destination directory @return [String]

module[R]

Go module name @return [String]

package[R]

Go package name @return [String]

Public Class Methods

new(url, dst, opts) click to toggle source

@param url [String] API URL @param dst [String] destination directory @param opts [Hash] @option opts [String] :version @option opts [String] :module @option opts [String] :package

# File lib/haveapi/go_client/generator.rb, line 24
def initialize(url, dst, opts)
  @dst = dst
  @module = opts[:module]
  @package = opts[:package]

  conn = HaveAPI::Client::Communicator.new(url)
  @api = ApiVersion.new(conn.describe_api(opts[:version]))
end

Public Instance Methods

generate() click to toggle source
# File lib/haveapi/go_client/generator.rb, line 33
def generate
  FileUtils.mkpath(dst)

  if self.module
    ErbTemplate.render_to_if_changed(
      'go.mod',
      {mod: self.module},
      File.join(dst, 'go.mod')
    )

    @dst = File.join(dst, package)
    FileUtils.mkpath(dst)
  end

  %w(client authentication request response types).each do |v|
    ErbTemplate.render_to_if_changed(
      "#{v}.go",
      {
        package: package,
        api: api,
      },
      File.join(dst, "#{v}.go")
    )
  end

  api.resources.each { |r| r.generate(self) }
  api.auth_methods.each { |v| v.generate(self) }
end