grape-doc 0.5.1 → 0.6.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: ed25962e6e4c2190d9b0025d60404ea8752aa84e
4
- data.tar.gz: f589a478394e5edd40c0dd9abb6c4741c06c4a8a
3
+ metadata.gz: 2663f1be3ceff0b795fab8fd8b6b7ade751ae9b9
4
+ data.tar.gz: 0a3c33f2094b71b751ab3b7e6b03a42cbcfeb92a
5
5
  SHA512:
6
- metadata.gz: e3e6b4778c0e4636d5e5ead23a8cbe58c265ad44b34d5c3d587b12df1a475a95a7794d5cfad39881f0d59a775bcd2402f25fc0a04a9231e46759ec8461956af7
7
- data.tar.gz: f45c66227bf5767eb0dafca6026cd071a7c16b560e9b288cbf6b7a8a73969ce9346e2f6e767fba525d99b2deaf77c9fb23faddfa9d45cc11a90b61c4016d3198
6
+ metadata.gz: 4ac309e0dca87872ad31477a9bd2c9009c7b556165a99b9d07911f334b7594fc146b78b895460a23fcb260f07f3e1794ba88f8069945dad0796dbb21811cb6e8
7
+ data.tar.gz: 7b2d9c0e55d506ba5101b58f601da249895265ae450c672e5370d5b8c1bedbd53d5d5d3b885ce309136c13be71fe0745a7ec1c0eca8d05e0b8d8b40b96b88afa
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.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.1.0'
26
+ spec.add_dependency 'rack-test-poc','>= 3.0.0'
27
27
 
28
28
  end
@@ -1,25 +1,34 @@
1
1
  module GrapeDoc
2
2
  class ApiDocParts
3
3
 
4
- module TOC
5
- class << self
6
-
7
- def add_header(header_obj)
8
- if [H1,H2].include? header_obj.class
9
- (@headers ||= []).push(header_obj)
10
- end
11
- end
12
-
13
- def create_toc
14
- @headers.map { |header_obj|
15
- [
16
- '*' * header_obj.markdown.scan(/\d+$/)[0].to_i,
17
- " \"#{header_obj.to_s}\":##{header_obj.instance_variable_get(:@opts)['id']}"
18
- ].join
19
- }.join("\n") + "\n\n"
20
- end
4
+ class TOC
21
5
 
6
+ def headers
7
+ @headers ||= []
22
8
  end
9
+
10
+ def initialize(*args)
11
+ headers.push(*args)
12
+ end
13
+
14
+ def add_header(header_obj)
15
+ headers.push(header_obj)
16
+ end
17
+
18
+ def to_textile
19
+ headers.map { |header_obj|
20
+ [
21
+ '*' * header_obj.markdown.scan(/\d+$/)[0].to_i,
22
+ " \"#{header_obj.to_s}\":##{header_obj.instance_variable_get(:@opts)['id']}"
23
+ ].join
24
+ }.join("\n") + "\n\n"
25
+ end
26
+
27
+ def clear
28
+ headers.clear
29
+ end
30
+
31
+
23
32
  end
24
33
 
25
34
  class H1 < StringObject
@@ -27,7 +36,6 @@ module GrapeDoc
27
36
  def initialize(*args)
28
37
  super
29
38
  @opts['id']= SecureRandom.uuid
30
- TOC.add_header(self)
31
39
  end
32
40
 
33
41
  end
@@ -11,6 +11,16 @@ module GrapeDoc
11
11
  self.push(create(type,*args))
12
12
  end
13
13
 
14
+ def add_toc(*args)
15
+ @toc_added ||= ->{
16
+
17
+ args.map!{|e| Helpers.constantize("GrapeDoc::ApiDocParts::#{Helpers.camelize(e)}") }
18
+ self.insert(1,ApiDocParts::TOC.new(*self.select{|e| args.any?{|klass| e.class == klass }}))
19
+ true
20
+
21
+ }.call
22
+ end
23
+
14
24
  def to_textile
15
25
  require 'RedCloth'
16
26
  RedCloth.new(self.map{|e| e.respond_to?(:to_textile) ? e.to_textile : e.to_s }.join("\n\n"))
@@ -4,11 +4,11 @@ module GrapeDoc
4
4
 
5
5
  attr_reader :options
6
6
  def initialize(opts={})
7
-
7
+
8
8
  raise(ArgumentError,'invalid options given') unless opts.class <= Hash
9
9
  @options = {
10
- 'format' => 'html',
11
- 'path' => File.join(Helpers.doc_folder_path,'api_doc')
10
+ 'format' => 'html',
11
+ 'path' => File.join(Helpers.doc_folder_path,'api_doc')
12
12
  }.merge(opts.reduce({}){|m,o| m.merge!(o[0].to_s => o[1]) ;m})
13
13
  self.options['path'] = self.options['path'].to_s.sub(/\..*$/,'')
14
14
 
@@ -18,10 +18,6 @@ module GrapeDoc
18
18
 
19
19
  end
20
20
 
21
- def document
22
- @api_doc ||= ApiDocumentation.new
23
- end
24
-
25
21
  def save
26
22
 
27
23
  case @options['format'].to_s.downcase
@@ -42,103 +38,123 @@ module GrapeDoc
42
38
 
43
39
  private
44
40
 
41
+ def document
42
+ @api_doc ||= ApiDocumentation.new
43
+ end
44
+
45
45
  def process_head
46
46
  document.add :h1, "Rest Api Documentation (#{RackTestPoc.root.split(File::Separator)[-1]})"
47
47
  end
48
48
 
49
49
  def process_table_of_content
50
- #TODO: Table of content here!
51
- document.insert 1, ApiDocParts::TOC.create_toc
50
+ document.add_toc :h1,:h2
52
51
  end
53
52
 
54
53
  def process_endpoints
55
54
 
55
+ routes_var = []
56
+
56
57
  # Iterates over all subclasses (direct and indirect)
57
58
  Helpers.each_grape_class do |rest_api_model|
58
59
  rest_api_model.routes.each do |route|
59
-
60
60
  next if route.route_path =~ /\(\.:format\)\(\.:format\)$/
61
+ routes_var.push(route)
62
+ end
63
+ end
61
64
 
62
- document.add :h2, "#{route.route_method.to_s.upcase}: #{route.route_path}"
63
- document.add :h3, 'Request'
64
- document.add :h4, 'description'
65
- var = case route.route_description
65
+ routes_var.uniq.each do |route|
66
66
 
67
- when Hash
68
- (route.route_description.find{ |k,v| k == 'desc' || k == :desc } || [])[1] || ''
67
+ document.add :h2, "#{route.route_method.to_s.upcase}: #{route.route_path}"
68
+ document.add :h3, 'Request'
69
+ document.add :h4, 'description'
70
+ var = case route.route_description
69
71
 
70
- when Array
71
- route.route_description
72
+ when Hash
73
+ (route.route_description.find{ |k,v| k == 'desc' || k == :desc } || [])[1] || ''
72
74
 
73
- else
74
- route.route_description.to_s
75
+ when Array
76
+ route.route_description
75
77
 
76
- end
78
+ else
79
+ route.route_description.to_s
77
80
 
78
- document.add :list,[*var]
81
+ end
79
82
 
80
- if route.route_params.length > 0
81
- document.add :h4, 'params'
83
+ document.add :list,[*var]
82
84
 
83
- route.route_params.each do |key,value|
84
- document.add :list,[
85
- document.create(:text,key.to_s,:bold),
86
- document.create(:list,value.map{ |k,v| "#{k}: #{v}" })
87
- ]
85
+ if route.route_params.length > 0
86
+ document.add :h4, 'params'
88
87
 
89
- end
88
+ route.route_params.each do |key,value|
89
+ document.add :list,[
90
+ document.create(:text,key.to_s,:bold),
91
+ document.create(:list,value.map{ |k,v| "#{k}: #{v}" })
92
+ ]
90
93
 
91
94
  end
92
95
 
93
- route_path_var = route.route_path.to_s.sub(/\(\.:format\)$/,'')
94
- @poc_data ||= Helpers.poc_data
95
- if @poc_data && @poc_data.find{|k,v| k =~ /^#{route_path_var}/ }
96
- route_method_var = route.route_method.to_s.upcase
96
+ end
97
97
 
98
- poc_cases = @poc_data.select{|k,v|
99
- k =~ /^#{route_path_var}\.?/ && v.keys.include?(route_method_var)
100
- }
98
+ route_path_var = route.route_path.to_s.sub(/\(\.:format\)$/,'')
99
+ @poc_data ||= Helpers.poc_data
100
+ if @poc_data && @poc_data.find{|k,v| k =~ /^#{route_path_var}/ }
101
+ route_method_var = route.route_method.to_s.upcase
101
102
 
102
- document.add :h3,'Response'
103
- document.add :h4,'example'
103
+ poc_cases = @poc_data.select{|k,v|
104
+ k =~ /^#{route_path_var}\.?/ && v.keys.include?(route_method_var)
105
+ }
104
106
 
105
- poc_cases.each do |poc_path,poc_data|
106
- poc_opts = poc_data[route_method_var] || next
107
+ document.add :h3,'Response'
108
+ document.add :h4,'Example(s)'
107
109
 
108
- document.add :h5,"curl sample with #{poc_path}"
109
- document.add :raw,[
110
- "$ curl ",
111
- "-X #{route_method_var} ",
112
- "\"http://#{ -> { poc_opts['response']['headers']['HTTP_HOST'] rescue 'example.org' }.call}",
113
- "#{poc_path}?","#{poc_opts['request']['query']['raw']}\" ",
114
- poc_opts['request']['headers'].map{|k,v| "-H \"#{k}: #{v}\"" }.join(' ')
115
- ].join
110
+ poc_cases.each do |poc_path,poc_data|
111
+ poc_examples = poc_data[route_method_var] || next
112
+ poc_examples.each_with_index do |poc_opts,i|
113
+ i = i + 1
114
+ begin
116
115
 
117
- document.add :list,[
118
- "status code: #{poc_opts['response']['status']}",
119
- "format type: #{poc_opts['response']['format']}"
120
- ]
116
+ document.add :h5,"#{i}. curl sample"
117
+ document.add :raw,[
118
+ "$ curl ",
119
+ "-X #{route_method_var} ",
120
+ "\"http://#{ -> { poc_opts['response']['headers']['HTTP_HOST'] rescue 'example.org' }.call}",
121
+ "#{poc_path}?","#{poc_opts['request']['query']['raw']}\" ",
122
+ poc_opts['request']['headers'].map{|k,v| "-H \"#{k}: #{v}\"" }.join(' ')
123
+ ].join
121
124
 
122
- document.add :h6,'raw response body'
123
- document.add :raw,poc_opts['response']['raw_body']
125
+ document.add :list,[
126
+ "status code: #{poc_opts['response']['status']}",
127
+ "format type: #{poc_opts['response']['format']}"
128
+ ]
124
129
 
125
- if JSON.respond_to?(:pretty_generate)
130
+ document.add :h5,'Response Body'
126
131
 
127
- document.add :h6,'json formatted body with Class types'
128
- pretty_object = JSON.pretty_generate(
129
- Parser.typer(poc_opts['response']['body'])
130
- ) rescue Parser.typer(poc_opts['response']['body'])
132
+ document.add(:h6,'Raw version')
133
+ document.add(:raw,poc_opts['response']['body']['raw'])
131
134
 
132
- document.add :raw,pretty_object
135
+ if JSON.respond_to?(:pretty_generate)
133
136
 
137
+ document.add(:h6,'json formatted body with Class types')
138
+ pretty_object = JSON.pretty_generate(
139
+ Parser.typer(poc_opts['response']['body']['object'])
140
+ ) rescue Parser.typer(poc_opts['response']['body']['object'])
134
141
 
135
- end
142
+ document.add(:raw,pretty_object)
143
+
144
+ end
136
145
 
146
+ if poc_opts['response']['body']['description']
147
+ document.add(:h6,'Yaml formatted body with descriptions')
148
+ document.add(:raw,poc_opts['response']['body']['description'].to_yaml)
149
+ end
137
150
 
151
+ rescue
152
+ end
138
153
  end
139
-
140
154
  end
155
+
141
156
  end
157
+
142
158
  end
143
159
 
144
160
  end
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.5.1
4
+ version: 0.6.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-14 00:00:00.000000000 Z
11
+ date: 2014-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,16 +98,16 @@ dependencies:
98
98
  name: rack-test-poc
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: 1.1.0
103
+ version: 3.0.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.1.0
110
+ version: 3.0.0
111
111
  description: " Documentation generator for Grape module compatible with Redmine/textile
112
112
  and Html formats "
113
113
  email: