class Swagger::IO::FileSystem

Constants

DOC_SUBPARTS

Public Class Methods

all_files(pattern) click to toggle source
# File lib/ruby-swagger/io/file_system.rb, line 45
def self.all_files(pattern)
  Dir["#{@@default_path}/#{pattern}"]
end
default_path() click to toggle source
# File lib/ruby-swagger/io/file_system.rb, line 18
def self.default_path
  @@default_path
end
default_path=(new_path) click to toggle source
# File lib/ruby-swagger/io/file_system.rb, line 14
def self.default_path=(new_path)
  @@default_path = new_path
end
delete_file(file) click to toggle source
# File lib/ruby-swagger/io/file_system.rb, line 49
def self.delete_file(file)
  FileUtils.rm_f(file)
end
file_exists?(name) click to toggle source
# File lib/ruby-swagger/io/file_system.rb, line 41
def self.file_exists?(name)
  File.exist?(@@default_path + '/' + name)
end
init_fs_structure() click to toggle source
# 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
new(swagger_doc) click to toggle source
# File lib/ruby-swagger/io/file_system.rb, line 53
def initialize(swagger_doc)
  @doc = swagger_doc
end
read() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

compile!() click to toggle source
# 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!() click to toggle source
# 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

Private Instance Methods

write_subpart(subpart, content) click to toggle source
# File lib/ruby-swagger/io/file_system.rb, line 91
def write_subpart(subpart, content)
  return unless content
  write_file(content.to_yaml, "#{subpart}.yml")
end