Class: Swagger::IO::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-swagger/io/file_system.rb

Constant Summary collapse

DOC_SUBPARTS =
%w(responses security tags)
@@default_path =
'./doc/swagger'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(swagger_doc) ⇒ FileSystem

Returns a new instance of FileSystem.



53
54
55
# File 'lib/ruby-swagger/io/file_system.rb', line 53

def initialize(swagger_doc)
  @doc = swagger_doc
end

Class Method Details

.all_files(pattern) ⇒ Object



45
46
47
# File 'lib/ruby-swagger/io/file_system.rb', line 45

def self.all_files(pattern)
  Dir["#{@@default_path}/#{pattern}"]
end

.default_pathObject



18
19
20
# File 'lib/ruby-swagger/io/file_system.rb', line 18

def self.default_path
  @@default_path
end

.default_path=(new_path) ⇒ Object



14
15
16
# File 'lib/ruby-swagger/io/file_system.rb', line 14

def self.default_path=(new_path)
  @@default_path = new_path
end

.delete_file(file) ⇒ Object



49
50
51
# File 'lib/ruby-swagger/io/file_system.rb', line 49

def self.delete_file(file)
  FileUtils.rm_f(file)
end

.file_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ruby-swagger/io/file_system.rb', line 41

def self.file_exists?(name)
  File.exist?(@@default_path + '/' + name)
end

.init_fs_structureObject



22
23
24
# File 'lib/ruby-swagger/io/file_system.rb', line 22

def self.init_fs_structure
  FileUtils.mkdir_p(@@default_path) unless Dir.exist?(@@default_path)
end

.readObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby-swagger/io/file_system.rb', line 70

def self.read
  doc = read_file('base_doc.yml')

  DOC_SUBPARTS.each do |doc_part|
    file_name = "#{doc_part}.yml"
    doc[doc_part] = read_file(file_name) if File.exist?("#{default_path}/#{file_name}")
  end

  doc['paths'] = Swagger::IO::Paths.read_paths
  doc['definitions'] = Swagger::IO::Definitions.read_definitions
  doc['securityDefinitions'] = Swagger::IO::Security.read_security_definitions

  Swagger::Data::Document.parse(doc)
end

.read_file(name) ⇒ Object



26
27
28
# File 'lib/ruby-swagger/io/file_system.rb', line 26

def self.read_file(name)
  YAML.load(ERB.new(File.read(@@default_path + '/' + name)).result)
end

.write_file(content, location, overwrite = false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby-swagger/io/file_system.rb', line 30

def self.write_file(content, location, overwrite = false)
  file_path = @@default_path + '/' + location

  return if !overwrite && File.exist?(file_path)

  dir_path = File.dirname(file_path)

  FileUtils.mkdir_p(dir_path) unless Dir.exist?(dir_path)
  File.open(file_path, 'w') { |f| f.write(content) }
end

Instance Method Details

#compile!Object



85
86
87
# File 'lib/ruby-swagger/io/file_system.rb', line 85

def compile!
  Swagger::IO::FileSystem.write_file(JSON.pretty_generate(@doc.to_swagger), 'swagger.json', true)
end

#write!Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby-swagger/io/file_system.rb', line 57

def write!
  Swagger::IO::FileSystem.init_fs_structure

  swagger = @doc.to_swagger

  Swagger::IO::Paths.write_paths(swagger.delete('paths'))

  DOC_SUBPARTS.each { |doc_part| write_subpart(doc_part, swagger.delete(doc_part)) }
  Swagger::IO::Definitions.write_definitions(swagger.delete('definitions'))
  Swagger::IO::Security.write_security_definitions(swagger.delete('securityDefinitions'))
  Swagger::IO::FileSystem.write_file(swagger.to_yaml, 'base_doc.yml')
end