rspec-api-blueprint-formatter 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b6dfeb563d5f0d7faae44cb720d625abfa25d91e
4
+ data.tar.gz: b9ffaabf36aeae46b20de087661323963c692ae0
5
+ SHA512:
6
+ metadata.gz: 1f96c797447ae5f70ef8999a630c9e61f4dd0fb6f8911c85927340c3b7fe987a65709739e6ce696740d1e3ada62147fb7aa8a6da981b4d6b402a513db3d002b4
7
+ data.tar.gz: 26d68c0902c8a2c74cc78f88636d777ec76f68c3dfc3100b72f9954efc47dedba1f728d8b1ca05982eeeea2045a2a0086b521b25892e9759d9cf1dc0920bdd5d
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "api_blueprint_formatter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,130 @@
1
+ require 'rspec'
2
+ require 'rspec/core/formatters/base_text_formatter'
3
+
4
+ class ApiBlueprint < RSpec::Core::Formatters::BaseTextFormatter
5
+ VERSION = "0.1.0"
6
+ RSpec::Core::Formatters.register self, :example_passed, :example_started, :stop
7
+
8
+ def initialize(output)
9
+ super
10
+ @passed_examples = {}
11
+ @group_level = 0
12
+ end
13
+
14
+ def example_started(notification)
15
+ @example_group_instance = notification.example.example_group_instance
16
+ end
17
+
18
+ def example_passed(passed)
19
+ metadata = passed.example.metadata
20
+
21
+ if metadata[:apidoc] &&
22
+ metadata[:resource_group] &&
23
+ metadata[:resource] &&
24
+ metadata[:action] &&
25
+ metadata[:action_description]
26
+
27
+ request = @example_group_instance.request
28
+ response = @example_group_instance.response
29
+ description = description_array_from(passed.example.metadata).reverse.join(' ').gsub(/[\(\)]/, '..')
30
+
31
+ @passed_examples.deep_merge!({
32
+ metadata[:resource_group] => {
33
+ metadata[:resource] => {
34
+ metadata[:action] => {
35
+ description: metadata[:action_description],
36
+ examples: {
37
+ description => {
38
+ request: {
39
+ parameters: request.parameters.except(*request.path_parameters.keys.map(&:to_s)).to_json,
40
+ format: request.format
41
+ },
42
+ source: passed.example.instance_variable_get(:@example_block).source,
43
+ location: metadata[:location],
44
+ response: {
45
+ status: response.status,
46
+ body: response.body
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ })
54
+ end
55
+ @example_group_instance = nil
56
+ end
57
+
58
+ def stop(notification)
59
+ @passed_examples.sort_by { |k,v| k }.each do |resource_group_name, resource_group_resources|
60
+ print_resource_group(resource_group_name, resource_group_resources)
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def print_resource_group(resource_group_name, resource_group_resources)
67
+ output.puts "# Group #{resource_group_name}"
68
+ resource_group_resources.each &method(:print_resource)
69
+ end
70
+
71
+ def print_resource(resource_name, actions)
72
+ unless resource_name =~ /^[^\[\]]*\[\/[^\]]+\]/
73
+ raise "resoure: '#{resource_name}' is invalid. :resource needs to be specified according to https://github.com/apiaryio/api-blueprint/blob/master/API%20Blueprint%20Specification.md#resource-section"
74
+ end
75
+ output.puts "# #{resource_name}"
76
+
77
+ http_verbs = actions.keys.map {|action| action.scan(/\[([A-Z]+)\]/).flatten[0] }
78
+
79
+ unless http_verbs.length == http_verbs.uniq.length
80
+ raise "Action HTTP verbs are not unique #{actions.keys.inspect} for resource: '#{resource_name}'"
81
+ end
82
+
83
+ actions.each &method(:print_action)
84
+ end
85
+
86
+ def print_action(action_name, action_meta_data)
87
+ output.puts "## #{action_name}\n" \
88
+ "\n" \
89
+ "#{action_meta_data[:description]}\n" \
90
+ "\n" \
91
+
92
+ action_meta_data[:examples].each &method(:print_example)
93
+ end
94
+
95
+ def print_example(example_description, example_metadata)
96
+ output.puts "+ Request #{example_description}\n" \
97
+ "\n" \
98
+ " #{example_metadata[:request][:parameters]}\n" \
99
+ " \n" \
100
+ " Location: #{example_metadata[:location]}\n" \
101
+ " Source code:\n" \
102
+ " \n" \
103
+ "#{indent_lines(8, example_metadata[:source])}\n" \
104
+ "\n"
105
+
106
+ output.puts "+ Response #{example_metadata[:response][:status]} (#{example_metadata[:request][:format]})\n" \
107
+ "\n" \
108
+ " #{example_metadata[:response][:body]}\n" \
109
+ "\n"
110
+ end
111
+
112
+ # To include the descriptions of all the contexts that are below the action
113
+ # group, but not including resource/resource_group descriptions
114
+ def description_array_from(example_metadata)
115
+ parent = example_metadata[:parent_example_group] if example_metadata.key?(:parent_example_group)
116
+ parent ||= example_metadata[:example_group] if example_metadata.key?(:example_group)
117
+ if parent[:action].nil?
118
+ []
119
+ else
120
+ [example_metadata[:description]] + description_array_from(parent)
121
+ end
122
+ end
123
+
124
+ def indent_lines(number_of_spaces, string)
125
+ string
126
+ .split("\n")
127
+ .map { |a| a.prepend(' ' * number_of_spaces) }
128
+ .join("\n")
129
+ end
130
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiBlueprint do
4
+ it 'has a version number' do
5
+ expect(ApiBlueprint::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'api_blueprint'
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-api-blueprint-formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nam Chu Hoai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ description: Use your Rspec tests to build your API documentation
56
+ email:
57
+ - nambrot@googlemail.com
58
+ executables:
59
+ - console
60
+ - setup
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - bin/console
65
+ - bin/setup
66
+ - lib/api_blueprint.rb
67
+ - spec/rspec/api/blueprint/formatter_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: https://github.com/nambrot/rspec-api-blueprint-formatter
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ allowed_push_host: https://rubygems.org
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Use your Rspec tests to build your API documentation
94
+ test_files:
95
+ - spec/rspec/api/blueprint/formatter_spec.rb
96
+ - spec/spec_helper.rb