grape-doc 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f00e6cadd228f64cd7fb2099ade14e6492f3089
4
- data.tar.gz: 9996410f37a28e87a478bdafb9cb0ff75b65debb
3
+ metadata.gz: 6c9c454799bd7aea1036f9f1a05aa6a4105d9676
4
+ data.tar.gz: 39921c04e363881c8f85c037a322c06dfd10ce10
5
5
  SHA512:
6
- metadata.gz: 488d1cf71281297eb741814477060490592ca15d58ed1b9590eae3516ca6a841f2ab4b2179a5325dea6ff25677e54ff22d1e22f43bd32223138ae2db912bb0d9
7
- data.tar.gz: 21333f8855c6ffb17cae84e5d86903a7fc31f148ee4121136cee56ecd76e087015d3204d831861ba7b5e10a6a9785abbdc96624951febf58ed8731c1191f88ed
6
+ metadata.gz: f8c0d7e1916d2dcaeaa8940d61fbf71c449da3ba5fe57306d7b4fae1e267c64ed10e826b8331853e03e72a82c1b6f6ccd4eee7e8c69985c27e036de32b6fa870
7
+ data.tar.gz: e57b8ce60926e4f2eff95e8c4818332042c9a580e044b385e5cad0b6ceaa065da35618164da8f3a00c4c1c1f792bd3cbab018fa4eac8193d138ca4f88e9c5e69
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.3.0
data/grape-doc.gemspec CHANGED
@@ -23,6 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency "loader"
24
24
  spec.add_dependency "RedCloth"
25
25
  spec.add_dependency "rack-test"
26
- spec.add_dependency "rack-test-poc",">= 1.0.2"
26
+ spec.add_dependency "rack-test-poc",">= 1.1.0"
27
27
 
28
28
  end
@@ -48,6 +48,24 @@ module GrapeDoc
48
48
 
49
49
  end
50
50
 
51
+ def typer(obj)
52
+ case obj
53
+
54
+ when Array
55
+ obj.map(&:typer)
56
+
57
+ when Hash
58
+ obj.reduce({}){|m,o| m.merge!(o[0] => typer(o[1]) ) ;m}
59
+
60
+ when Class,Module
61
+ obj
62
+
63
+ else
64
+ obj.class
65
+
66
+ end
67
+
68
+ end
51
69
 
52
70
  end
53
71
  end
@@ -1,17 +1,15 @@
1
+ require 'json'
1
2
  module GrapeDoc
2
3
  class Generator
3
4
 
4
- attr_accessor :file_path
5
+ attr_reader :options
5
6
  def initialize(opts={})
6
7
 
7
8
  raise(ArgumentError,'invalid options given') unless opts.class <= Hash
8
- opts.merge!(
9
- {
10
- format: 'html'
11
- }.merge(opts)
12
- )
13
-
14
- self.file_path= opts[:path] || opts['path'] || File.join(Helpers.doc_folder_path,'api_doc.html')
9
+ @options = {
10
+ 'format' => 'html',
11
+ 'path' => File.join(Helpers.doc_folder_path,'api_doc.html')
12
+ }.merge(opts.reduce({}){|m,o| m.merge!(o[0].to_s => o[1]) ;m})
15
13
 
16
14
  process_head
17
15
  process_table_of_content
@@ -25,7 +23,7 @@ module GrapeDoc
25
23
 
26
24
  def save
27
25
 
28
- File.write self.file_path,
26
+ File.write self.options['path'],
29
27
  document.to_textile.to_html
30
28
 
31
29
  true;rescue;false
@@ -47,6 +45,8 @@ module GrapeDoc
47
45
  Helpers.each_grape_class do |rest_api_model|
48
46
  rest_api_model.routes.each do |route|
49
47
 
48
+ next if route.route_path =~ /\(\.:format\)\(\.:format\)$/
49
+
50
50
  document.add :h2, "#{route.route_method.to_s.upcase}: #{route.route_path}"
51
51
  document.add :h3, 'Request'
52
52
  document.add :h4, 'description'
@@ -81,33 +81,51 @@ module GrapeDoc
81
81
  route_path_var = route.route_path.to_s.sub(/\(\.:format\)$/,'')
82
82
  @poc_data ||= Helpers.poc_data
83
83
  if @poc_data && @poc_data.find{|k,v| k =~ /^#{route_path_var}/ }
84
- if -> { @poc_data[route_path_var][route.route_method.to_s.upcase] rescue nil }.call
85
- poc_opts = @poc_data[route_path_var][route.route_method.to_s.upcase]
84
+ route_method_var = route.route_method.to_s.upcase
86
85
 
87
- document.add :h3,'Response'
88
- # document.add :raw,poc_opts['response'].to_yaml
89
- document.add :h4,'body'
90
- document.add :raw,poc_opts['response']['raw_body']
86
+ poc_cases = @poc_data.select{|k,v|
87
+ k =~ /^#{route_path_var}\.?/ && v.keys.include?(route_method_var)
88
+ }
91
89
 
92
- document.add :h5,'options:'
93
-
94
- document.add :list,[
95
- "'status code: #{poc_opts['response']['status']}",
96
- "format: #{poc_opts['response']['format']}"
97
- ]
90
+ document.add :h3,'Response'
98
91
 
92
+ poc_cases.each do |poc_path,poc_data|
93
+ poc_opts = poc_data[route_method_var] || next
99
94
 
100
95
  document.add :h4,'example'
101
96
 
102
97
  document.add :h5,'curl sample'
103
-
104
98
  document.add :raw,[
105
- "curl ",
106
- "-X #{route.route_method.to_s.upcase} ",
107
- "\"http://api_url#{route_path_var}?",
108
- "#{poc_opts['request']['query']['raw']}\""
99
+ "$ curl ",
100
+ "-X #{route_method_var} ",
101
+ "\"http://#{ -> { poc_opts['response']['headers']['HTTP_HOST'] rescue 'example.org' }.call}",
102
+ "#{poc_path}?","#{poc_opts['request']['query']['raw']}\" ",
103
+ poc_opts['request']['headers'].map{|k,v| "-H \"#{k}: #{v}\"" }.join(' ')
109
104
  ].join
110
105
 
106
+ document.add :list,[
107
+ "status code: #{poc_opts['response']['status']}",
108
+ "format type: #{poc_opts['response']['format']}"
109
+ ]
110
+
111
+ document.add :h6,'raw body'
112
+ document.add :raw,poc_opts['response']['raw_body']
113
+
114
+ if JSON.respond_to?(:pretty_generate)
115
+
116
+ document.add :h6,'json formatted body with Class types'
117
+ document.add :raw,
118
+ JSON.pretty_generate(
119
+ ApiDocParts::Parser.typer(poc_opts['response']['body'])
120
+ )
121
+
122
+ end
123
+
124
+
125
+ end
126
+
127
+ if -> { @poc_data[route_path_var][route.route_method.to_s.upcase] rescue nil }.call
128
+ poc_opts = @poc_data[route_path_var][route.route_method.to_s.upcase]
111
129
 
112
130
 
113
131
  end
data/test/sample.html CHANGED
@@ -13,4 +13,31 @@
13
13
  <li>type: String</li>
14
14
  <li>desc: it&#8217;s a test string</li>
15
15
  </ul></li>
16
- </ul>
16
+ </ul>
17
+ <h3>Response</h3>
18
+ <h4>example</h4>
19
+ <h5>curl sample</h5>
20
+ <pre>$ curl -X GET "http://example.org/hello?test=hy" -H "Accept-Version: v1" -H "X-Token: blabla"</pre>
21
+ <ul>
22
+ <li>status code: 200</li>
23
+ <li>format type: json</li>
24
+ </ul>
25
+ <h6>raw body</h6>
26
+ <pre>{"hello":"world!"}</pre>
27
+ <h6>json formatted body with Class types</h6>
28
+ <pre>{
29
+ "hello": "String"
30
+ }</pre>
31
+ <h4>example</h4>
32
+ <h5>curl sample</h5>
33
+ <pre>$ curl -X GET "http://example.org/hello.json?" -H "Accept-Version: v1" -H "X-Token: blabla"</pre>
34
+ <ul>
35
+ <li>status code: 200</li>
36
+ <li>format type: json</li>
37
+ </ul>
38
+ <h6>raw body</h6>
39
+ <pre>{"hello":"world!"}</pre>
40
+ <h6>json formatted body with Class types</h6>
41
+ <pre>{
42
+ "hello": "String"
43
+ }</pre>
data/test/test_all.rb ADDED
@@ -0,0 +1,4 @@
1
+ require_relative 'test_helper'
2
+ require_relative 'test_sample_api'
3
+ require_relative 'test_collector'
4
+ require_relative 'test_generator'
@@ -0,0 +1,11 @@
1
+ require_relative 'test_helper'
2
+ describe 'Doc generating' do
3
+
4
+ specify 'test documentation generator' do
5
+
6
+ var = GrapeDoc.new
7
+ GrapeDoc.generate path: File.join(__dir__,'sample.html')
8
+
9
+ end
10
+
11
+ end
@@ -9,6 +9,8 @@ describe 'Dummy test for rack-test-poc' do
9
9
 
10
10
  specify 'test the get call' do
11
11
 
12
+ header("Accept-Version","v1")
13
+ header("X-Token","blabla")
12
14
  get '/hello',test: 'hy'
13
15
  last_response.status.must_be :==, 200
14
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-03 00:00:00.000000000 Z
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: 1.0.2
103
+ version: 1.1.0
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: 1.0.2
110
+ version: 1.1.0
111
111
  description: " Documentation generator for Grape module compatible with Redmine and
112
112
  Github formats "
113
113
  email:
@@ -136,9 +136,10 @@ files:
136
136
  - lib/grape/doc/helper.rb
137
137
  - lib/grape/doc/prototype.rb
138
138
  - test/sample.html
139
- - test/test_doc_gen.rb
139
+ - test/test_all.rb
140
+ - test/test_collector.rb
141
+ - test/test_generator.rb
140
142
  - test/test_helper.rb
141
- - test/test_object_space_collector.rb
142
143
  - test/test_sample_api.rb
143
144
  homepage: https://github.com/adamluzsi/grape-doc
144
145
  licenses: []
@@ -165,8 +166,9 @@ specification_version: 4
165
166
  summary: Documentation generator for Grape module
166
167
  test_files:
167
168
  - test/sample.html
168
- - test/test_doc_gen.rb
169
+ - test/test_all.rb
170
+ - test/test_collector.rb
171
+ - test/test_generator.rb
169
172
  - test/test_helper.rb
170
- - test/test_object_space_collector.rb
171
173
  - test/test_sample_api.rb
172
174
  has_rdoc:
data/test/test_doc_gen.rb DELETED
@@ -1,15 +0,0 @@
1
- require_relative 'test_helper'
2
- describe 'Doc generating' do
3
-
4
- specify 'test documentation generator' do
5
-
6
- var = GrapeDoc.new
7
- puts var.document.to_textile
8
- File.write File.join(RackTestPoc.root,'test','sample.html'),
9
- var.document.to_textile.to_html
10
-
11
- GrapeDoc.generate
12
-
13
- end
14
-
15
- end