Class: Swagger::Shell::DocLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger/shell/doc_loader.rb

Instance Method Summary collapse

Instance Method Details

#load(url) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/swagger/shell/doc_loader.rb', line 4

def load(url)
  api_docs = JSON.parse(Faraday.new(url: url).get.body)
  swagger_version = api_docs["swaggerVersion"] || api_docs["swagger"]

  ApiStruct.new(api_docs["basePath"]).tap do |root_api|
    # TODO: refactor
    if swagger_version == "1.2"
      api_docs["apis"].each do |api|
        api_body = JSON.parse Faraday.new(url: url + api["path"].gsub("{format}", :json.to_s)).get.body

        api_body["apis"].each do |api2|
          # 冗長なパスを排除
          path = api2["path"].gsub(/^#{Swagger::Shell.config_api.ignore_top_url}/, "")
          path_keys = path.split("/").reject {|s| s == "" }.tap {|p| p.last.gsub!(".json", "") }

          info = api2["operations"].first
          api_info = OpenStruct.new(
            summary: info["summary"],
            notes: info["notes"],
            parameters: info["parameters"],
            response_messages: info["responseMessages"],
            nickname: info["nickname"],
          )
          root_api.add_api(path_keys, info["method"], api_info)
        end
      end
    elsif swagger_version == "2.0"
      api_docs["paths"].each do |path, methods|
        methods.each do |method, api_info|
          # TODO: ignore path
          path_keys = path.split("/").reject {|s| s == "" } # TODO: format // .tap {|p| p.last.gsub!(".json", "") }
          root_api.add_api(path_keys, method) # TODO: api_info
        end
      end
    end
  end
end