Class: HaveAPI::GoClient::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/go_client/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, dst, opts) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • url (String)

    API URL

  • dst (String)

    destination directory

  • opts (Hash)

Options Hash (opts):

  • :version (String)
  • :module (String)
  • :package (String)


24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#dstString (readonly)

Destination directory

Returns:

  • (String)


8
9
10
# File 'lib/haveapi/go_client/generator.rb', line 8

def dst
  @dst
end

#moduleString (readonly)

Go module name

Returns:

  • (String)


12
13
14
# File 'lib/haveapi/go_client/generator.rb', line 12

def module
  @module
end

#packageString (readonly)

Go package name

Returns:

  • (String)


16
17
18
# File 'lib/haveapi/go_client/generator.rb', line 16

def package
  @package
end

Instance Method Details

#generateObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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:,
        api:
      },
      File.join(dst, "#{v}.go")
    )
  end

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

#go_fmtObject



62
63
64
65
66
# File 'lib/haveapi/go_client/generator.rb', line 62

def go_fmt
  return if system('go', 'fmt', chdir: dst)

  raise 'go fmt failed'
end