Class: AppMap::Swagger::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/appmap/swagger/configuration.rb

Constant Summary collapse

DEFAULT_VERSION =
'1.0'
DEFAULT_OUTPUT_DIR =
'swagger'
DEFAULT_DESCRIPTION =
'Generate Swagger from AppMaps'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



27
28
29
30
31
32
# File 'lib/appmap/swagger/configuration.rb', line 27

def initialize
  @project_name = nil
  @project_version = DEFAULT_VERSION
  @output_dir = DEFAULT_OUTPUT_DIR
  @description = DEFAULT_DESCRIPTION
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/appmap/swagger/configuration.rb', line 12

def description
  @description
end

#output_dirObject

Returns the value of attribute output_dir.



12
13
14
# File 'lib/appmap/swagger/configuration.rb', line 12

def output_dir
  @output_dir
end

#project_nameObject



34
35
36
# File 'lib/appmap/swagger/configuration.rb', line 34

def project_name
  @project_name || default_project_name
end

#project_versionObject

Returns the value of attribute project_version.



12
13
14
# File 'lib/appmap/swagger/configuration.rb', line 12

def project_version
  @project_version
end

#templateObject



38
39
40
# File 'lib/appmap/swagger/configuration.rb', line 38

def template
  @template || default_template
end

Class Method Details

.load(config_data) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/appmap/swagger/configuration.rb', line 18

def load(config_data)
  Configuration.new.tap do |config|
    config_data.each do |k,v|
      config.send "#{k}=", v
    end
  end
end

Instance Method Details

#default_project_nameObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/appmap/swagger/configuration.rb', line 58

def default_project_name
  # https://www.rubydoc.info/docs/rails/Module#module_parent_name-instance_method
  module_parent_name = ->(cls) { cls.name =~ /::[^:]+\Z/ ? $`.freeze : nil }

  # Lazy-evaluate this so that Rails.application will be defined.
  # If this code runs too early in the lifecycle, Rails.application is nil.
  if defined?(::Rails)
    [module_parent_name.(::Rails.application.class).humanize.titleize, "API"].join(" ")
  else
    "MyProject API"
  end
end

#default_templateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/appmap/swagger/configuration.rb', line 42

def default_template
  YAML.load <<~TEMPLATE
    openapi: 3.0.1
    info:
      title: #{project_name}
      version: #{project_version}
    paths:
    components:
    servers:
    - url: http://{defaultHost}
      variables:
        defaultHost:
          default: localhost:3000
    TEMPLATE
end