source2swagger 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  ## Description
3
2
 
4
3
  Coming soon...
@@ -23,10 +22,21 @@ Coming soon...
23
22
 
24
23
  #### Example
25
24
 
26
- $ bin/souce2swagger -i ~/project/lib -e "rb" -c "##~"_
25
+ $ bin/source2swagger -i ~/project/lib -e "rb" -c "##~"_
27
26
 
28
27
  This will output the Swagger compatible JSON specs on the terminal.
29
28
 
29
+ For this example the file annotated with the swagger spec are ruby files (*-e "rb"*). The annotations
30
+ started with *##~* to distinguish them from normal ruby comments that start *#*.
31
+
32
+ The source code is on the directory *~/project/lib* , note that the path is handled recursively, it will analyze all ruby
33
+ file under the declared path.
34
+
35
+ You can also target a single file setting the option *-f*
36
+
37
+ $ bin/source2swagger -f ~/data/test/sample3.rb -c "##~"_
38
+
39
+
30
40
  Add *-o /tmp* and it will write the JSON file(s) to */tmp*
31
41
 
32
42
  #### Contributions
@@ -122,7 +132,7 @@ Check [test/data/sample3.rb](https://github.com/solso/source2swagger/blob/master
122
132
 
123
133
  (partial)
124
134
 
125
- For a more comprehensive specification of the fields needes to declare your API on the Swagger format you can always go to the [source](http://swagger.wordnik.com/spec)
135
+ For a more comprehensive specification of the fields needed to declare your API on the Swagger format you can always go to the [source](http://swagger.wordnik.com/spec)
126
136
 
127
137
  $ROOT
128
138
 
data/bin/source2swagger CHANGED
@@ -21,9 +21,10 @@ opt_input = nil
21
21
  opt_output = nil
22
22
  opt_comment = nil
23
23
  opt_extension = nil
24
+ opt_filename = nil
24
25
 
25
26
  parser = OptionParser.new do |parser|
26
- parser.on('-i','--input PATH', 'Directory of the input source code') do |value|
27
+ parser.on('-i','--input PATH', 'Directory of the annotated source code') do |value|
27
28
  opt_input = value
28
29
  end
29
30
 
@@ -31,6 +32,10 @@ parser = OptionParser.new do |parser|
31
32
  opt_extension = value
32
33
  end
33
34
 
35
+ parser.on('-f','--file FILENAME', 'Overwrites options: -i and -e. Takes a single annotated source code file') do |value|
36
+ opt_filename = value
37
+ end
38
+
34
39
  parser.on('-c','--comment ("##~"|"//~")','Comment tag used to write docs') do |value|
35
40
  opt_comment = value
36
41
  end
@@ -39,14 +44,23 @@ parser = OptionParser.new do |parser|
39
44
  opt_output = value
40
45
  end
41
46
 
42
- parser.parse!
47
+ begin
48
+ parser.parse!
49
+ rescue Exception => e
50
+ puts e.message
51
+ puts parser
52
+ abort
53
+ end
43
54
  end
44
55
 
45
- unless opt_extension and opt_input and opt_comment
56
+
57
+ puts opt_filename
58
+
59
+ unless ((opt_extension and opt_input and opt_comment) || (opt_filename and opt_comment))
46
60
  puts parser
47
61
  abort
48
62
  end
49
-
63
+
50
64
  def save(results, output_path)
51
65
  results.each do |k,v|
52
66
  puts " Saving API #{k} to #{output_path}/#{k}.json"
@@ -63,22 +77,38 @@ def print(results)
63
77
  end
64
78
  end
65
79
 
66
- def run(input, extension, comment, output)
80
+ def run(input, extension, comment, output, filename)
67
81
  reader = SwaggerReader.new
68
82
  $_swaggerhash = Hash.new
69
83
 
70
- code = reader.analyze_all_files(input,extension,comment)
71
- results = reader.process_code(code) unless code.nil? || code.empty?
72
-
73
- puts "Swagger API in #{ARGV[0]}/**/*.#{ARGV[1]}: #{results.size} API\n"
84
+ if filename.nil?
85
+ code = reader.analyze_all_files(input,extension,comment)
86
+ else
87
+ code = reader.analyze_file(filename, comment)
88
+ end
89
+
90
+ begin
91
+ results = reader.process_code(code) unless code.nil? || code.empty?
92
+ rescue SwaggerReaderException => e
93
+ puts "There is a problem with your swagger annotation. See below...\n\n"
94
+ puts e.inspect
95
+ end
96
+
97
+ if filename.nil?
98
+ puts "Swagger API in #{input}/**/*.#{extension}: #{results.size} API\n"
99
+ else
100
+ puts "Swagger API in #{filename}: #{results.size} API\n"
101
+ end
102
+
74
103
  if output.nil?
75
104
  print(results)
76
105
  else
77
106
  save(results,output)
78
107
  end
108
+
79
109
  puts "Done!"
80
110
 
81
111
  end
82
112
 
83
113
 
84
- run(opt_input, opt_extension, opt_comment, opt_output)
114
+ run(opt_input, opt_extension, opt_comment, opt_output, opt_filename)
@@ -47,7 +47,7 @@ class SwaggerReader
47
47
  cont = 0
48
48
  code[:code].each do |code_line|
49
49
  code_line.strip!
50
- if code_line[0]=='@'
50
+ if code_line[0]=="@"[0]
51
51
  tmp_vars[:code] << code_line.gsub('@',' ')
52
52
  tmp_vars[:line_number] << code[:line_number][cont]
53
53
  tmp_vars[:file] << code[:file][cont]
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
16
  gem.require_paths = ["lib"]
17
- gem.version = '0.0.1'
17
+ gem.version = '0.0.2'
18
18
 
19
19
  gem.add_dependency 'json'
20
20
  end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,8 @@
1
+ require 'rubygems'
1
2
  require 'test/unit'
3
+ require 'json'
2
4
  require 'swagger_hash'
3
5
  require 'swagger_reader'
4
- require 'json'
5
6
 
6
7
  $:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
7
8
 
@@ -117,7 +117,7 @@ class SwaggerReaderTest < Test::Unit::TestCase
117
117
  api1 = reader.process_code(code)
118
118
  assert_equal true, false
119
119
  rescue Exception => e
120
- assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad2.rb:9\n#<SyntaxError: (eval):1: unterminated string meets end of file>", e.to_s
120
+ assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad2.rb:9", e.message.split("\n").first
121
121
  end
122
122
 
123
123
  end
@@ -131,7 +131,7 @@ class SwaggerReaderTest < Test::Unit::TestCase
131
131
  api1 = reader.process_code(code)
132
132
  assert_equal true, false
133
133
  rescue Exception => e
134
- assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad1.rb:17\n#<SyntaxError: (eval):1: syntax error, unexpected keyword_end, expecting $end\n...=> \"API down\", :code => 500;end = a.operations.add;out = {:a...\n... ^>", e.to_s
134
+ assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad1.rb:17", e.message.split("\n").first
135
135
  end
136
136
 
137
137
  end
@@ -145,7 +145,7 @@ class SwaggerReaderTest < Test::Unit::TestCase
145
145
  api1 = reader.process_code(code)
146
146
  assert_equal true, false
147
147
  rescue Exception => e
148
- assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad3.rb:17\n#<NameError: undefined local variable or method `error_sanitize_NOT_YET_DEFINED' ", e.to_s.split("for")[0].to_s
148
+ assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad3.rb:17", e.message.split("\n").first
149
149
 
150
150
  end
151
151
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: source2swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000Z
12
+ date: 2012-06-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &2162299820 !ruby/object:Gem::Requirement
16
+ requirement: &2154849880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2162299820
24
+ version_requirements: *2154849880
25
25
  description: Tool for converting comments to Swagger JSON specification
26
26
  email: josep@3scale.net
27
27
  executables: