grape-app-doc 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +95 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/grape-app-doc.gemspec +24 -0
- data/lib/grape/app/doc.rb +45 -0
- data/lib/grape/app/doc/attribute.rb +11 -0
- data/lib/grape/app/doc/config.rb +7 -0
- data/lib/grape/app/doc/document.rb +20 -0
- data/lib/grape/app/doc/endpoint.rb +54 -0
- data/lib/grape/app/doc/entity.rb +45 -0
- data/lib/grape/app/doc/header.rb +9 -0
- data/lib/grape/app/doc/parameter.rb +30 -0
- data/lib/grape/app/doc/templates/slate.erb +17 -0
- data/spec/doc/document_spec.rb +15 -0
- data/spec/doc/endpoint_spec.rb +36 -0
- data/spec/doc/entity_spec.rb +16 -0
- data/spec/doc/header_spec.rb +9 -0
- data/spec/doc/parameter_spec.rb +35 -0
- data/spec/doc_spec.rb +9 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/support/mock_api.rb +61 -0
- data/spec/support/mock_entities.rb +69 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 13cedc518a306358ab83dce425f647ded45942a68ff4324b664f0dda0bd8b8c2
|
4
|
+
data.tar.gz: 297fa97989f91b2bdb714f1218e49be18d15a2683b9c65622ceef0502de925f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3523de7c20d0c066952e09e913380ad38a2777aa8a80f032e6e4b8cb4b312cd18355ae8055886535ba9f4cf144a986ac770a4aec5862d7450162e618e766faf
|
7
|
+
data.tar.gz: 7e805df0a668c0995ae7103f43971182424651d753b1a26f4651d2993eb2e8a87fc1a3256cdea2b9bda30d70eab162f6a6c3348fac3fd7ec61219e417394c4b9
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grape-app-doc (0.1.0)
|
5
|
+
grape-app (>= 0.5.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.1)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
axiom-types (0.1.1)
|
16
|
+
descendants_tracker (~> 0.0.4)
|
17
|
+
ice_nine (~> 0.11.0)
|
18
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
19
|
+
builder (3.2.3)
|
20
|
+
coercible (1.0.0)
|
21
|
+
descendants_tracker (~> 0.0.1)
|
22
|
+
concurrent-ruby (1.0.5)
|
23
|
+
descendants_tracker (0.0.4)
|
24
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
25
|
+
diff-lcs (1.3)
|
26
|
+
equalizer (0.0.11)
|
27
|
+
grape (1.1.0)
|
28
|
+
activesupport
|
29
|
+
builder
|
30
|
+
mustermann-grape (~> 1.0.0)
|
31
|
+
rack (>= 1.3.0)
|
32
|
+
rack-accept
|
33
|
+
virtus (>= 1.0.0)
|
34
|
+
grape-app (0.5.3)
|
35
|
+
activesupport
|
36
|
+
grape (>= 1.0.0)
|
37
|
+
grape-entity
|
38
|
+
rack-cors
|
39
|
+
rack-ssl-enforcer
|
40
|
+
thor
|
41
|
+
grape-entity (0.7.1)
|
42
|
+
activesupport (>= 4.0)
|
43
|
+
multi_json (>= 1.3.2)
|
44
|
+
i18n (1.1.0)
|
45
|
+
concurrent-ruby (~> 1.0)
|
46
|
+
ice_nine (0.11.2)
|
47
|
+
minitest (5.11.3)
|
48
|
+
multi_json (1.13.1)
|
49
|
+
mustermann (1.0.2)
|
50
|
+
mustermann-grape (1.0.0)
|
51
|
+
mustermann (~> 1.0.0)
|
52
|
+
rack (2.0.5)
|
53
|
+
rack-accept (0.4.5)
|
54
|
+
rack (>= 0.4)
|
55
|
+
rack-cors (1.0.2)
|
56
|
+
rack-ssl-enforcer (0.2.9)
|
57
|
+
rake (12.3.1)
|
58
|
+
rspec (3.8.0)
|
59
|
+
rspec-core (~> 3.8.0)
|
60
|
+
rspec-expectations (~> 3.8.0)
|
61
|
+
rspec-mocks (~> 3.8.0)
|
62
|
+
rspec-core (3.8.0)
|
63
|
+
rspec-support (~> 3.8.0)
|
64
|
+
rspec-expectations (3.8.1)
|
65
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
66
|
+
rspec-support (~> 3.8.0)
|
67
|
+
rspec-its (1.2.0)
|
68
|
+
rspec-core (>= 3.0.0)
|
69
|
+
rspec-expectations (>= 3.0.0)
|
70
|
+
rspec-mocks (3.8.0)
|
71
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
72
|
+
rspec-support (~> 3.8.0)
|
73
|
+
rspec-support (3.8.0)
|
74
|
+
thor (0.20.0)
|
75
|
+
thread_safe (0.3.6)
|
76
|
+
tzinfo (1.2.5)
|
77
|
+
thread_safe (~> 0.1)
|
78
|
+
virtus (1.0.5)
|
79
|
+
axiom-types (~> 0.1)
|
80
|
+
coercible (~> 1.0)
|
81
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
82
|
+
equalizer (~> 0.0, >= 0.0.9)
|
83
|
+
|
84
|
+
PLATFORMS
|
85
|
+
ruby
|
86
|
+
|
87
|
+
DEPENDENCIES
|
88
|
+
bundler
|
89
|
+
grape-app-doc!
|
90
|
+
rake
|
91
|
+
rspec
|
92
|
+
rspec-its
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
1.16.3
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Grape::App::Doc
|
2
|
+
|
3
|
+
Currently, its experimental work-in-progress. Do not use!
|
4
|
+
|
5
|
+
## Documentation Guidelines
|
6
|
+
|
7
|
+
1. Each API endpoint must have a `success` description, referencing an entity class. Example:
|
8
|
+
```ruby
|
9
|
+
desc "List" do
|
10
|
+
success Post::Entity
|
11
|
+
end
|
12
|
+
get '/posts' do
|
13
|
+
present Post.all
|
14
|
+
end
|
15
|
+
```
|
16
|
+
2. Each entity exposure should have documentation
|
17
|
+
3. Tntity exposure type must be declared as a class. Example:
|
18
|
+
```ruby
|
19
|
+
class Post::Entity < Grape::Entity
|
20
|
+
expose :title, documentation: { type: String, desc: "Status update text." }
|
21
|
+
end
|
22
|
+
```
|
23
|
+
4. Entity exposure documentation may contain the following keys: `type`, `desc`, `required`, `is_array`, `values`, `default`
|
24
|
+
5. TODO: more
|
25
|
+
|
26
|
+
## Licence
|
27
|
+
|
28
|
+
```
|
29
|
+
Copyright (c) 2015 Black Square Media
|
30
|
+
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
32
|
+
a copy of this software and associated documentation files (the
|
33
|
+
"Software"), to deal in the Software without restriction, including
|
34
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
35
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
+
permit persons to whom the Software is furnished to do so, subject to
|
37
|
+
the following conditions:
|
38
|
+
|
39
|
+
The above copyright notice and this permission notice shall be
|
40
|
+
included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
43
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
45
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
46
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
47
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
48
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
49
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'grape-app-doc'
|
5
|
+
s.version = '0.1.0'
|
6
|
+
s.authors = ['Black Square Media Ltd']
|
7
|
+
s.email = ['info@blacksquaremedia.com']
|
8
|
+
s.summary = %{Grape app documentation}
|
9
|
+
s.description = %{EXPERIMENTAL}
|
10
|
+
s.homepage = 'https://github.com/bsm/grape-app-doc'
|
11
|
+
s.license = 'MIT'
|
12
|
+
|
13
|
+
s.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^spec/}) }
|
14
|
+
s.test_files = `git ls-files -z -- spec/*`.split("\x0")
|
15
|
+
s.require_paths = ['lib']
|
16
|
+
s.required_ruby_version = '>= 2.2.0'
|
17
|
+
|
18
|
+
s.add_dependency 'grape-app', '>= 0.5.3'
|
19
|
+
s.add_development_dependency 'bundler'
|
20
|
+
s.add_development_dependency 'rspec'
|
21
|
+
s.add_development_dependency 'rspec-its'
|
22
|
+
s.add_development_dependency 'rake'
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'grape-app'
|
2
|
+
require 'grape-entity'
|
3
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
4
|
+
require 'rack/utils'
|
5
|
+
|
6
|
+
module Grape::App::Doc
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def config
|
10
|
+
Grape::App::Doc::Config
|
11
|
+
end
|
12
|
+
|
13
|
+
def configure(&block)
|
14
|
+
config = Grape::App::Doc::Config
|
15
|
+
block.call(config) if block
|
16
|
+
config
|
17
|
+
end
|
18
|
+
|
19
|
+
def create(api = Grape::App)
|
20
|
+
Grape::App::Doc::Document.new(api)
|
21
|
+
end
|
22
|
+
|
23
|
+
def next_increment!
|
24
|
+
@increment ||= 0
|
25
|
+
@increment += 1
|
26
|
+
end
|
27
|
+
|
28
|
+
def doc_error(message)
|
29
|
+
method = config.fail_on_errors ? :abort : :warn
|
30
|
+
Kernel.send method, " ! #{message}"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
%w|
|
36
|
+
config
|
37
|
+
endpoint
|
38
|
+
document
|
39
|
+
header
|
40
|
+
parameter
|
41
|
+
attribute
|
42
|
+
entity
|
43
|
+
|.each do |name|
|
44
|
+
require "grape/app/doc/#{name}"
|
45
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Grape::App::Doc::Attribute
|
2
|
+
attr_reader :key, :type, :desc, :values, :default
|
3
|
+
|
4
|
+
def initialize(key, opts = {})
|
5
|
+
@key = key
|
6
|
+
@type = opts[:type]
|
7
|
+
@desc = opts[:desc] || opts[:description] || ""
|
8
|
+
@values = opts[:values] || []
|
9
|
+
@default = opts[:default]
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Grape::App::Doc::Document
|
2
|
+
|
3
|
+
attr_reader :entities, :endpoints
|
4
|
+
|
5
|
+
def initialize(api = Grape::App)
|
6
|
+
registry = Grape::App::Doc::Entity::Registry.new
|
7
|
+
@endpoints = api.routes.map do |route|
|
8
|
+
Grape::App::Doc::Endpoint.new(route, registry)
|
9
|
+
end
|
10
|
+
@entities = registry.values
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param [String|Symbol] template path
|
14
|
+
# @return [String] output formatted as template
|
15
|
+
def output(template)
|
16
|
+
template = File.expand_path("../templates/#{template}.erb") unless File.exist?(template)
|
17
|
+
ERB.new(File.read(template), nil, '-').result(binding)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class Grape::App::Doc::Endpoint
|
2
|
+
Status = Struct.new(:code, :desc, :entity)
|
3
|
+
|
4
|
+
attr_reader :headers, :failure, :success, :version, :params, :request_method,
|
5
|
+
:namespace, :description, :details, :raw_path
|
6
|
+
|
7
|
+
def initialize(route, registry)
|
8
|
+
@request_method = route.request_method || 'GET'
|
9
|
+
|
10
|
+
@version = route.version || ''
|
11
|
+
@namespace = route.namespace || ''
|
12
|
+
@description = route.description || ''
|
13
|
+
@details = route.details || ''
|
14
|
+
@raw_path = route.path
|
15
|
+
|
16
|
+
@success = parse_status(route, registry, route.entity || [200, "OK"])
|
17
|
+
@failure = Array(route.http_codes).map do |item|
|
18
|
+
parse_status(route, registry, item)
|
19
|
+
end.compact
|
20
|
+
@headers = (route.headers || {}).map do |name, opts|
|
21
|
+
Grape::App::Doc::Header.new(name, opts)
|
22
|
+
end
|
23
|
+
@params = (route.params || {}).map do |name, opts|
|
24
|
+
Grape::App::Doc::Parameter.new(name, opts) if opts.is_a?(Hash)
|
25
|
+
end.compact
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [String] friendly path string
|
29
|
+
def path
|
30
|
+
@path ||= raw_path.sub(':version', version).sub(/\(\.[^\)]+$/, '')
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [String] extracted the resource name
|
34
|
+
def resource
|
35
|
+
namespace.split('/').last ||
|
36
|
+
raw_path.split('/:version').last.match('\/(\w*?)[\.\/\(]').captures.first
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def parse_status(route, registry, item)
|
42
|
+
if item.is_a?(Array) && item.size > 1
|
43
|
+
code, desc, entity = item
|
44
|
+
code = Rack::Utils::SYMBOL_TO_STATUS_CODE[code] if code.is_a?(Symbol)
|
45
|
+
Status.new(code, desc, registry.register(entity))
|
46
|
+
elsif item.is_a?(Class) && item <= Grape::Entity
|
47
|
+
Status.new(200, "OK", registry.register(item))
|
48
|
+
else
|
49
|
+
Grape::App::Doc.doc_error("route #{route} contains an invalid definition #{item.inspect}")
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class Grape::App::Doc::Entity
|
2
|
+
attr_reader :name, :desc, :uid, :attributes
|
3
|
+
|
4
|
+
def initialize(klass, registry)
|
5
|
+
@name = (klass.meta[:type] if klass.respond_to?(:meta)) || klass.name.gsub('::Entity', '')
|
6
|
+
@desc = (klass.meta[:desc] || klass.meta[:description] if klass.respond_to?(:meta)) || ""
|
7
|
+
@uid = [name.parameterize, Grape::App::Doc.next_increment!].join('-')
|
8
|
+
|
9
|
+
exposures = klass.root_exposures
|
10
|
+
exposures = exposures.map do |key, opts|
|
11
|
+
LegacyExposure.new(key, opts[:using], opts[:documentation])
|
12
|
+
end if klass.root_exposures.is_a?(Hash)
|
13
|
+
|
14
|
+
@attributes = exposures.map do |exp|
|
15
|
+
doc = exp.documentation.try(:dup)
|
16
|
+
unless doc.is_a?(Hash)
|
17
|
+
Grape::App::Doc.doc_error("#{klass}: #{name} exposure does not have documentation")
|
18
|
+
next
|
19
|
+
end
|
20
|
+
unless doc.key?(:type)
|
21
|
+
Grape::App::Doc.doc_error("#{klass}: #{name} exposure does not have :type documentation")
|
22
|
+
next
|
23
|
+
end
|
24
|
+
if exp.respond_to?(:using_class) && exp.using_class
|
25
|
+
registry.register(exp.using_class)
|
26
|
+
end
|
27
|
+
Grape::App::Doc::Attribute.new(exp.key, doc)
|
28
|
+
end.compact
|
29
|
+
end
|
30
|
+
|
31
|
+
def example
|
32
|
+
@example ||= attributes.inject({}) do |acc, attr|
|
33
|
+
acc[attr.key] = attr.type
|
34
|
+
acc
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Registry < Hash
|
39
|
+
def register(klass)
|
40
|
+
self[klass] ||= Grape::App::Doc::Entity.new(klass, self) if klass
|
41
|
+
end
|
42
|
+
end
|
43
|
+
LegacyExposure = Struct.new(:key, :using_class, :documentation)
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Grape::App::Doc::Parameter
|
2
|
+
attr_reader :name, :doc, :values, :default, :type, :desc
|
3
|
+
|
4
|
+
def initialize(name, opts = {})
|
5
|
+
@name = name.to_s
|
6
|
+
@doc = opts[:documentation] || {}
|
7
|
+
@desc = doc[:desc] || doc[:description] || ''
|
8
|
+
@values = opts[:values] || []
|
9
|
+
@default = opts[:default]
|
10
|
+
@type = normalize_type(doc[:type] || opts[:type])
|
11
|
+
@required = opts[:required]
|
12
|
+
end
|
13
|
+
|
14
|
+
def required?
|
15
|
+
@required
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def normalize_type(str)
|
21
|
+
return 'String' unless str.is_a?(String)
|
22
|
+
|
23
|
+
str = str.dup
|
24
|
+
str.sub! 'Virtus::Attribute::', ''
|
25
|
+
str.sub! 'Axiom::Types::', ''
|
26
|
+
str.sub! 'BigDecimal', 'Decimal'
|
27
|
+
str
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<% unless entities.empty? -%>
|
2
|
+
# Entities
|
3
|
+
|
4
|
+
<% entities.each do |entity| -%>
|
5
|
+
<%= entity.name %>
|
6
|
+
|
7
|
+
<% end -%>
|
8
|
+
<% end -%>
|
9
|
+
|
10
|
+
<% endpoints.group_by(&:version).each do |version, endpoints| -%>
|
11
|
+
# Endpoints <%= "<sup>#{version}</sup>" if version %>
|
12
|
+
|
13
|
+
<% endpoints.each do |endpoint| -%>
|
14
|
+
<%= endpoint.path %>
|
15
|
+
|
16
|
+
<% end -%>
|
17
|
+
<% end -%>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Grape::App::Doc::Document do
|
4
|
+
|
5
|
+
it 'should parse endpoints' do
|
6
|
+
expect(subject.endpoints.size).to eq(4)
|
7
|
+
expect(subject.endpoints[0]).to be_instance_of(Grape::App::Doc::Endpoint)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should parse entities' do
|
11
|
+
expect(subject.entities.size).to eq(4)
|
12
|
+
expect(subject.entities[0]).to be_instance_of(Grape::App::Doc::Entity)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Grape::App::Doc::Endpoint do
|
4
|
+
|
5
|
+
let :route do
|
6
|
+
Grape::Router::Route.new :get, '/:version/something(.:format)',
|
7
|
+
version: 'v2015',
|
8
|
+
prefix: '/api',
|
9
|
+
namespace: '/',
|
10
|
+
headers: { XAdminToken: { description: 'Valdates your identity', required: true }},
|
11
|
+
params: {'id'=>'', 'title'=>{:required=>true, :documentation=>{:desc=>'A title'}}}
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { described_class.new route, Grape::App::Doc::Entity::Registry.new }
|
15
|
+
|
16
|
+
it 'should parse routes with defaults' do
|
17
|
+
expect(subject.request_method).to eq('GET')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should parse success/failures' do
|
21
|
+
expect(subject.success).to be_instance_of(described_class::Status)
|
22
|
+
expect(subject.success.code).to eq(200)
|
23
|
+
expect(subject.failure).to eq([])
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should parse headers' do
|
27
|
+
expect(subject.headers.size).to eq(1)
|
28
|
+
expect(subject.headers[0]).to be_instance_of(Grape::App::Doc::Header)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should parse params' do
|
32
|
+
expect(subject.params.size).to eq(1)
|
33
|
+
expect(subject.params[0]).to be_instance_of(Grape::App::Doc::Parameter)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Grape::App::Doc::Entity do
|
4
|
+
|
5
|
+
subject { described_class.new Post::Entity, described_class::Registry.new }
|
6
|
+
|
7
|
+
its(:name) { is_expected.to eq("Post") }
|
8
|
+
its(:desc) { is_expected.to eq("") }
|
9
|
+
its(:uid) { is_expected.to match(/post-\d+/) }
|
10
|
+
|
11
|
+
it "should parse attributes" do
|
12
|
+
expect(subject.attributes.size).to eq(5)
|
13
|
+
expect(subject.attributes[0]).to be_instance_of(Grape::App::Doc::Attribute)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Grape::App::Doc::Header do
|
4
|
+
subject { described_class.new :name, desc: "DESC", other: "custom" }
|
5
|
+
|
6
|
+
its(:name) { is_expected.to eq("name") }
|
7
|
+
its(:desc) { is_expected.to eq("DESC") }
|
8
|
+
its(:opts) { is_expected.to be_instance_of(Hash) }
|
9
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Grape::App::Doc::Parameter do
|
4
|
+
subject { described_class.new :name, type: "String", required: true }
|
5
|
+
|
6
|
+
its(:name) { is_expected.to eq("name") }
|
7
|
+
its(:desc) { is_expected.to eq("") }
|
8
|
+
its(:doc) { is_expected.to eq({}) }
|
9
|
+
its(:type) { is_expected.to eq("String") }
|
10
|
+
its(:values) { is_expected.to eq([]) }
|
11
|
+
its(:default) { is_expected.to be_nil }
|
12
|
+
its(:required?) { is_expected.to be_truthy }
|
13
|
+
|
14
|
+
it "should allow custom type documentation" do
|
15
|
+
pm = described_class.new :name, type: "String", documentation: { type: "[DateTime]" }
|
16
|
+
expect(pm.type).to eq("[DateTime]")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should normalize types" do
|
20
|
+
pm = described_class.new :name, type: "[Integer]"
|
21
|
+
expect(pm.type).to eq("[Integer]")
|
22
|
+
|
23
|
+
pm = described_class.new :name, type: "BigDecimal"
|
24
|
+
expect(pm.type).to eq("Decimal")
|
25
|
+
|
26
|
+
pm = described_class.new :name, type: "Virtus::Attribute::Boolean"
|
27
|
+
expect(pm.type).to eq("Boolean")
|
28
|
+
|
29
|
+
pm = described_class.new :name, type: "[Axiom::Types::Decimal]"
|
30
|
+
expect(pm.type).to eq("[Decimal]")
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
end
|
data/spec/doc_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/its'
|
3
|
+
require 'grape/app/doc'
|
4
|
+
|
5
|
+
require 'support/mock_entities'
|
6
|
+
require 'support/mock_api'
|
7
|
+
|
8
|
+
RSpec.configure do |c|
|
9
|
+
|
10
|
+
c.before :each do
|
11
|
+
allow(Kernel).to receive(:warn) do |m|
|
12
|
+
fail(m)
|
13
|
+
end
|
14
|
+
Grape::App::Doc.class_variable_set(:@@increment, 0)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module MockAPI
|
2
|
+
class V1 < Grape::API
|
3
|
+
prefix '/a'
|
4
|
+
version 'v1'
|
5
|
+
|
6
|
+
desc 'Returns the service status' do
|
7
|
+
success [:no_content, 'Up']
|
8
|
+
failure [[503, 'Down']]
|
9
|
+
headers XAdminToken: { description: 'Valdates your identity', required: true }
|
10
|
+
end
|
11
|
+
get '/status' do
|
12
|
+
status 204
|
13
|
+
end
|
14
|
+
|
15
|
+
resource 'posts' do
|
16
|
+
|
17
|
+
desc 'Returns a list of posts' do
|
18
|
+
success Post::Entity
|
19
|
+
end
|
20
|
+
params do
|
21
|
+
optional :sort, type: Hash, documentation: { desc: "Sort order" } do
|
22
|
+
requires :col, values: ['id', 'title'], documentation: { desc: "Sort column" }
|
23
|
+
optional :dir, values: ['asc', 'desc'], default: 'asc', documentation: { desc: "Sort direction" }
|
24
|
+
end
|
25
|
+
optional :search, type: String, documentation: { desc: "search string" }
|
26
|
+
end
|
27
|
+
get '' do
|
28
|
+
present [Post.new, Post.new]
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Returns a post' do
|
32
|
+
success [200, "Found", Post::Entity]
|
33
|
+
failure [[404, 'Not found']]
|
34
|
+
end
|
35
|
+
get ':id' do
|
36
|
+
present Post.new
|
37
|
+
end
|
38
|
+
|
39
|
+
desc 'Creates a post' do
|
40
|
+
success [:created, "Created"]
|
41
|
+
failure [
|
42
|
+
[400, 'Validation failed', ValidationErrors::Entity],
|
43
|
+
[401, 'Bad API token'],
|
44
|
+
]
|
45
|
+
end
|
46
|
+
params do
|
47
|
+
requires :title, documentation: { desc: "A title" }
|
48
|
+
requires :author, type: Hash, documentation: { desc: "The author attributes" } do
|
49
|
+
requires :name, :email
|
50
|
+
end
|
51
|
+
optional :status, values: ['draft', 'live'], default: 'draft'
|
52
|
+
end
|
53
|
+
post '' do
|
54
|
+
status :created
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
Grape::App.mount MockAPI::V1
|
@@ -0,0 +1,69 @@
|
|
1
|
+
class ValidationErrors < Array
|
2
|
+
def errors
|
3
|
+
self
|
4
|
+
end
|
5
|
+
|
6
|
+
class Entity < Grape::Entity
|
7
|
+
expose :errors, documentation: { type: "String[]", desc: "Validation errors" }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Author
|
12
|
+
def name
|
13
|
+
"Alice Smith"
|
14
|
+
end
|
15
|
+
|
16
|
+
def email
|
17
|
+
"alice.smith@example.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
class Entity < Grape::Entity
|
21
|
+
expose :name, documentation: { type: "String", desc: "A name" }
|
22
|
+
expose :email, documentation: { type: "String", desc: "The email address" }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Comment
|
27
|
+
def comment
|
28
|
+
"A shared opinion"
|
29
|
+
end
|
30
|
+
|
31
|
+
def author
|
32
|
+
Author.new
|
33
|
+
end
|
34
|
+
|
35
|
+
class Entity < Grape::Entity
|
36
|
+
expose :comment, documentation: { type: "String", desc: "The comment" }
|
37
|
+
expose :author, using: Author::Entity, documentation: { type: "Author", desc: "The comment author" }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Post
|
42
|
+
def id
|
43
|
+
33
|
44
|
+
end
|
45
|
+
|
46
|
+
def title
|
47
|
+
"A post title"
|
48
|
+
end
|
49
|
+
|
50
|
+
def author
|
51
|
+
Author.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def comments
|
55
|
+
@comments ||= [Comment.new, Comment.new]
|
56
|
+
end
|
57
|
+
|
58
|
+
def status
|
59
|
+
"live"
|
60
|
+
end
|
61
|
+
|
62
|
+
class Entity < Grape::Entity
|
63
|
+
expose :id, documentation: { type: "Integer", desc: "Unique identifier" }
|
64
|
+
expose :title, documentation: { type: "String", desc: "A friendly title" }
|
65
|
+
expose :author, using: Author::Entity, documentation: { type: "Author", desc: "The author" }
|
66
|
+
expose :comments, using: Comment::Entity, documentation: { type: "Comment[]", desc: "Comments associatated with this post" }
|
67
|
+
expose :status, documentation: { type: "String", values: ['draft', 'live'], default: 'draft', description: "The status" }
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-app-doc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Black Square Media Ltd
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: grape-app
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.5.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.5.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-its
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: EXPERIMENTAL
|
84
|
+
email:
|
85
|
+
- info@blacksquaremedia.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- grape-app-doc.gemspec
|
95
|
+
- lib/grape/app/doc.rb
|
96
|
+
- lib/grape/app/doc/attribute.rb
|
97
|
+
- lib/grape/app/doc/config.rb
|
98
|
+
- lib/grape/app/doc/document.rb
|
99
|
+
- lib/grape/app/doc/endpoint.rb
|
100
|
+
- lib/grape/app/doc/entity.rb
|
101
|
+
- lib/grape/app/doc/header.rb
|
102
|
+
- lib/grape/app/doc/parameter.rb
|
103
|
+
- lib/grape/app/doc/templates/slate.erb
|
104
|
+
- spec/doc/document_spec.rb
|
105
|
+
- spec/doc/endpoint_spec.rb
|
106
|
+
- spec/doc/entity_spec.rb
|
107
|
+
- spec/doc/header_spec.rb
|
108
|
+
- spec/doc/parameter_spec.rb
|
109
|
+
- spec/doc_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/support/mock_api.rb
|
112
|
+
- spec/support/mock_entities.rb
|
113
|
+
homepage: https://github.com/bsm/grape-app-doc
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 2.2.0
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.7.6
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Grape app documentation
|
137
|
+
test_files:
|
138
|
+
- spec/doc/document_spec.rb
|
139
|
+
- spec/doc/endpoint_spec.rb
|
140
|
+
- spec/doc/entity_spec.rb
|
141
|
+
- spec/doc/header_spec.rb
|
142
|
+
- spec/doc/parameter_spec.rb
|
143
|
+
- spec/doc_spec.rb
|
144
|
+
- spec/spec_helper.rb
|
145
|
+
- spec/support/mock_api.rb
|
146
|
+
- spec/support/mock_entities.rb
|