swag 0.2.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/swag +12 -4
- data/lib/swag.rb +79 -75
- data/lib/swag_helper.rb +52 -104
- data/lib/vars.rb +73 -0
- metadata +3 -3
- data/lib/global_vars.rb +0 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 34ae21308f9a2bf56ca1e79e960cfe790cc55132
|
|
4
|
+
data.tar.gz: 0ad17a73b82f097ed91fb1ad0876a02d974b6b0e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 72a13f9c7193e5097d6f420856b70d0e53695d3c93af4d6ec267384f6cc81332f07fbe19d08d78ad8a6d504050a1c6dff246042ad3b30accf59bb793ef98d78b
|
|
7
|
+
data.tar.gz: 0100875e0e178bef2675fd5a944848435632b75525b554fc536a0a1a761963932eaffa5415516177fc59c2a8e9127925266cf94bc6f89496e38d10b49d9b78ae
|
data/bin/swag
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
#! /usr/bin/env ruby
|
|
2
2
|
|
|
3
3
|
require 'swag'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
Swag.hi
|
|
5
|
+
if ARGV[0] == nil
|
|
6
|
+
Swag.usage
|
|
7
|
+
elsif ARGV[1]
|
|
8
|
+
Swag.usage
|
|
9
|
+
elsif ARGV[0] == "config"
|
|
10
|
+
Swag.config
|
|
11
|
+
elsif ARGV[0] == "merge"
|
|
12
|
+
Swag.merge
|
|
13
|
+
else
|
|
14
|
+
Swag.path(ARGV[0])
|
|
15
|
+
end
|
data/lib/swag.rb
CHANGED
|
@@ -1,92 +1,96 @@
|
|
|
1
1
|
require_relative 'swag_helper.rb'
|
|
2
|
-
require_relative '
|
|
2
|
+
require_relative 'vars.rb'
|
|
3
|
+
|
|
3
4
|
|
|
4
5
|
class Swag
|
|
5
6
|
|
|
6
7
|
@helper = SwagHelper.new
|
|
7
8
|
|
|
8
|
-
def initialize
|
|
9
|
-
end
|
|
10
|
-
|
|
11
9
|
def self.hi
|
|
12
|
-
|
|
10
|
+
puts "Swag, world."
|
|
13
11
|
end
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
puts "
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
def self.usage
|
|
14
|
+
puts "Usage: swag <command>"
|
|
15
|
+
puts "Command options: config, <path>, merge"
|
|
16
|
+
puts "config"
|
|
17
|
+
puts " - sets up the 'swag' folder in your current directory."
|
|
18
|
+
puts " - creates 'swag/config.yml' for your API's meta information."
|
|
19
|
+
puts " - swag/config.yml MUST exist in order for swag to run properly."
|
|
20
|
+
puts "<path>"
|
|
21
|
+
puts " - where <path> is a path in your API to document"
|
|
22
|
+
puts " - for example , 'swag users' will document the /users path"
|
|
23
|
+
puts " - documentation is generated as swag/<path>-api.yml"
|
|
24
|
+
# might get rid of the below part
|
|
25
|
+
# puts "merge"
|
|
26
|
+
# puts " - generates swag/api.yml, a full documentation of your API."
|
|
27
|
+
# puts " - reads from all swag/<path>-api.yml files to accomplish this."
|
|
28
|
+
# puts " - important: you must document each path individually before merging."
|
|
24
29
|
end
|
|
25
30
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
puts "Error while reading controllers."
|
|
35
|
-
puts ex.inspect
|
|
36
|
-
puts ex.backtrace
|
|
37
|
-
end # end begin
|
|
38
|
-
end # end checkControllers
|
|
39
|
-
|
|
40
|
-
# writes specific controller's routes (helper for self.writePaths)
|
|
41
|
-
# 'controllerName' is the controller's name, 'doc' is the open File
|
|
42
|
-
def self.analyzeController(controllerName, doc)
|
|
43
|
-
puts "Analyzing controller: #{controllerName}"
|
|
31
|
+
def self.config
|
|
32
|
+
puts "Starting configuration..."
|
|
33
|
+
if File.exist?("swag/config.yml")
|
|
34
|
+
puts "Config file already exists."
|
|
35
|
+
else
|
|
36
|
+
begin
|
|
37
|
+
# make swag directory
|
|
38
|
+
Dir.mkdir("swag") unless Dir.exists?("swag")
|
|
44
39
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
40
|
+
# make config file
|
|
41
|
+
config = File.open("swag/config.yml", 'w')
|
|
42
|
+
config << $DEFAULT_CONFIG.to_yaml
|
|
43
|
+
config.close
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
end
|
|
62
|
-
end # end each_line do block
|
|
63
|
-
@helper.doShow(nameSliced, controllerName, doc) if @show
|
|
64
|
-
@helper.doDelete(nameSliced, controllerName, doc) if @delete
|
|
65
|
-
end # end File.open do block (File is closed automatically)
|
|
66
|
-
end # end analyzeController
|
|
45
|
+
puts "Created swag/config.yml"
|
|
46
|
+
puts "Please edit swag/config.yml to include your API's meta info."
|
|
47
|
+
puts "This is important in order for swag to work properly!"
|
|
48
|
+
puts "Run swag again when ready. Aborting."
|
|
49
|
+
abort
|
|
50
|
+
rescue Errno::ENOENT => e
|
|
51
|
+
puts "Error while making swag/config.yml"
|
|
52
|
+
puts e
|
|
53
|
+
puts e.backtrace
|
|
54
|
+
end # end try/catch
|
|
55
|
+
end # end if/else
|
|
56
|
+
end # end method
|
|
67
57
|
|
|
68
|
-
|
|
69
|
-
def self.writePaths
|
|
70
|
-
puts "Writing paths."
|
|
58
|
+
def self.merge
|
|
71
59
|
begin
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
doc
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
60
|
+
doc = File.open("swag/api.yml", 'w')
|
|
61
|
+
@helper.readConfig(doc)
|
|
62
|
+
doc.close
|
|
63
|
+
rescue Errno::ENOENT => e
|
|
64
|
+
puts "Error opening file."
|
|
65
|
+
puts e
|
|
66
|
+
rescue IOError => e
|
|
67
|
+
puts "Error writing to file."
|
|
68
|
+
puts e
|
|
69
|
+
end
|
|
70
|
+
end
|
|
78
71
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
72
|
+
def self.path(arg)
|
|
73
|
+
@helper.setupApi unless File.exist?("swag/api.yml")
|
|
74
|
+
begin
|
|
75
|
+
api = YAML.load_file('./swag/api.yml')
|
|
76
|
+
puts api
|
|
77
|
+
puts "Path to explore: #{api["host"]}#{api["basepath"]}#{arg}"
|
|
78
|
+
rescue Errno::ENOENT => e
|
|
79
|
+
puts "Error loading swag/api.yml. Make sure it is configured properly."
|
|
80
|
+
puts e
|
|
81
|
+
puts e.backtrace
|
|
82
|
+
end
|
|
83
|
+
puts "Is this correct? [y/n]:"
|
|
84
|
+
answer = STDIN.gets.chomp
|
|
85
|
+
if answer.downcase == "y"
|
|
86
|
+
@helper.doPath(arg, api)
|
|
87
|
+
elsif answer == ""
|
|
88
|
+
puts "yes"
|
|
89
|
+
@helper.doPath(arg, api)
|
|
90
|
+
elsif answer.downcase == "n"
|
|
91
|
+
puts "Please try again."
|
|
92
|
+
else
|
|
93
|
+
puts "Please try again."
|
|
94
|
+
end
|
|
95
|
+
end # end method
|
|
92
96
|
end # end Class
|
data/lib/swag_helper.rb
CHANGED
|
@@ -1,120 +1,68 @@
|
|
|
1
|
-
require_relative '
|
|
1
|
+
require_relative 'swag.rb'
|
|
2
|
+
require_relative 'vars.rb'
|
|
3
|
+
require 'yaml'
|
|
4
|
+
require 'net/http'
|
|
2
5
|
|
|
3
6
|
class SwagHelper
|
|
4
7
|
|
|
5
8
|
def initialize
|
|
6
9
|
end
|
|
7
10
|
|
|
8
|
-
def
|
|
9
|
-
File.open("
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
def setupApi
|
|
12
|
+
doc = File.open("swag/api.yml", 'w')
|
|
13
|
+
# read config.yml in as a Ruby hash. Populate doc with appropriate info.
|
|
14
|
+
config = YAML.load_file('./swag/config.yml')
|
|
15
|
+
puts "Loaded config info from swag/config.yml"
|
|
16
|
+
doc << config.to_yaml
|
|
17
|
+
puts "Wrote config info to swag/api.yml"
|
|
18
|
+
doc.close
|
|
14
19
|
end
|
|
15
20
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
config << " title: #{File.basename(Dir.getwd)}\n"
|
|
22
|
-
config << " description: a Rails app.\n"
|
|
23
|
-
config << " author:\n"
|
|
24
|
-
config << " name:\n"
|
|
25
|
-
config << " contact:\n"
|
|
26
|
-
config << " license:\n"
|
|
27
|
-
config << "schemes:\n"
|
|
28
|
-
config << " - http\n"
|
|
29
|
-
config << "consumes:\n"
|
|
30
|
-
config << "produces:\n"
|
|
31
|
-
config << "paths:\n"
|
|
32
|
-
config.close
|
|
33
|
-
end
|
|
21
|
+
# TODO: leaving this as a reminder to implement the /path/:id path thing yeah
|
|
22
|
+
# def doShow(nameSliced, controllerName, doc)
|
|
23
|
+
# # puts "#{controllerName} contains show"
|
|
24
|
+
# doc << " /#{nameSliced}/:id\n"
|
|
25
|
+
# end
|
|
34
26
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
# read line-by-line
|
|
39
|
-
# write relevant information to doc
|
|
40
|
-
if File.exists?("swagGem/config.yml")
|
|
41
|
-
readConfig(doc)
|
|
42
|
-
else
|
|
43
|
-
writeConfig
|
|
44
|
-
readConfig(doc)
|
|
45
|
-
end
|
|
27
|
+
def doGet(fullPath)
|
|
28
|
+
puts "Sending an http get request to #{fullPath}. Returning a hash."
|
|
29
|
+
return $DEFAULT_GET
|
|
46
30
|
end
|
|
47
31
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
doc << " default:\n"
|
|
60
|
-
doc << " description: unexpected error\n"
|
|
61
|
-
doc << " schema:\n"
|
|
62
|
-
end
|
|
32
|
+
def doPath(arg, api)
|
|
33
|
+
input = {
|
|
34
|
+
"#{arg}" => {
|
|
35
|
+
"get" => $DEFAULT_GET,
|
|
36
|
+
"post" => $DEFAULT_POST,
|
|
37
|
+
"patch" => {},
|
|
38
|
+
"delete" => $DEFAULT_DELETE,
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
# the path to send to HTTP methods
|
|
42
|
+
fullPath = "#{api["host"]}#{api["basepath"]}#{arg}"
|
|
63
43
|
|
|
64
|
-
|
|
65
|
-
#
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
doc << " responses:\n"
|
|
71
|
-
doc << " 200:\n"
|
|
72
|
-
doc << " description: #{nameSliced} response\n"
|
|
73
|
-
doc << " schema:\n"
|
|
74
|
-
doc << " default:\n"
|
|
75
|
-
doc << " description: unexpected error\n"
|
|
76
|
-
doc << " schema:\n"
|
|
77
|
-
end
|
|
44
|
+
# assign the get, post, patch, delete sections for this path
|
|
45
|
+
# call the HTTP methods from above
|
|
46
|
+
input["#{arg}"]["get"] = doGet(fullPath)
|
|
47
|
+
# input["#{arg}"]["post"] = doPost(fullPath)
|
|
48
|
+
# input["#{arg}"]["patch"] = doPatch(fullPath)
|
|
49
|
+
# input["#{arg}"]["delete"] = doDelete(fullPath)
|
|
78
50
|
|
|
79
|
-
|
|
80
|
-
#
|
|
81
|
-
doc << " /#{nameSliced}/:id\n"
|
|
82
|
-
doc << " get:\n"
|
|
83
|
-
doc << " description: returns a specific #{nameSliced} item by id.\n"
|
|
84
|
-
doc << " produces:\n"
|
|
85
|
-
doc << " parameters:\n"
|
|
86
|
-
doc << " - name: id\n"
|
|
87
|
-
doc << " in: path\n"
|
|
88
|
-
doc << " description: id of specific #{nameSliced} object\n"
|
|
89
|
-
doc << " required: true\n"
|
|
90
|
-
doc << " type: integer\n"
|
|
91
|
-
doc << " format:\n"
|
|
92
|
-
doc << " responses:\n"
|
|
93
|
-
doc << " 200:\n"
|
|
94
|
-
doc << " description: #{nameSliced} response\n"
|
|
95
|
-
doc << " schema:\n"
|
|
96
|
-
doc << " default:\n"
|
|
97
|
-
doc << " description: unexpected error\n"
|
|
98
|
-
doc << " schema:\n"
|
|
99
|
-
end
|
|
51
|
+
# now that the path info is assigned, combine it with the whole API doc
|
|
52
|
+
api["paths"]["#{arg}"] = input["#{arg}"]
|
|
100
53
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
doc << " description: #{nameSliced} object deleted\n"
|
|
115
|
-
doc << " schema:\n"
|
|
116
|
-
doc << " default:\n"
|
|
117
|
-
doc << " description: unexpected error\n"
|
|
118
|
-
doc << " schema:\n"
|
|
54
|
+
# write the final product to swag/api.yml
|
|
55
|
+
begin
|
|
56
|
+
doc = File.open("swag/api.yml", 'w')
|
|
57
|
+
doc << api.to_yaml
|
|
58
|
+
puts "Wrote api info to swag/api.yml"
|
|
59
|
+
doc.close
|
|
60
|
+
rescue Errno::ENOENT => e
|
|
61
|
+
puts "Error creating file: swag/api.yml"
|
|
62
|
+
puts e
|
|
63
|
+
rescue IOError => e
|
|
64
|
+
puts "Error writing to file: swag/api.yml"
|
|
65
|
+
puts e
|
|
66
|
+
end
|
|
119
67
|
end
|
|
120
68
|
end # end class
|
data/lib/vars.rb
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
SWAG_VERSION = '0.5.0'
|
|
2
|
+
|
|
3
|
+
$DEFAULT_CONFIG = {
|
|
4
|
+
"swag" => "#{SWAG_VERSION}",
|
|
5
|
+
"info" => {
|
|
6
|
+
"version" => '0.0',
|
|
7
|
+
"title" => "My API.",
|
|
8
|
+
"description" => "An API.",
|
|
9
|
+
"author" => {
|
|
10
|
+
"name" => "Example",
|
|
11
|
+
"contact" => "example@example.com",
|
|
12
|
+
},
|
|
13
|
+
"license" => "BSD 3-Clause",
|
|
14
|
+
},
|
|
15
|
+
"host" => "localhost:3000",
|
|
16
|
+
"basepath" => "/",
|
|
17
|
+
"schemes" => ["http"],
|
|
18
|
+
"paths" => {}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
$DEFAULT_GET = {
|
|
22
|
+
"description" => "",
|
|
23
|
+
"produces" => "",
|
|
24
|
+
"parameters" => "",
|
|
25
|
+
"responses" => {
|
|
26
|
+
"200:" => {
|
|
27
|
+
"description" => "",
|
|
28
|
+
"schema" => "",
|
|
29
|
+
},
|
|
30
|
+
"default" => {
|
|
31
|
+
"description" => "unexpected error",
|
|
32
|
+
"schema" => [],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
$DEFAULT_POST = {
|
|
38
|
+
"description" => "",
|
|
39
|
+
"produces" => "",
|
|
40
|
+
"parameters" => [],
|
|
41
|
+
"responses" => {
|
|
42
|
+
"200:" => {
|
|
43
|
+
"description" => "",
|
|
44
|
+
"schema" => [],
|
|
45
|
+
},
|
|
46
|
+
"default" => {
|
|
47
|
+
"description" => "unexpected error",
|
|
48
|
+
"schema" => [],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
$DEFAULT_DELETE = {
|
|
54
|
+
"description" => "",
|
|
55
|
+
"produces" => "",
|
|
56
|
+
"parameters" => [{
|
|
57
|
+
"name" => "id",
|
|
58
|
+
"in" => "path",
|
|
59
|
+
"description" => "id of entry to be deleted.",
|
|
60
|
+
"required" => "true",
|
|
61
|
+
"type" => "integer",
|
|
62
|
+
}],
|
|
63
|
+
"responses" => {
|
|
64
|
+
"200:" => {
|
|
65
|
+
"description" => "",
|
|
66
|
+
"schema" => [],
|
|
67
|
+
},
|
|
68
|
+
"default" => {
|
|
69
|
+
"description" => "unexpected error",
|
|
70
|
+
"schema" => "",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
}
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: swag
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ethan Miller
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-08-
|
|
11
|
+
date: 2015-08-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: |-
|
|
14
14
|
A gem with which to generate YAML from a Rails app. Currently
|
|
@@ -20,9 +20,9 @@ extensions: []
|
|
|
20
20
|
extra_rdoc_files: []
|
|
21
21
|
files:
|
|
22
22
|
- bin/swag
|
|
23
|
-
- lib/global_vars.rb
|
|
24
23
|
- lib/swag.rb
|
|
25
24
|
- lib/swag_helper.rb
|
|
25
|
+
- lib/vars.rb
|
|
26
26
|
homepage: http://rubygems.org/gems/swag
|
|
27
27
|
licenses:
|
|
28
28
|
- BSD-3-Clause
|
data/lib/global_vars.rb
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
SWAG_VERSION = '0.2.0'
|