Skip to content

Commit ad47920

Browse files
committedOct 21, 2018
Init Project
0 parents  commit ad47920

9 files changed

+182
-0
lines changed
 

‎.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

‎Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in rspec_api_blueprint.gemspec
4+
gemspec

‎LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2018 Hoa-Nguyen
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Rspec Api Blueprint
2+
3+
Autogeneration of API documentation using the Blueprint format from request specs.
4+
5+
You can find more about Blueprint at http://apiblueprint.org
6+
7+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
gem 'rspec_apib', require: false
12+
13+
Or install it yourself as:
14+
15+
$ gem install rspecb
16+
17+
## Usage
18+
19+
In your spec_helper.rb file add
20+
21+
require 'rspec_api_blueprint'
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

‎Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "bundler/gem_tasks"

‎lib/rspec_apib.rb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require "rspec_apib/version"
2+
require "rspec_apib/string_extensions"
3+
4+
RSpec.configure do |config|
5+
config.before(:suite) do |example|
6+
api_docs_folder_path = File.join(Dir.pwd, '/api_docs/')
7+
Dir.mkdir(api_docs_folder_path) unless Dir.exists?(api_docs_folder_path)
8+
end
9+
10+
config.after(:each, type: :controller) do |example|
11+
response ||= last_response
12+
request ||= last_request
13+
14+
if response
15+
example_group = example.metadata[:example_group]
16+
example_groups = []
17+
18+
while example_group
19+
example_groups << example_group
20+
example_group = example_group[:parent_example_group]
21+
end
22+
23+
action = example_groups[-2][:description_args].first if example_groups[-2]
24+
example_groups[-1][:description_args].first.match(/(\w+)\sRequests/)
25+
26+
file_name = 'api'
27+
file = File.join(Dir.pwd, "/api_docs/#{file_name}.apib")
28+
29+
File.open(file, 'a') do |f|
30+
# Resource & Action
31+
f.write "### #{action}\n\n" if [200, 201].include?(response.status)
32+
33+
# Request
34+
request_body = request.body.read
35+
authorization_header = request.env ? request.env['HTTP_EH_AUTH'] : request.headers['HTTP_EH_AUTH']
36+
37+
if request_body.present? || authorization_header.present?
38+
f.write "+ Request (#{request.content_type})\n\n"
39+
40+
# Request Headers
41+
if authorization_header.present?
42+
f.write "+ Headers\n".indent(2)
43+
f.write "HTTP_EH_AUTH: #{authorization_header}\n".indent(4)
44+
end
45+
46+
f.write "\n"
47+
48+
# Request Body
49+
if request_body.present? && request.content_type == 'application/json'
50+
f.write "+ Body\n".indent(2) if authorization_header
51+
f.write "#{JSON.pretty_generate(JSON.parse(request_body))}\n".indent(4)
52+
f.write "\n"
53+
end
54+
end if [200, 201].include?(response.status)
55+
56+
# Response
57+
f.write "+ Response #{response.status} (#{response.content_type})\n\n"
58+
59+
if response.body.present? && response.content_type =~ /application\/json/
60+
f.write "+ Body\n".indent(2)
61+
f.write "#{JSON.pretty_generate(JSON.parse(response.body))}\n".indent(4)
62+
f.write "\n"
63+
end
64+
end
65+
end
66+
end
67+
end

‎lib/rspec_apib/string_extensions.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
unless "".respond_to?(:indent)
2+
class String
3+
def indent(count, char = ' ')
4+
gsub(/([^\n]*)(\n|$)/) do |match|
5+
last_iteration = ($1 == "" && $2 == "")
6+
line = ""
7+
line << (char * count) unless last_iteration
8+
line << $1
9+
line << $2
10+
line
11+
end
12+
end
13+
end
14+
end

‎lib/rspec_apib/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module RspecApiB
2+
VERSION = "0.0.1"
3+
end

‎rspec_apib.gemspec

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'rspec_apib/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "rspec-apib"
8+
spec.version = RspecApiB::VERSION
9+
spec.authors = ["Hoa Nguyen"]
10+
spec.email = ["nvh0412@gmail.com"]
11+
spec.description = %q{Autogeneration of apib file from rspec}
12+
spec.summary = %q{Autogeneration of apib file from rspec}
13+
spec.homepage = ""
14+
spec.license = "MIT"
15+
16+
spec.files = `git ls-files`.split($/)
17+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19+
spec.require_paths = ["lib"]
20+
21+
spec.add_development_dependency "bundler", "~> 1.3"
22+
spec.add_development_dependency "rake"
23+
24+
spec.add_dependency 'rspec'
25+
end

0 commit comments

Comments
 (0)
Please sign in to comment.