rspec_generate_doc 0.1.3 → 0.2.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: 59e3232e70074556ac288756f0e58e844096a370
4
- data.tar.gz: 2bb90de0ca26f57252a63bff701cef305ea8c9ac
3
+ metadata.gz: 8f1b535b856bbb662c5c00d849b5eea93f7e5b24
4
+ data.tar.gz: b0f41dc6755e8ac3383d01174428de31f48517b1
5
5
  SHA512:
6
- metadata.gz: c9a9b4625df9726ef7bc37b77dfd7d392cbf935f761a66a6dd434a329b378cd91c36d7164bf17b1c494b04044826271394dc75aebf99dc633bb27a5ac6e7a2a0
7
- data.tar.gz: 8516e8c822698f98ec5244fa83074ad4b0e36f5514af0b363001f2e7d6efe908c54ef9ef2d13e08068114b2d1b6fc09c4e8d89d7600ec7963fdcf38e6b1c128e
6
+ metadata.gz: 00523f36f4afa791dcfd10cee715694453d7e13691be071d88e6c921b3eff5c1e8e98c34b93de709873b644be635c408095c0bf8118bcd40cd39f19034d7af8d
7
+ data.tar.gz: caa362b4ea165600531ebfaf3e97dbdcab6ca4a523c2c640a2914207642784a7b9a195523bb8d67c47ae2417108f814dfd11c4b84e62d5ceffb9526f6fb2e176
data/README.md CHANGED
@@ -28,6 +28,11 @@ require 'rspec_generate_doc'
28
28
  RspecGenerateDoc.configure do |config|
29
29
  config.docs_dir = 'path/to/docs/dir' # default: "#{Rails.root}/docs"
30
30
  config.locale = :ru # default: I18n.default_locale
31
+ config.action_decorator = My::Cool::ActionDecorator # default: RspecGenerateDoc::Decorators::Action
32
+ config.parameter_decorator = My::Cool::ParameterDecorator # default: RspecGenerateDoc::Decorators::Parameter
33
+ config.template_file = '/path/to/my/template.api.erb' # default: "#{File.dirname(__FILE__)}/templates/slate.md.erb"
34
+ config.file_prefix = 'prefix_to_file_name_' # default: '_'
35
+ config.file_suffix = '_suffix_to_file_name' # default: ''
31
36
  end
32
37
  ```
33
38
 
@@ -36,7 +41,7 @@ Example use
36
41
  ``` ruby
37
42
  # spec/controllers/user_controller_spec.rb
38
43
  ...
39
- let(:params) do
44
+ let(:api_params) do
40
45
  {
41
46
  id: { description: 'user id', required: true },
42
47
  email: { description: 'user email', required: false }
@@ -1,21 +1,36 @@
1
- require 'fileutils'
1
+ require 'rspec_generate_doc/decorators/action'
2
+ require 'rspec_generate_doc/decorators/parameter'
2
3
 
3
4
  module RspecGenerateDoc
4
5
  class Configuration
6
+ attr_writer :docs_dir, :locale, :action_decorator, :parameter_decorator, :template_file, :file_prefix, :file_suffix
7
+
5
8
  def docs_dir
6
9
  @docs_dir || "#{Rails.root}/docs"
7
10
  end
8
11
 
9
- def docs_dir=(dir)
10
- @docs_dir = dir
11
- end
12
-
13
12
  def locale
14
13
  @locale || I18n.default_locale
15
14
  end
16
15
 
17
- def locale=(locale)
18
- @locale = locale
16
+ def action_decorator
17
+ @action_decorator || RspecGenerateDoc::Decorators::Action
18
+ end
19
+
20
+ def parameter_decorator
21
+ @parameter_decorator || RspecGenerateDoc::Decorators::Parameter
22
+ end
23
+
24
+ def template_file
25
+ @template_file || "#{File.dirname(__FILE__)}/templates/slate.md.erb"
26
+ end
27
+
28
+ def file_prefix
29
+ @file_prefix || '_'
30
+ end
31
+
32
+ def file_suffix
33
+ @file_suffix || ''
19
34
  end
20
35
  end
21
36
  end
@@ -0,0 +1,69 @@
1
+ require 'ostruct'
2
+
3
+ module RspecGenerateDoc
4
+ module Decorators
5
+ class Action
6
+ attr_reader :name, :response, :params, :options
7
+ def initialize(data = {})
8
+ @name = (data[:name] || '').split('#').join(' ')
9
+ @response = data[:response]
10
+ @params = to_params(data[:api_params])
11
+ @options = OpenStruct.new(data[:opntions] || {})
12
+ end
13
+
14
+ def request_method
15
+ @request_method ||= request.request_method
16
+ end
17
+
18
+ def request_fullpath
19
+ @request_fullpath ||= request.original_fullpath.split('?').first
20
+ end
21
+
22
+ def host
23
+ @host ||= request.host
24
+ end
25
+
26
+ def status
27
+ @status ||= response.status
28
+ end
29
+
30
+ def status_message
31
+ @status_message ||= response.status_message
32
+ end
33
+
34
+ def status_with_message
35
+ @status_with_message ||= "#{status} #{status_message}"
36
+ end
37
+
38
+ def content_type
39
+ @content_type ||= response.content_type
40
+ end
41
+
42
+ def content_type?
43
+ content_type.present?
44
+ end
45
+
46
+ def body
47
+ @body ||= json_object.nil? ? response.body.to_s : JSON.pretty_generate(json_object)
48
+ end
49
+
50
+ private
51
+
52
+ def to_params(api_params)
53
+ api_params.map do |name, value|
54
+ hash = value.is_a?(Hash) ? value : {}
55
+ hash[:name] = name unless hash[:name] || hash['name']
56
+ RspecGenerateDoc.configuration.parameter_decorator.new(hash)
57
+ end
58
+ end
59
+
60
+ def json_object
61
+ @json_object ||= JSON.parse(response.body) rescue nil
62
+ end
63
+
64
+ def request
65
+ @request ||= response.request
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,18 @@
1
+ module RspecGenerateDoc
2
+ module Decorators
3
+ class Parameter
4
+ attr_reader :name, :required, :description, :options
5
+
6
+ def initialize(data = {})
7
+ @name = data[:name] || data['name']
8
+ @required = data[:required] || data['required'] || false
9
+ @description = data[:description] || data['description'] || name || ''
10
+ @options = OpenStruct.new(data[:options] || data['options'] || {})
11
+ end
12
+
13
+ def required_human
14
+ description ? I18n.t(:required_yes, scope: :rspec_api_docs) : I18n.t(:required_no, scope: :rspec_api_docs)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,52 @@
1
+ require 'erb'
2
+ require 'fileutils'
3
+
4
+ module RspecGenerateDoc
5
+ class GenarateFIle
6
+ TEMPLATE_EXTNAME = 'erb'.freeze
7
+
8
+ attr_reader :actions, :parent
9
+
10
+ def initialize(data = {})
11
+ @actions = data[:actions]
12
+ @parent = data[:parent]
13
+ I18n.locale = configuration.locale
14
+ end
15
+
16
+ def self.source_root
17
+ configuration.docs_dir
18
+ end
19
+
20
+ def create_file_by_template
21
+ file = File.open(file_path, 'w+')
22
+ file.write ERB.new(File.binread(configuration.template_file), nil, '-').result(binding)
23
+ file.close
24
+ end
25
+
26
+ private
27
+
28
+ def file_path
29
+ "#{dir}/#{configuration.file_prefix}#{parent_name}#{configuration.file_suffix}.#{file_extension}"
30
+ end
31
+
32
+ def parent_name
33
+ @parent_name ||= parent.downcase.split('::').join('_')
34
+ end
35
+
36
+ def template_name
37
+ @template_name ||= configuration.template_file.split('/').last
38
+ end
39
+
40
+ def file_extension
41
+ @file_extension ||= template_name.sub(/#{TEMPLATE_EXTNAME}$/, "").split('.').last
42
+ end
43
+
44
+ def dir
45
+ @dir ||= FileUtils.mkdir_p(configuration.docs_dir).join('')
46
+ end
47
+
48
+ def configuration
49
+ @configuration ||= RspecGenerateDoc.configuration
50
+ end
51
+ end
52
+ end
@@ -1,29 +1,37 @@
1
1
  require 'rspec/rails'
2
- require 'rspec_generate_doc/generate'
2
+ require 'rspec_generate_doc/generate_file'
3
+ require 'rspec_generate_doc/decorators/action'
3
4
 
4
5
  module RspecGenerateDoc
5
6
  module LibraryHooks
6
7
  module Doc
7
8
  ::RSpec.configure do |config|
8
9
  config.before(:context) do
9
- @hash = {}
10
+ @actions = []
11
+ @is_correct_type = self.class.metadata[:type] == :controller
10
12
  end
11
13
 
12
14
  config.after(:each) do
13
- key = self.class.description
15
+ next unless @is_correct_type
16
+ name = self.class.description
14
17
  parent = self.class.parent
15
18
  loop do
16
19
  break if parent.nil? || parent.description == self.class.top_level_description
17
- key = "#{parent.description} #{key}"
20
+ name = "#{parent.description} #{name}"
18
21
  parent = parent.parent
19
22
  end
20
23
 
21
- skip = try(:skip_this) || @skip_this
22
- @hash[key] ||= { response: response, params: (try(:params) || @params || {}) } unless skip
24
+ next if try(:skip_this) || @skip_this
25
+ api_params = try(:api_params) || @api_params || {}
26
+ opntions = try(:api_opntions) || @api_opntions || {}
27
+ @actions << RspecGenerateDoc.configuration.action_decorator
28
+ .new(name: name, response: response, api_params: api_params, options: opntions)
23
29
  end
24
30
 
25
31
  config.after(:context) do
26
- RspecGenerateDoc::Genarate.new(self.class.top_level_description, @hash)
32
+ next unless @is_correct_type
33
+ parent = self.class.top_level_description
34
+ RspecGenerateDoc::GenarateFIle.new(parent: parent, actions: @actions).create_file_by_template
27
35
  end
28
36
  end
29
37
  end
@@ -0,0 +1,24 @@
1
+ # <%= parent %>
2
+
3
+ <% actions.each do |action| -%>
4
+ #<%= action.name %>
5
+
6
+ ```http
7
+ <%= action.request_method %> <%= action.request_fullpath %> HTTP/1.1
8
+ Host: <%= action.host %>
9
+ User-Agent: ExampleClient/1.0.0
10
+ ``
11
+ ```http
12
+ HTTP/1.1 <%= action.status %> <%= action.status_message %>
13
+ <% if action.content_type? -%>
14
+ Content-Type: <%= action.content_type %>
15
+ <% end -%>
16
+ <%= action.body %>
17
+ ```
18
+ <%= I18n.t(:parameter, scope: :rspec_api_docs) %> | <%= I18n.t(:required, scope: :rspec_api_docs) %> | <%= I18n.t(:description, scope: :rspec_api_docs) %>
19
+ -------- | ------- | -------
20
+ <% action.params.each do |parametr| -%>
21
+ <%= parametr.name %> | <%= parametr.required_human %> | <%= parametr.description %>
22
+ <% end -%>
23
+
24
+ <% end -%>
@@ -1,3 +1,3 @@
1
1
  module RspecGenerateDoc
2
- VERSION = '0.1.3'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,8 +1,6 @@
1
1
  require 'rspec/rails'
2
- require 'rspec_generate_doc/version'
3
2
  require 'rspec_generate_doc/configuration'
4
3
  require 'rspec_generate_doc/library_hooks/doc'
5
- require 'rspec_generate_doc/generate'
6
4
 
7
5
  module RspecGenerateDoc
8
6
  extend self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_generate_doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kutyavin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-15 00:00:00.000000000 Z
11
+ date: 2017-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,8 +71,11 @@ files:
71
71
  - config/locales/ru.yml
72
72
  - lib/rspec_generate_doc.rb
73
73
  - lib/rspec_generate_doc/configuration.rb
74
- - lib/rspec_generate_doc/generate.rb
74
+ - lib/rspec_generate_doc/decorators/action.rb
75
+ - lib/rspec_generate_doc/decorators/parameter.rb
76
+ - lib/rspec_generate_doc/generate_file.rb
75
77
  - lib/rspec_generate_doc/library_hooks/doc.rb
78
+ - lib/rspec_generate_doc/templates/slate.md.erb
76
79
  - lib/rspec_generate_doc/version.rb
77
80
  - rspec_generate_doc.gemspec
78
81
  homepage: https://github.com/jokius/rspec_generate_doc
@@ -95,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
98
  version: '0'
96
99
  requirements: []
97
100
  rubyforge_project:
98
- rubygems_version: 2.5.2
101
+ rubygems_version: 2.6.11
99
102
  signing_key:
100
103
  specification_version: 4
101
104
  summary: Generate documentation api of rspec tests
@@ -1,77 +0,0 @@
1
- require 'fileutils'
2
-
3
- module RspecGenerateDoc
4
- class Genarate
5
- attr_reader :parent, :methods_hash
6
-
7
- def initialize(parent, methods_hash)
8
- @parent = parent
9
- @methods_hash = methods_hash
10
- I18n.locale = RspecGenerateDoc.configuration.locale
11
- create_file
12
- end
13
-
14
- private
15
-
16
- def dir
17
- @dir ||= FileUtils.mkdir_p(RspecGenerateDoc.configuration.docs_dir).join('')
18
- end
19
-
20
- def create_file
21
- file = File.open("#{dir}/_#{parent_name}.md", 'w+')
22
- file.write file_text
23
- file.close
24
- end
25
-
26
- def parent_name
27
- @parent_name ||= parent.downcase.split('::').join('_')
28
- end
29
-
30
- def file_text
31
- file_text = "# #{parent}\r\n\r\n"
32
- methods_hash.each do |key, method|
33
- response = method[:response]
34
- request = response.request
35
- file_text += "##{key.split('#').join(' ')}\r\n\r\n"\
36
- "```http\r\n"\
37
- "#{request.request_method} #{request.original_fullpath.split('?').first} HTTP/1.1\r\n"\
38
- "Host: #{response.request.host}\r\n"\
39
- "User-Agent: ExampleClient/1.0.0\r\n"\
40
- "```\r\n\r\n"\
41
- "```http\r\n"\
42
- "HTTP/1.1 #{response.status} #{response.status_message}\r\n"
43
- file_text += "Content-Type: #{response.content_type}\r\n\r\n" if response.content_type.present?
44
- if response.body.present?
45
- object = begin
46
- JSON.parse(response.body)
47
- rescue
48
- nil
49
- end
50
- body = object.nil? ? response.body : JSON.pretty_generate(object)
51
- file_text += "#{body}\r\n" if response.body.present?
52
- end
53
-
54
- file_text += "```\r\n\r\n"\
55
- "#{I18n.t(:parameter, scope: :rspec_api_docs)} | #{I18n.t(:required, scope: :rspec_api_docs)} | #{I18n.t(:description, scope: :rspec_api_docs)}\r\n"\
56
- "-------- | ------- | -------\r\n"
57
- file_text += params_to_text(request, method[:params])
58
- file_text += "\r\n\r\n"
59
- end
60
-
61
- file_text
62
- end
63
-
64
- def params_to_text(request, params)
65
- file_text = ''
66
- request.params.except(*%i(controller action format)).each do |name, _|
67
- hash = params[name.to_sym] || {}
68
- hash = {} unless hash.is_a? Hash
69
- required = hash[:required] ? I18n.t(:required_yes, scope: :rspec_api_docs) : I18n.t(:required_no, scope: :rspec_api_docs)
70
- description = hash[:description] || name
71
- file_text += "#{name} | #{required} | #{description}\r\n"
72
- end
73
-
74
- file_text
75
- end
76
- end
77
- end