Class: Swagger::IO::Paths

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

Class Method Summary collapse

Class Method Details

.read_pathsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby-swagger/io/paths.rb', line 6

def self.read_paths
  paths = {}

  l = (Swagger::IO::FileSystem.default_path + '/paths').length

  Swagger::IO::FileSystem.all_files('paths/**/*.yml').each do |file|
    content = YAML::load_file(file)
    paths[File.dirname(file[l..file.length])] ||= {}
    paths[File.dirname(file[l..file.length])][File.basename(file, ".yml")] = content
  end

  paths
end

.write_paths(paths) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby-swagger/io/paths.rb', line 20

def self.write_paths(paths)
  return if paths.nil?

  l = (Swagger::IO::FileSystem.default_path + '/paths').length

  #Remove dead endpoints
  Swagger::IO::FileSystem.all_files("paths/**/*.yml").each do |file|
    def_name = file[l..file.length]

    if paths[File.dirname(file[l..file.length])].nil? || paths[File.dirname(file[l..file.length])][File.basename(file, ".yml")].nil?
      STDERR.puts "#{def_name} is not present anymore, removing #{file}"
      Swagger::IO::FileSystem.delete_file(file)
    end

  end

  paths.each do |path, path_obj|
    path_obj.each do |action, action_obj|

      file = "paths/#{path}/#{action}.yml"

      # If an old definition exists, we copy over the documentation to the generated definition
      if Swagger::IO::FileSystem.file_exists?(file)
        old_action = Swagger::IO::FileSystem.read_file(file)

        Swagger::IO::Comparable.copy_description_old_definition(action_obj, old_action)
      end


      Swagger::IO::FileSystem.write_file(action_obj.to_yaml, file, true)
    end
  end
end