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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32bc8a758f8ddbfc48c7d10b11fc39f760744b8b
4
- data.tar.gz: e177da8c57c5c06157a6a353ae2f64ec7c29cc3b
3
+ metadata.gz: 34ae21308f9a2bf56ca1e79e960cfe790cc55132
4
+ data.tar.gz: 0ad17a73b82f097ed91fb1ad0876a02d974b6b0e
5
5
  SHA512:
6
- metadata.gz: e5cbc5a2d8993061faf7364d6170a1e7806669ab67f88e9f119a5ca2d74e61d60b0d3237b059c2cd18419ba212b23a7fd01ec91e7f4e8efdb638888eb7502043
7
- data.tar.gz: ac552d828e53d02f1d923f2483ab4ae9feecfc01ba72d7714411bc342f09ed371eb6b8462ef14208efcc4293bed3998995e7ce27c3fa40710a21bd5b1dd35700
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
- puts Swag.hi
5
- puts Swag.check
6
- puts Swag.checkControllers
7
- puts Swag.writePaths
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
@@ -1,92 +1,96 @@
1
1
  require_relative 'swag_helper.rb'
2
- require_relative 'global_vars.rb'
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
- puts "Swag, world."
10
+ puts "Swag, world."
13
11
  end
14
12
 
15
- # verifies rails directory
16
- def self.check
17
- puts "Checking your directory: #{Dir.pwd}"
18
- if File.exist?("bin/rails")
19
- puts "Rails App detected. Proceeding."
20
- else
21
- puts "You must be in a Rails root directory for Swag to work. Aborting."
22
- abort
23
- end
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
- # checks that app/controllers can be read
27
- def self.checkControllers
28
- puts "Checking controllers."
29
- begin
30
- Dir.foreach("app/controllers") do |c|
31
- puts "Found #{c}"
32
- end
33
- rescue => ex
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
- # edits the controller name for use in the File
46
- nameSliced = controllerName.slice(0..(controllerName.index('_') -1))
47
- doc << " /#{nameSliced}:\n"
40
+ # make config file
41
+ config = File.open("swag/config.yml", 'w')
42
+ config << $DEFAULT_CONFIG.to_yaml
43
+ config.close
48
44
 
49
- File.open("app/controllers/#{controllerName}", 'r') do |c|
50
- @show = false
51
- @delete = false
52
- c.each_line do |line|
53
- if line.include? "def index"
54
- @helper.doIndex(nameSliced, controllerName, doc)
55
- elsif line.include? "def create"
56
- @helper.doCreate(nameSliced, controllerName, doc)
57
- elsif line.include? "def show"
58
- @show = true
59
- elsif line.include? "def delete"
60
- @delete = true
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
- # lists controller paths by reading app/controllers directory
69
- def self.writePaths
70
- puts "Writing paths."
58
+ def self.merge
71
59
  begin
72
- # creates directory for swag to use
73
- Dir.mkdir("swagGem") unless File.exists?("swagGem")
74
- doc = File.open("swagGem/api.yml", 'w')
75
-
76
- # sets up doc w/ config info
77
- @helper.checkConfig(doc)
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
- Dir.foreach("app/controllers") do |c|
80
- unless (c == "." || c == ".." || c == "concerns" ||
81
- c == "application_controller.rb")
82
- analyzeController(c, doc)
83
- end
84
- end
85
- doc.close
86
- rescue => ex
87
- puts "Error while writing paths."
88
- puts ex.inspect
89
- puts ex.backtrace
90
- end # end begin block
91
- end # end writePaths
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
@@ -1,120 +1,68 @@
1
- require_relative 'global_vars.rb'
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 readConfig(doc)
9
- File.open("swagGem/config.yml", 'r') do |c|
10
- c.each_line do |line|
11
- doc << "#{line}"
12
- end # end each line block
13
- end # close File block
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
- def writeConfig
17
- config = File.open("swagGem/config.yml", 'w')
18
- config << "swag: #{SWAG_VERSION}\n"
19
- config << "info:\n"
20
- config << " version:\n"
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
- # checks config info
36
- def checkConfig(doc)
37
- # open config file (if it exists)
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
- # prints index info to open YAML File 'doc'
49
- def doIndex(nameSliced, controllerName, doc)
50
- # puts "#{controllerName} contains index"
51
- doc << " get:\n"
52
- doc << " description: returns an index for this resource.\n"
53
- doc << " produces:\n"
54
- doc << " parameters:\n"
55
- doc << " responses:\n"
56
- doc << " 200:\n"
57
- doc << " description: #{nameSliced} response\n"
58
- doc << " schema:\n"
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
- def doCreate(nameSliced, controllerName, doc)
65
- # puts "#{controllerName} contains new"
66
- doc << " post:\n"
67
- doc << " description: creates a new instance of this resource.\n"
68
- doc << " produces:\n"
69
- doc << " parameters:\n"
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
- def doShow(nameSliced, controllerName, doc)
80
- # puts "#{controllerName} contains show"
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
- def doDelete(nameSliced, controllerName, doc)
102
- doc << " delete:\n"
103
- doc << " description: deletes a specific #{nameSliced} item by id.\n"
104
- doc << " produces:\n"
105
- doc << " parameters:\n"
106
- doc << " - name: id\n"
107
- doc << " in: path\n"
108
- doc << " description: id of specific #{nameSliced} object\n"
109
- doc << " required: true\n"
110
- doc << " type: integer\n"
111
- doc << " format:\n"
112
- doc << " responses:\n"
113
- doc << " 204:\n"
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
@@ -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.2.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-05 00:00:00.000000000 Z
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
@@ -1 +0,0 @@
1
- SWAG_VERSION = '0.2.0'