Class: LambdaOpenApi::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_open_api/formatter.rb

Class Method Summary collapse

Class Method Details

.add_request(resource) ⇒ Object



8
9
10
11
# File 'lib/lambda_open_api/formatter.rb', line 8

def add_request(resource)
  @paths[resource.path_name] ||= {}
  @paths[resource.path_name][resource.http_verb] = resource.action_json
end

.create_file(file_name, file_content) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lambda_open_api/formatter.rb', line 52

def create_file(file_name, file_content)
  dirname = File.dirname(file_name)
  p dirname
  unless File.directory?(dirname)
    p 'to create'
    p FileUtils.mkdir_p(dirname)
  end


  File.open(file_name, "w") {|f| f.write(JSON.pretty_generate(file_content) + "\n") }
end

.generate_docsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lambda_open_api/formatter.rb', line 13

def generate_docs




  if uses_file_sets?
    LambdaOpenApi.configuration.file_sets.each do |file_set|
      LambdaOpenApi.configuration.host = file_set[:host]

      open_api = high_level_structure.merge({"paths" => @paths})

      create_file(file_set[:file_name], open_api)
    end
  else
    open_api = high_level_structure.merge({"paths" => @paths})
    create_file(LambdaOpenApi.configuration.file_name, open_api)
  end
end

.high_level_structureObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lambda_open_api/formatter.rb', line 32

def high_level_structure
  {
    "swagger" => "2.0",
    "info" => {
      "title" => LambdaOpenApi.configuration.title,
      "description" => LambdaOpenApi.configuration.description,
      "version" => LambdaOpenApi.configuration.version
    },
    "host" => LambdaOpenApi.configuration.host,
    "schemes" => LambdaOpenApi.configuration.schemes,
    "consumes" => LambdaOpenApi.configuration.consumes,
    "produces" => LambdaOpenApi.configuration.produces
  }
end

.uses_file_sets?Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/lambda_open_api/formatter.rb', line 47

def uses_file_sets?
  LambdaOpenApi.configuration.file_sets&.is_a?(Array) &&
  LambdaOpenApi.configuration.file_sets.any?
end