Class: Boring::Rswag::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/boring/rswag/install/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_authentication_schemeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/generators/boring/rswag/install/install_generator.rb', line 74

def add_authentication_scheme
  if options[:skip_api_authentication]
    return
  end

  say "\nAdding Authentication for APIs", :green

  authentication_type = options[:authentication_type]

  if authentication_type == 'api_key'
    validate_api_authentication_options

    authentication_options = options[:api_authentication_options]
  end

  authentication_content = case authentication_type
  when 'bearer'
    <<~RUBY.chomp.indent(0)
      type: :http,
      scheme: :bearer
    RUBY
  when 'api_key'
    <<~RUBY
      type: :apiKey,
      name: "#{authentication_options['name']}",
      in: "#{authentication_options['in']}",
    RUBY
  else
    <<~RUBY
      type: :http,
      scheme: :basic
    RUBY
  end

  gsub_file "spec/swagger_helper.rb",
            /servers: \[.*\]/m do |match|
    configuration_content = <<~RUBY.indent(6)
    components: {
      securitySchemes: {
        authorization: {\n#{authentication_content.chomp.indent(6)}
        }
      }
    },
    security: [
      authorization: []
    ]
    RUBY

    match << ",\n#{configuration_content.chomp}"
  end
end

#add_rswag_gemsObject



41
42
43
44
45
46
47
# File 'lib/generators/boring/rswag/install/install_generator.rb', line 41

def add_rswag_gems
  say "Adding rswag gems to Gemfile", :green

  gem 'rswag-api'
  gem 'rswag-ui'
  gem 'rswag-specs', group: [:development, :test]
end

#enable_swagger_ui_authenticationObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/generators/boring/rswag/install/install_generator.rb', line 126

def enable_swagger_ui_authentication
  return unless options[:enable_swagger_ui_authentication]

  say "\nAdding Basic Authentication to secure the UI", :green

  uncomment_lines 'config/initializers/rswag_ui.rb', /c.basic_auth_enabled/
  uncomment_lines 'config/initializers/rswag_ui.rb', /c.basic_auth_credentials/
  gsub_file "config/initializers/rswag_ui.rb",
            "c.basic_auth_credentials 'username', 'password'",
            'c.basic_auth_credentials Rails.application.credentials.dig(:swagger_ui, :username), Rails.application.credentials.dig(:swagger_ui, :password)'

  say "❗️❗️\nusername will be used from `Rails.application.credentials.dig(:swagger_ui, :username)` and password from `Rails.application.credentials.dig(:swagger_ui, :password)`. You can change these values if they don't match with your app.\n", :yellow
end

#install_rswagObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/boring/rswag/install/install_generator.rb', line 49

def install_rswag
  say "\nRunning rswag install generator to add required files", :green

  Bundler.with_unbundled_env do
    run 'bundle install'

    generate 'rswag:api:install'
    generate 'rswag:ui:install'
    generate 'rswag:specs:install', [env: 'test']
  end
end

#show_readmeObject



140
141
142
# File 'lib/generators/boring/rswag/install/install_generator.rb', line 140

def show_readme
  readme "README"
end

#update_api_hostObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/boring/rswag/install/install_generator.rb', line 61

def update_api_host
  say "\nUpdating API Host URL", :green

  rails_port = options[:rails_port]

  gsub_file "spec/swagger_helper.rb",
            "url: 'https://{defaultHost}'",
            "url: 'http://{defaultHost}'"
  gsub_file "spec/swagger_helper.rb",
            "default: 'www.example.com'",
            "default: 'localhost:#{rails_port}'"
end

#verify_presence_of_rspec_gemObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/boring/rswag/install/install_generator.rb', line 29

def verify_presence_of_rspec_gem
  gem_file_content_array = File.readlines("Gemfile")

  rspec_is_installed = gem_file_content_array.any? { |line| line.include?("rspec-rails") }

  return if rspec_is_installed

  say "We couldn't find rspec-rails gem which is a dependency for the rswag gem. Please configure rspec and run the generator again!"

  abort
end