grape-apiary 0.0.4 → 0.1.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 +4 -4
- data/.rubocop.yml +35 -5
- data/.travis.yml +3 -3
- data/CHANGELOG.md +11 -0
- data/Gemfile +0 -14
- data/Guardfile +2 -2
- data/README.md +27 -14
- data/Rakefile +1 -1
- data/bin/grape-apiary +23 -0
- data/grape-apiary.gemspec +13 -5
- data/lib/grape-apiary.rb +9 -9
- data/lib/grape-apiary/blueprint.rb +17 -17
- data/lib/grape-apiary/config.rb +7 -2
- data/lib/grape-apiary/parameter.rb +8 -7
- data/lib/grape-apiary/resource.rb +7 -3
- data/lib/grape-apiary/route.rb +4 -4
- data/lib/grape-apiary/sample_generator.rb +29 -15
- data/lib/grape-apiary/tasks/grape-apiary.rake +15 -0
- data/lib/grape-apiary/templates/blueprint.md.erb +3 -2
- data/lib/grape-apiary/templates/properties.md.erb +7 -0
- data/lib/grape-apiary/version.rb +3 -1
- data/spec/grape-apiary/blueprint_spec.rb +4 -0
- data/spec/grape-apiary/config_spec.rb +12 -0
- data/spec/grape-apiary/resource_spec.rb +1 -1
- data/spec/grape-apiary/sample_generator_spec.rb +1 -0
- data/spec/support/sample_api.rb +8 -8
- metadata +31 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af6793664c67c149446b22cc641327ae4befe6b3
|
4
|
+
data.tar.gz: 8dc668050c54078a9c011b59a3d0ee41928ae3a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31e332776b5469706fc694d0310222c842b474b699fe065a56c51673e6e7ab5919480d666218984a595cfd43113a5616e52188a58b6908837035a97aced82f2b
|
7
|
+
data.tar.gz: ff66609f82ed054e6e9e7eb26c7f301cb63296ffe3f41931f4efa050557af2109fbecdc9ff7a0cfe3f44207981ee8f7003ae3156260a0d5199b06c112f9d386e
|
data/.rubocop.yml
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
AllCops:
|
2
2
|
Excludes:
|
3
3
|
- Rakefile
|
4
|
-
- vendor
|
5
|
-
- bin
|
4
|
+
- vendor/**/*
|
5
|
+
- bin/**/
|
6
6
|
|
7
7
|
Documentation:
|
8
8
|
# don't require classes to be documented
|
@@ -12,7 +12,37 @@ Encoding:
|
|
12
12
|
# no need to always specify encoding
|
13
13
|
Enabled: false
|
14
14
|
|
15
|
-
|
16
|
-
#
|
17
|
-
|
15
|
+
AlignParameters:
|
16
|
+
# allow for multi-line methods to have normal indentation.
|
17
|
+
# for example:
|
18
|
+
#
|
19
|
+
# Person.where(
|
20
|
+
# first_name: 'tom',
|
21
|
+
# last_name: 'foolery'
|
22
|
+
# )
|
23
|
+
EnforcedStyle: with_fixed_indentation
|
18
24
|
|
25
|
+
Style/AlignParameters:
|
26
|
+
# allow for end of if to be aligned with a variable.
|
27
|
+
# for example:
|
28
|
+
#
|
29
|
+
# foo = if a == b
|
30
|
+
# 'bar'
|
31
|
+
# else
|
32
|
+
# 'baz'
|
33
|
+
# end
|
34
|
+
EnforcedStyle: with_fixed_indentation
|
35
|
+
|
36
|
+
Style/ElseAlignment:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Lint/EndAlignment:
|
40
|
+
AlignWith: variable
|
41
|
+
|
42
|
+
ClassAndModuleChildren:
|
43
|
+
# ok to use compact style when modules are predefined.
|
44
|
+
# for example the following is fine so long as we're sure that
|
45
|
+
# module MDB has already been required/defined.
|
46
|
+
#
|
47
|
+
# class Foo::Bar; end
|
48
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
|
+
|
5
|
+
## 0.1.0 (2016-01-25)
|
6
|
+
#### Features
|
7
|
+
- [PR #5](https://github.com/technekes/grape-apiary/pull/5) - Rake task persists docs to disk from [@bigfleet](https://github.com/bigfleet).
|
8
|
+
|
9
|
+
## 0.0.1 - 0.0.4 (2014-02-10)
|
10
|
+
#### Features
|
11
|
+
- Initial implementation from [@jallen3d](https://github.com/johnallen3d).
|
data/Gemfile
CHANGED
@@ -2,17 +2,3 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in grape-apiary.gemspec
|
4
4
|
gemspec
|
5
|
-
|
6
|
-
gem 'grape', github: 'intridea/grape'
|
7
|
-
|
8
|
-
group :development, :test do
|
9
|
-
gem 'coveralls', '~> 0.7'
|
10
|
-
gem 'rspec', '~> 2.14'
|
11
|
-
gem 'bundler', '~> 1.5'
|
12
|
-
gem 'rake', '~> 10.0'
|
13
|
-
gem 'rubocop', '~> 0.18'
|
14
|
-
gem 'pry', '~> 0.9'
|
15
|
-
gem 'guard', '~> 2.4'
|
16
|
-
gem 'guard-rspec', '~> 4.2'
|
17
|
-
gem 'guard-bundler', '~> 2.0'
|
18
|
-
end
|
data/Guardfile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# More info at https://github.com/guard/guard#readme
|
2
2
|
|
3
|
-
guard 'rspec', :
|
3
|
+
guard 'rspec', version: 2 do
|
4
4
|
watch(%r{^spec/.+_spec\.rb$})
|
5
5
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
6
|
-
watch('spec/spec_helper.rb') {
|
6
|
+
watch('spec/spec_helper.rb') { 'spec/' }
|
7
7
|
end
|
8
8
|
|
9
9
|
guard 'bundler' do
|
data/README.md
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
# GrapeApiary
|
2
2
|
|
3
|
-
[](https://codeclimate.com/github/connexio-labs/grape-apiary)
|
4
|
+
[](https://travis-ci.org/connexio-labs/grape-apiary)
|
5
|
+
[](https://coveralls.io/r/connexio-labs/grape-apiary)
|
6
|
+
[](https://gemnasium.com/connexio-labs/grape-apiary)
|
7
7
|
[](http://badge.fury.io/rb/grape-apiary)
|
8
8
|
|
9
|
-
Auto generates an [Apiary Blueprint](http://apiary.io) from the docuementation that is created by your [Grape](https://github.com/
|
10
|
-
|
11
|
-
### NOTE
|
12
|
-
|
13
|
-
This is an early implementation that makes some assumptions about your API (follows a standard REST pattern) that works with our implementation of Grape API's. There is a new an [unreleased feature in Grape](https://github.com/intridea/grape#parameter-documentation) that allows for appending additional documentation. This project is dependent on this feature in order to create example JSON requests and responses.
|
9
|
+
Auto generates an [Apiary Blueprint](http://apiary.io) from the docuementation that is created by your [Grape](https://github.com/ruby-grape/grape) API.
|
14
10
|
|
15
11
|
## Installation
|
16
12
|
|
17
13
|
Add this line to your application's Gemfile:
|
18
14
|
|
19
|
-
gem 'grape'
|
15
|
+
gem 'grape'
|
20
16
|
gem 'grape-apiary'
|
21
17
|
|
22
18
|
And then execute:
|
@@ -27,10 +23,25 @@ Or install it yourself as:
|
|
27
23
|
|
28
24
|
$ gem install grape-apiary
|
29
25
|
|
26
|
+
In order to use the Rake task that can write your generated documents to the filesystem, add this
|
27
|
+
stanza to your `Rakefile`:
|
28
|
+
|
29
|
+
```
|
30
|
+
spec = Gem::Specification.find_by_name 'grape-apiary'
|
31
|
+
load "#{spec.gem_dir}/lib/grape-apiary/tasks/grape-apiary.rake"
|
32
|
+
```
|
33
|
+
|
30
34
|
## Usage
|
31
35
|
|
32
36
|
Add some metadata about your API and then execute the `generate` method on the `GrapeApiary::Blueprint` class.
|
33
37
|
|
38
|
+
If you have installed the Rake task, per above, you can write your
|
39
|
+
documentation to `docs/grape-apiary.md` with this command:
|
40
|
+
|
41
|
+
```
|
42
|
+
API=AwesomeAPI bundle exec rake ga:docs
|
43
|
+
```
|
44
|
+
|
34
45
|
### Configuration
|
35
46
|
|
36
47
|
Configure details about your api in an initializers or similar:
|
@@ -47,6 +58,8 @@ GrapeApiary.config do |config|
|
|
47
58
|
config.example_id_type = :uuid
|
48
59
|
# resources you do not want documented
|
49
60
|
config.resource_exclusion = [:admin, :swagger_doc]
|
61
|
+
# whether or not examples should include a root element (default: false)
|
62
|
+
config.include_root = true
|
50
63
|
end
|
51
64
|
|
52
65
|
# request headers you want documented
|
@@ -71,16 +84,16 @@ GrapeApiary::Blueprint.new(AwesomeAPI).generate
|
|
71
84
|
|
72
85
|
## TODO
|
73
86
|
|
74
|
-
* Add a
|
75
|
-
* Add support for listing all of a resources attributes at the resource level as a markdown table
|
87
|
+
* ~~Add support for listing all of a resources attributes at the resource level as a markdown table~~
|
76
88
|
* Handle ever changing sample id's (don't want git diff's after every generation)
|
77
89
|
* Add option to change or remove the sample id field (eg. `_id` vs `id`)
|
78
90
|
* What if someone does not use JSON?!?
|
79
|
-
*
|
91
|
+
* ~~Create sample response for list endpoints (array)~~
|
92
|
+
* Add an option to include root in json
|
80
93
|
|
81
94
|
## Contributing
|
82
95
|
|
83
|
-
1. Fork it ( http://github.com
|
96
|
+
1. Fork it ( http://github.com/technekes/grape-apiary/fork )
|
84
97
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
98
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
99
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
data/bin/grape-apiary
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler'
|
4
|
+
|
5
|
+
raise "Bundler is required. Please install bundler with 'gem install bundler'" unless defined?(Bundler)
|
6
|
+
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# grape-apiary grape:docs
|
11
|
+
|
12
|
+
# init dependencies
|
13
|
+
Bundler.setup
|
14
|
+
|
15
|
+
# init rake
|
16
|
+
Rake.application.init
|
17
|
+
|
18
|
+
# load the rake tasks
|
19
|
+
gem_dir = File.expand_path('..',File.dirname(__FILE__))
|
20
|
+
load "#{gem_dir}/lib/grape-apiary/tasks/grape-apiary.rake"
|
21
|
+
|
22
|
+
# invoke the given task
|
23
|
+
Rake.application.invoke_task(ARGV[0])
|
data/grape-apiary.gemspec
CHANGED
@@ -8,9 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = GrapeApiary::VERSION
|
9
9
|
spec.authors = ['John Allen']
|
10
10
|
spec.email = ['john@threedogconsulting.com']
|
11
|
-
spec.
|
12
|
-
spec.description = %q{Auto generates an Apiary (http://apiary.io) Blueprint from the docuementation that is created by your Grape API}
|
13
|
-
spec.homepage = 'https://github.com/connexio-labs/grape-apiary'
|
11
|
+
spec.homepage = 'https://github.com/technekes/grape-apiary'
|
14
12
|
spec.license = 'MIT'
|
15
13
|
|
16
14
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -18,13 +16,23 @@ Gem::Specification.new do |spec|
|
|
18
16
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
17
|
spec.require_paths = ['lib']
|
20
18
|
|
21
|
-
spec.
|
19
|
+
spec.summary = <<-SUMMARY
|
20
|
+
Allows for generating an Apiary Blueprint for you Grape API
|
21
|
+
SUMMARY
|
22
|
+
|
23
|
+
spec.description = <<-DESCRIPTION
|
24
|
+
Auto generates an Apiary (http://apiary.io) Blueprint from the
|
25
|
+
docuementation that is created by your Grape API
|
26
|
+
DESCRIPTION
|
27
|
+
|
28
|
+
spec.add_runtime_dependency 'grape', '~> 0.14'
|
29
|
+
spec.add_runtime_dependency 'activesupport'
|
22
30
|
|
23
31
|
spec.add_development_dependency 'coveralls', '~> 0.7'
|
24
32
|
spec.add_development_dependency 'rspec', '~> 2.14'
|
25
33
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
26
34
|
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
35
|
+
spec.add_development_dependency 'rubocop', '~> 0.36'
|
28
36
|
spec.add_development_dependency 'pry', '~> 0.9'
|
29
37
|
spec.add_development_dependency 'guard', '~> 2.4'
|
30
38
|
spec.add_development_dependency 'guard-rspec', '~> 4.2'
|
data/lib/grape-apiary.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
+
require 'active_support/all' # rubocop:disable Style/FileName
|
1
2
|
require 'grape'
|
2
|
-
require 'grape-apiary/version'
|
3
|
-
require 'grape-apiary/config'
|
4
|
-
require 'grape-apiary/parameter'
|
5
|
-
require 'grape-apiary/sample_generator'
|
6
|
-
require 'grape-apiary/route'
|
7
|
-
require 'grape-apiary/resource'
|
8
|
-
require 'grape-apiary/blueprint'
|
9
3
|
|
10
4
|
module GrapeApiary
|
11
|
-
|
5
|
+
autoload :Version, 'grape-apiary/version'
|
6
|
+
autoload :Config, 'grape-apiary/config'
|
7
|
+
autoload :Parameter, 'grape-apiary/parameter'
|
8
|
+
autoload :SampleGenerator, 'grape-apiary/sample_generator'
|
9
|
+
autoload :Route, 'grape-apiary/route'
|
10
|
+
autoload :Resource, 'grape-apiary/resource'
|
11
|
+
autoload :Blueprint, 'grape-apiary/blueprint'
|
12
12
|
|
13
|
-
def config
|
13
|
+
def self.config
|
14
14
|
block_given? ? yield(Config) : Config
|
15
15
|
end
|
16
16
|
end
|
@@ -1,30 +1,23 @@
|
|
1
1
|
module GrapeApiary
|
2
2
|
class Blueprint
|
3
|
-
attr_reader :api_class, :
|
3
|
+
attr_reader :api_class, :blueprint_template, :properties_template
|
4
4
|
|
5
5
|
delegate(*GrapeApiary::Config::SETTINGS, to: 'GrapeApiary::Config')
|
6
6
|
|
7
7
|
def initialize(api_class)
|
8
|
-
@api_class
|
8
|
+
@api_class = api_class
|
9
|
+
@blueprint_template = template_for(:blueprint)
|
10
|
+
@properties_template = template_for(:properties)
|
9
11
|
end
|
10
12
|
|
11
13
|
def generate
|
12
|
-
ERB.new(
|
14
|
+
ERB.new(blueprint_template, nil, '-').result(binding)
|
13
15
|
end
|
14
16
|
|
15
17
|
def write
|
16
18
|
fail 'Not yet supported'
|
17
19
|
end
|
18
20
|
|
19
|
-
def template
|
20
|
-
@template ||= begin
|
21
|
-
directory = File.dirname(File.expand_path(__FILE__))
|
22
|
-
path = File.join(directory, './templates/blueprint.md.erb')
|
23
|
-
|
24
|
-
File.read(path)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
21
|
def routes
|
29
22
|
@routes ||= api_class.routes.map do |route|
|
30
23
|
GrapeApiary::Route.new(route)
|
@@ -33,7 +26,7 @@ module GrapeApiary
|
|
33
26
|
|
34
27
|
def resources
|
35
28
|
@resources ||= begin
|
36
|
-
grouped_routes = routes.group_by(&:route_name).reject do |name,
|
29
|
+
grouped_routes = routes.group_by(&:route_name).reject do |name, _|
|
37
30
|
resource_exclusion.include?(name.to_sym)
|
38
31
|
end
|
39
32
|
|
@@ -41,6 +34,10 @@ module GrapeApiary
|
|
41
34
|
end
|
42
35
|
end
|
43
36
|
|
37
|
+
def properties_table(resource)
|
38
|
+
ERB.new(properties_template, nil, '-').result(resource.resource_binding)
|
39
|
+
end
|
40
|
+
|
44
41
|
def formatted_request_headers
|
45
42
|
formatted_headers(GrapeApiary::Config.request_headers)
|
46
43
|
end
|
@@ -53,12 +50,15 @@ module GrapeApiary
|
|
53
50
|
%w(PUT POST).include?(route.route_method)
|
54
51
|
end
|
55
52
|
|
56
|
-
def routes_binding
|
57
|
-
binding
|
58
|
-
end
|
59
|
-
|
60
53
|
private
|
61
54
|
|
55
|
+
def template_for(name)
|
56
|
+
directory = File.dirname(File.expand_path(__FILE__))
|
57
|
+
path = File.join(directory, "./templates/#{name}.md.erb")
|
58
|
+
|
59
|
+
File.read(path)
|
60
|
+
end
|
61
|
+
|
62
62
|
def formatted_headers(headers)
|
63
63
|
return '' unless headers.present?
|
64
64
|
|
data/lib/grape-apiary/config.rb
CHANGED
@@ -7,8 +7,9 @@ module GrapeApiary
|
|
7
7
|
:request_headers,
|
8
8
|
:response_headers,
|
9
9
|
:example_id_type,
|
10
|
-
:resource_exclusion
|
11
|
-
|
10
|
+
:resource_exclusion,
|
11
|
+
:include_root
|
12
|
+
].freeze
|
12
13
|
|
13
14
|
class << self
|
14
15
|
attr_accessor(*SETTINGS)
|
@@ -25,6 +26,10 @@ module GrapeApiary
|
|
25
26
|
@resource_exclusion ||= []
|
26
27
|
end
|
27
28
|
|
29
|
+
def include_root
|
30
|
+
@include_root ||= false
|
31
|
+
end
|
32
|
+
|
28
33
|
def supported_id_types
|
29
34
|
[:integer, :uuid, :bson]
|
30
35
|
end
|
@@ -1,16 +1,17 @@
|
|
1
1
|
module GrapeApiary
|
2
2
|
class Parameter
|
3
|
-
attr_reader :route, :name, :settings
|
3
|
+
attr_reader :route, :full_name, :name, :settings
|
4
4
|
|
5
5
|
delegate :route_model, :route_namespace, to: :route
|
6
6
|
delegate :requirement, :type, :documentation, :desc, to: :settings
|
7
7
|
delegate :example, to: :documentation, allow_nil: true
|
8
8
|
|
9
9
|
def initialize(route, name, options)
|
10
|
-
@
|
11
|
-
@name
|
12
|
-
@name
|
13
|
-
@
|
10
|
+
@full_name = name
|
11
|
+
@name = name
|
12
|
+
@name = name.scan(/\[(.*)\]/).flatten.first if name.include?('[')
|
13
|
+
@route = route
|
14
|
+
@settings = parse_options(options)
|
14
15
|
end
|
15
16
|
|
16
17
|
def description
|
@@ -20,14 +21,14 @@ module GrapeApiary
|
|
20
21
|
private
|
21
22
|
|
22
23
|
def parse_options(options)
|
23
|
-
options = default_options
|
24
|
+
options = default_options if options.blank?
|
24
25
|
|
25
26
|
options[:requirement] = options[:required] ? 'required' : 'optional'
|
26
27
|
|
27
28
|
Hashie::Mash.new(options)
|
28
29
|
end
|
29
30
|
|
30
|
-
def default_options
|
31
|
+
def default_options
|
31
32
|
model = name.include?('_id') ? name.gsub('_id', '') : route.route_model
|
32
33
|
|
33
34
|
{
|
@@ -20,7 +20,7 @@ module GrapeApiary
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def paths
|
23
|
-
@paths ||= routes.group_by(&:route_path_without_format).map do |
|
23
|
+
@paths ||= routes.group_by(&:route_path_without_format).map do |_, routes|
|
24
24
|
Resource.new(name, routes)
|
25
25
|
end
|
26
26
|
end
|
@@ -36,8 +36,8 @@ module GrapeApiary
|
|
36
36
|
sample_generator.request
|
37
37
|
end
|
38
38
|
|
39
|
-
def sample_response
|
40
|
-
sample_generator.response
|
39
|
+
def sample_response(route)
|
40
|
+
sample_generator.response(route.list?)
|
41
41
|
end
|
42
42
|
|
43
43
|
def unique_params
|
@@ -55,5 +55,9 @@ module GrapeApiary
|
|
55
55
|
[]
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
def resource_binding
|
60
|
+
binding
|
61
|
+
end
|
58
62
|
end
|
59
63
|
end
|
data/lib/grape-apiary/route.rb
CHANGED
@@ -5,7 +5,7 @@ module GrapeApiary
|
|
5
5
|
delegate :route_namespace, :route_path, :route_method, to: '__getobj__'
|
6
6
|
|
7
7
|
def route_params
|
8
|
-
@route_params ||= __getobj__.route_params.map do |param|
|
8
|
+
@route_params ||= __getobj__.route_params.sort.map do |param|
|
9
9
|
Parameter.new(self, *param)
|
10
10
|
end
|
11
11
|
end
|
@@ -41,12 +41,12 @@ module GrapeApiary
|
|
41
41
|
"+ Response #{code} (application/json)"
|
42
42
|
end
|
43
43
|
|
44
|
-
private
|
45
|
-
|
46
44
|
def list?
|
47
|
-
route_method
|
45
|
+
%w(GET POST).include?(route_method) && !route_path.include?(':id')
|
48
46
|
end
|
49
47
|
|
48
|
+
private
|
49
|
+
|
50
50
|
def request_body?
|
51
51
|
!%w(GET DELETE).include?(route_method)
|
52
52
|
end
|
@@ -1,46 +1,60 @@
|
|
1
1
|
module GrapeApiary
|
2
2
|
class SampleGenerator
|
3
|
-
attr_reader :resource
|
3
|
+
attr_reader :resource, :root
|
4
4
|
|
5
5
|
delegate :unique_params, to: :resource
|
6
6
|
|
7
7
|
def initialize(resource)
|
8
8
|
@resource = resource
|
9
|
+
@root = resource.key.singularize
|
9
10
|
end
|
10
11
|
|
11
|
-
def sample
|
12
|
-
|
13
|
-
|
14
|
-
[resource.name, resource.example]
|
15
|
-
end
|
12
|
+
def sample(id = false)
|
13
|
+
array = resource.unique_params.map do |param|
|
14
|
+
next if param.name == root
|
16
15
|
|
17
|
-
|
16
|
+
[param.name, param.example]
|
18
17
|
end
|
18
|
+
|
19
|
+
hash = Hash[array.compact]
|
20
|
+
|
21
|
+
hash = hash.reverse_merge(id: Config.generate_id) if id
|
22
|
+
hash = { root => hash } if Config.include_root
|
23
|
+
|
24
|
+
hash
|
19
25
|
end
|
20
26
|
|
21
27
|
def request
|
22
|
-
|
28
|
+
hash = sample
|
29
|
+
|
30
|
+
return unless hash.present?
|
23
31
|
|
24
32
|
# format json spaces for blueprint markdown
|
25
|
-
JSON
|
33
|
+
JSON
|
34
|
+
.pretty_generate(hash)
|
26
35
|
.gsub('{', (' ' * 14) + '{')
|
27
36
|
.gsub('}', (' ' * 14) + '}')
|
28
37
|
.gsub(/\ {2}\"/, (' ' * 16) + '"')
|
29
38
|
end
|
30
39
|
|
31
|
-
def response
|
32
|
-
return unless sample.present?
|
40
|
+
def response(list = false)
|
41
|
+
return unless (hash = sample(true)).present?
|
33
42
|
|
34
|
-
hash
|
35
|
-
|
43
|
+
pretty_response_for(list ? [hash] : hash)
|
44
|
+
end
|
36
45
|
|
37
|
-
|
38
|
-
|
46
|
+
private
|
47
|
+
|
48
|
+
# rubocop:disable Metrics/AbcSize
|
49
|
+
def pretty_response_for(hash)
|
50
|
+
JSON
|
51
|
+
.pretty_generate(hash)
|
39
52
|
.gsub('[', (' ' * 12) + '[')
|
40
53
|
.gsub(']', (' ' * 12) + ']')
|
41
54
|
.gsub('{', (' ' * 14) + '{')
|
42
55
|
.gsub('}', (' ' * 14) + '}')
|
43
56
|
.gsub(/\ {2}\"/, (' ' * 16) + '"')
|
44
57
|
end
|
58
|
+
# rubocop:enable Metrics/AbcSize
|
45
59
|
end
|
46
60
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
namespace :ga do
|
2
|
+
desc 'Produce documentation'
|
3
|
+
task docs: :environment do
|
4
|
+
api = ENV['API'] || ARGV[1]
|
5
|
+
fail 'You must provide the name of an API to document' if api.nil?
|
6
|
+
begin
|
7
|
+
api_class = Object.const_get(api)
|
8
|
+
api_docs = GrapeApiary::Blueprint.new(api_class).generate
|
9
|
+
output_file = 'docs/grape-apiary.md'
|
10
|
+
File.open(output_file, 'w') { |file| file.write(api_docs) }
|
11
|
+
rescue NameError
|
12
|
+
raise "#{api} has not been defined as a Grape API"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -6,9 +6,10 @@ HOST: <%= host %>
|
|
6
6
|
|
7
7
|
<% resources.each do |resource| %>
|
8
8
|
# Group <%= resource.title %>
|
9
|
+
<%= properties_table(resource) %>
|
9
10
|
<% resource.namespaced.each do |grouped_resource| %>
|
10
11
|
<% grouped_resource.paths.each do |resource_by_path| %>
|
11
|
-
## <%=
|
12
|
+
## <%= resource_by_path.header %>
|
12
13
|
Actions on the <%= resource.name %> resource
|
13
14
|
<% if resource_by_path.routes.first.route_params.present? %>
|
14
15
|
+ Parameters
|
@@ -30,7 +31,7 @@ Actions on the <%= resource.name %> resource
|
|
30
31
|
|
31
32
|
+ Body
|
32
33
|
|
33
|
-
<%= resource.sample_response %>
|
34
|
+
<%= resource.sample_response(route) %>
|
34
35
|
<% end %>
|
35
36
|
<% end %>
|
36
37
|
<% end %>
|
data/lib/grape-apiary/version.rb
CHANGED
@@ -48,6 +48,10 @@ describe GrapeApiary::Blueprint do
|
|
48
48
|
it 'includes groups for each resource' do
|
49
49
|
expect(subject).to include('# Group Widgets')
|
50
50
|
end
|
51
|
+
|
52
|
+
it 'includes properties for the resources' do
|
53
|
+
expect(subject).to include('Properties')
|
54
|
+
end
|
51
55
|
end
|
52
56
|
|
53
57
|
it 'exposes configuration settings' do
|
@@ -23,6 +23,18 @@ describe GrapeApiary::Config do
|
|
23
23
|
expect(subject.description).to eq(description)
|
24
24
|
end
|
25
25
|
|
26
|
+
context '.include_root' do
|
27
|
+
it 'defaults to false' do
|
28
|
+
expect(subject.include_root).to be(false)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'allows for inclusion of the root to be set' do
|
32
|
+
subject.include_root = true
|
33
|
+
|
34
|
+
expect(subject.include_root).to eq(true)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
26
38
|
context 'headers' do
|
27
39
|
[:request_headers, :response_headers].each do |type|
|
28
40
|
context type do
|
@@ -15,7 +15,7 @@ describe GrapeApiary::Resource do
|
|
15
15
|
it 'response generation is delegated to a generator' do
|
16
16
|
expect(subject.sample_generator).to receive(:response)
|
17
17
|
|
18
|
-
subject.sample_response
|
18
|
+
subject.sample_response(GrapeApiary::Route.new(Grape::Route.new))
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/spec/support/sample_api.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class SampleApi < Grape::API
|
2
2
|
resource 'widgets' do
|
3
3
|
desc 'widgets list'
|
4
|
-
get
|
4
|
+
get '/' do
|
5
5
|
end
|
6
6
|
|
7
7
|
desc 'individual widget'
|
@@ -11,13 +11,13 @@ class SampleApi < Grape::API
|
|
11
11
|
desc 'create a widget'
|
12
12
|
params do
|
13
13
|
requires :name,
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
type: 'string',
|
15
|
+
desc: 'the widgets name',
|
16
|
+
documentation: { example: 'super widget' }
|
17
17
|
optional :description,
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
type: 'string',
|
19
|
+
desc: 'the widgets name',
|
20
|
+
documentation: { example: 'the best widget ever made' }
|
21
21
|
end
|
22
22
|
post '/' do
|
23
23
|
end
|
@@ -27,7 +27,7 @@ class SampleApi < Grape::API
|
|
27
27
|
optional :name, type: 'string', desc: 'the widgets name'
|
28
28
|
optional :description, type: 'string', desc: 'the widgets name'
|
29
29
|
end
|
30
|
-
put
|
30
|
+
put ':id' do
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-apiary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Allen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -16,14 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.14'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: coveralls
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,14 +100,14 @@ dependencies:
|
|
86
100
|
requirements:
|
87
101
|
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0.
|
103
|
+
version: '0.36'
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
108
|
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version: '0.
|
110
|
+
version: '0.36'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: pry
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -150,11 +164,13 @@ dependencies:
|
|
150
164
|
- - "~>"
|
151
165
|
- !ruby/object:Gem::Version
|
152
166
|
version: '2.0'
|
153
|
-
description:
|
154
|
-
|
167
|
+
description: |2
|
168
|
+
Auto generates an Apiary (http://apiary.io) Blueprint from the
|
169
|
+
docuementation that is created by your Grape API
|
155
170
|
email:
|
156
171
|
- john@threedogconsulting.com
|
157
|
-
executables:
|
172
|
+
executables:
|
173
|
+
- grape-apiary
|
158
174
|
extensions: []
|
159
175
|
extra_rdoc_files: []
|
160
176
|
files:
|
@@ -162,11 +178,13 @@ files:
|
|
162
178
|
- ".rspec"
|
163
179
|
- ".rubocop.yml"
|
164
180
|
- ".travis.yml"
|
181
|
+
- CHANGELOG.md
|
165
182
|
- Gemfile
|
166
183
|
- Guardfile
|
167
184
|
- LICENSE.txt
|
168
185
|
- README.md
|
169
186
|
- Rakefile
|
187
|
+
- bin/grape-apiary
|
170
188
|
- grape-apiary.gemspec
|
171
189
|
- lib/grape-apiary.rb
|
172
190
|
- lib/grape-apiary/blueprint.rb
|
@@ -175,7 +193,9 @@ files:
|
|
175
193
|
- lib/grape-apiary/resource.rb
|
176
194
|
- lib/grape-apiary/route.rb
|
177
195
|
- lib/grape-apiary/sample_generator.rb
|
196
|
+
- lib/grape-apiary/tasks/grape-apiary.rake
|
178
197
|
- lib/grape-apiary/templates/blueprint.md.erb
|
198
|
+
- lib/grape-apiary/templates/properties.md.erb
|
179
199
|
- lib/grape-apiary/version.rb
|
180
200
|
- lib/grape/apiary.rb
|
181
201
|
- spec/grape-apiary/blueprint_spec.rb
|
@@ -186,7 +206,7 @@ files:
|
|
186
206
|
- spec/spec_helper.rb
|
187
207
|
- spec/support/config_context.rb
|
188
208
|
- spec/support/sample_api.rb
|
189
|
-
homepage: https://github.com/
|
209
|
+
homepage: https://github.com/technekes/grape-apiary
|
190
210
|
licenses:
|
191
211
|
- MIT
|
192
212
|
metadata: {}
|
@@ -206,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
206
226
|
version: '0'
|
207
227
|
requirements: []
|
208
228
|
rubyforge_project:
|
209
|
-
rubygems_version: 2.
|
229
|
+
rubygems_version: 2.5.1
|
210
230
|
signing_key:
|
211
231
|
specification_version: 4
|
212
232
|
summary: Allows for generating an Apiary Blueprint for you Grape API
|
@@ -219,4 +239,3 @@ test_files:
|
|
219
239
|
- spec/spec_helper.rb
|
220
240
|
- spec/support/config_context.rb
|
221
241
|
- spec/support/sample_api.rb
|
222
|
-
has_rdoc:
|