Class: HaveAPI::GoClient::Generator
- Inherits:
-
Object
- Object
- HaveAPI::GoClient::Generator
- Includes:
- Utils
- Defined in:
- lib/haveapi/go_client/generator.rb
Constant Summary
Constants included from Utils
Instance Attribute Summary collapse
-
#dst ⇒ String
readonly
Destination directory.
-
#module ⇒ String
readonly
Go module name.
-
#package ⇒ String
readonly
Go package name.
Instance Method Summary collapse
- #generate ⇒ Object
- #go_fmt ⇒ Object
-
#initialize(url, dst, opts) ⇒ Generator
constructor
A new instance of Generator.
Methods included from Utils
#camelize, #go_comment_text, #go_json_tag, #go_module_path, #go_package_name, #go_query_key, #go_string_literal, #safe_file_component
Constructor Details
#initialize(url, dst, opts) ⇒ Generator
Returns a new instance of Generator.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/haveapi/go_client/generator.rb', line 27 def initialize(url, dst, opts) @dst = dst @module = opts[:module] && go_module_path(opts[:module]) @package = go_package_name(opts[:package]) conn = HaveAPI::Client::Communicator.new(url) if opts[:basic_user] && opts[:basic_password] conn.authenticate(:basic, user: opts[:basic_user], password: opts[:basic_password]) end @api = ApiVersion.new(conn.describe_api(opts[:version])) end |
Instance Attribute Details
#dst ⇒ String (readonly)
Destination directory
11 12 13 |
# File 'lib/haveapi/go_client/generator.rb', line 11 def dst @dst end |
#module ⇒ String (readonly)
Go module name
15 16 17 |
# File 'lib/haveapi/go_client/generator.rb', line 15 def module @module end |
#package ⇒ String (readonly)
Go package name
19 20 21 |
# File 'lib/haveapi/go_client/generator.rb', line 19 def package @package end |
Instance Method Details
#generate ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/haveapi/go_client/generator.rb', line 39 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_fmt ⇒ Object
68 69 70 71 72 |
# File 'lib/haveapi/go_client/generator.rb', line 68 def go_fmt return if system('go', 'fmt', chdir: dst) raise 'go fmt failed' end |