Top Level Namespace

Defined Under Namespace

Modules: Global, RrxDev, Spec Classes: SwaggerConfig

Instance Method Summary collapse

Instance Method Details

#array(type, description = nil) ⇒ Object



38
39
40
# File 'lib/rrx_dev/rswag.rb', line 38

def array(type, description = nil)
  prop_type :array, description, items: type
end

#boolean(description = nil) ⇒ Object



34
35
36
# File 'lib/rrx_dev/rswag.rb', line 34

def boolean(description = nil)
  prop_type :boolean, description
end

#date_time(description = nil) ⇒ Object



30
31
32
# File 'lib/rrx_dev/rswag.rb', line 30

def date_time(description = nil)
  string description, format: :dateTime
end

#enum(vals, description = nil) ⇒ Object



42
43
44
# File 'lib/rrx_dev/rswag.rb', line 42

def enum(vals, description = nil)
  string description, enum: vals
end

#init_rrx_spec_rails!Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rrx_dev/spec/rails.rb', line 3

def init_rrx_spec_rails!
  loading_helper = caller_locations.find { |loc| loc.path.match?(/[\\\/](spec|rails)_helper.rb/) }
  raise 'rrx_dev/spec must be required by spec_helper.rb or rails_helper.rb' unless loading_helper

  spec_dir = Pathname.new(loading_helper.path).dirname.freeze
  app_env  = (spec_dir.glob('*/config/environment.rb').first || spec_dir.join('../config/environment.rb')).freeze
  raise "Could not find Rails environment file" unless app_env.exist?

  puts "Loading Rails environment from #{app_env}" if ENV['DEBUG']

  ENV['RAILS_ENV'] ||= 'test'
  require app_env.expand_path.to_s

  # Prevent database truncation if the environment is production
  abort('The Rails environment is running in production mode!') if Rails.env.production?

  require 'rspec/rails'

  if defined?(ActiveRecord)
    begin
      ActiveRecord::Migration.maintain_test_schema!
    rescue ActiveRecord::PendingMigrationError => e
      Rails.logger.debug e.to_s.strip
      exit 1 # rubocop:disable Rails/Exit
    end
  end

  Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }
end

#integer(description = nil) ⇒ Object



22
23
24
# File 'lib/rrx_dev/rswag.rb', line 22

def integer(description = nil)
  prop_type :integer, description
end

#object(optional: {}, additional: false, **properties) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/rrx_dev/rswag.rb', line 7

def object(optional: {}, additional: false, **properties)
  {
    type:                 :object,
    required:             properties.keys.map(&:to_s),
    properties:           properties.update(optional),
    additionalProperties: additional
  }.freeze
end

#prop_type(type, description = nil, **options) ⇒ Object



16
17
18
19
20
# File 'lib/rrx_dev/rswag.rb', line 16

def prop_type(type, description = nil, **options)
  t = { type: }.update(options)
  t[:description] = description if description
  t.freeze
end

#ref(to) ⇒ Object

Rswag schema helpers



3
4
5
# File 'lib/rrx_dev/rswag.rb', line 3

def ref(to)
  { '$ref': "#/components/schemas/#{to}" }
end

#string(description = nil, **options) ⇒ Object



26
27
28
# File 'lib/rrx_dev/rswag.rb', line 26

def string(description = nil, **options)
  prop_type :string, description, **options
end

#swagger(&block) ⇒ Object



71
72
73
74
75
# File 'lib/rrx_dev/rswag.rb', line 71

def swagger(&block)
  swagger_config = SwaggerConfig.new
  swagger_config.instance_exec(&block)
  swagger_config.config
end