Class: Apicraft::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/apicraft/loader.rb

Overview

Recursively loads and processes YAML files from the application’s contract folder during the application boot. This class is responsible for loading and initializing the contracts defined in the YAML files.

Class Method Summary collapse

Class Method Details

.configObject



25
26
27
# File 'lib/apicraft/loader.rb', line 25

def self.config
  Apicraft.config
end

.load!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/apicraft/loader.rb', line 9

def self.load!
  contracts_path = Apicraft.config.contracts_path
  return if contracts_path.blank?

  raise Errors::InvalidContractsPath unless Dir.exist?(contracts_path)

  Find.find(contracts_path) do |path|
    next unless File.file?(path) && %w[.yaml .yml .json].include?(File.extname(path))

    load_file!(path)

    route = path.sub(contracts_path.to_s, "")
    Web::Router.add(route, path)
  end
end

.load_file!(file) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/apicraft/loader.rb', line 29

def self.load_file!(file)
  ext = File.extname(file)

  parsed = if ext == ".json"
             JSON.parse(File.read(file))
           else
             YAML.load_file(file)
           end

  OpenAPIParser.parse(
    parsed,
    {
      strict_reference_validation: config.strict_reference_validation
    }
  )

  Openapi::Contract.create!(
    OpenAPIParser.parse(parsed)
  )
end