autodoc 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 195ad826ae28c1a37c803892d5d9aebc2795e0f8
4
- data.tar.gz: 04dd5257253195ca002a42ad3c739024becacdc6
3
+ metadata.gz: 0989cfe76444a6c585c644685c02a57d28c10386
4
+ data.tar.gz: bc53f5170027552ded6804d7d579ba9623306d38
5
5
  SHA512:
6
- metadata.gz: 2d1a23fb5c9536cd3e474eba5123712a0bdc184292b71f637f74e29e00e830c9215d05090ed6f9641ff219bae68dec9d302408b86988c819f69be6f3efb2e9ad
7
- data.tar.gz: a2c936e794832fb3677e9e6aef952bbe680765ce5aeaaf05c99d7f89848ca946008eacae8ba129b1c2f7057ac10347b55883be264cdf85327c9a40be677adb2b
6
+ metadata.gz: 876a3a38f18353a84e10650afb995186789453e387c730fc270aabb955796a61629d8dd7c3a8abb8d9a0fe49cf0281e3b32298e0d27f11135fa910672ba850c4
7
+ data.tar.gz: edce3d786a7ce4199637d3a9e61038e32c61a0ba8f8fdb6b7a5ae0d319cedde849deffd7594b0737a96f88387f1e922145444fd93a0a983043450888343e9e17
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.2.3
2
+ * clean up dependencies & implementation
3
+
1
4
  ## 0.2.2
2
5
  * fix default documented headers
3
6
 
data/autodoc.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_dependency "actionpack"
20
21
  spec.add_dependency "rspec"
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
22
23
  spec.add_development_dependency "rails", ">= 3.2.11"
data/lib/autodoc.rb CHANGED
@@ -1,14 +1,12 @@
1
- require "rspec"
2
- require "autodoc/collector"
3
1
  require "autodoc/configuration"
4
2
  require "autodoc/document"
5
- require "autodoc/transaction"
6
3
  require "autodoc/version"
4
+ require "autodoc/rspec" if ENV["AUTODOC"]
7
5
 
8
6
  module Autodoc
9
7
  class << self
10
- def collector
11
- @collector ||= Autodoc::Collector.new
8
+ def contexts
9
+ @contexts ||= []
12
10
  end
13
11
 
14
12
  def configuration
@@ -16,25 +14,3 @@ module Autodoc
16
14
  end
17
15
  end
18
16
  end
19
-
20
- if ENV["AUTODOC"]
21
- RSpec.configure do |config|
22
- config.after(:each, type: :request) do
23
- if example.metadata[:autodoc]
24
- txn = Autodoc::Transaction.build(self)
25
- Autodoc.collector.collect(example, txn)
26
- end
27
- end
28
-
29
- config.after(:suite) do
30
- Autodoc.collector.documents.each do |filepath, documents|
31
- filepath = filepath.gsub("./spec/requests/", "").gsub("_spec.rb", ".md")
32
- pathname = Rails.root.join("doc")
33
- pathname += ENV["AUTODOC"] if ENV["AUTODOC"] != "1"
34
- pathname += filepath
35
- pathname.parent.mkpath
36
- pathname.open("w") {|file| file << documents.join("\n").rstrip + "\n" }
37
- end
38
- end
39
- end
40
- end
@@ -1,37 +1,21 @@
1
- # You can configure documented headers.
2
- #
3
- # Examples
4
- #
5
- # # response.headers["X-Content-Type-Options"] will be documented.
6
- # Autodoc.configuration.headers << "X-Content-Type-Options"
7
- #
8
1
  module Autodoc
9
2
  class Configuration
10
- attr_accessor :headers, :template
3
+ attr_writer :headers, :base_path, :template
11
4
 
12
- def initialize
13
- reset
5
+ def headers
6
+ @headers ||= %w[Location]
14
7
  end
15
8
 
16
- def reset
17
- @headers = %w[Location]
18
- @template = <<-EOF.strip_heredoc
19
- <%# coding: UTF-8 -%>
20
- ## <%= method %> <%= path %>
21
- <%= description %>
22
- <%= parameters_section %>
23
- ### request
24
- ```
25
- <%= method %> <%= path %>
26
- ```
27
- <%= request_body_section %>
28
- ### response
29
- ```ruby
30
- Status: <%= response_status %><%= response_headers %>
31
- response: <%= response_body %>
32
- ```
9
+ def base_path
10
+ @base_path ||= begin
11
+ path = Rails.root.join("doc")
12
+ path += ENV["AUTODOC"] if ENV["AUTODOC"] != "1"
13
+ path
14
+ end
15
+ end
33
16
 
34
- EOF
17
+ def template
18
+ @template ||= File.read(File.expand_path("../template.md.erb", __FILE__))
35
19
  end
36
20
  end
37
21
  end
@@ -1,3 +1,4 @@
1
+ require "action_dispatch/http/request"
1
2
  require "erb"
2
3
 
3
4
  module Autodoc
@@ -6,13 +7,10 @@ module Autodoc
6
7
  new(*args).render
7
8
  end
8
9
 
9
- attr_reader :example, :transaction
10
+ attr_reader :example
10
11
 
11
- delegate :method, :request_body, :response_status, :response_header, :response_body_raw, :controller, :action,
12
- to: :transaction
13
-
14
- def initialize(example, txn)
15
- @example, @transaction = example, txn
12
+ def initialize(context)
13
+ @context = context
16
14
  end
17
15
 
18
16
  def render
@@ -21,12 +19,68 @@ module Autodoc
21
19
 
22
20
  private
23
21
 
22
+ def request
23
+ @request ||= begin
24
+ if using_rack_test?
25
+ ActionDispatch::Request.new(@context.last_request.env)
26
+ else
27
+ @context.request
28
+ end
29
+ end
30
+ end
31
+
32
+ def response
33
+ @response ||= begin
34
+ if using_rack_test?
35
+ @context.last_response
36
+ else
37
+ @context.response
38
+ end
39
+ end
40
+ end
41
+
42
+ def method
43
+ request.method
44
+ end
45
+
46
+ def request_body
47
+ request.body.string
48
+ end
49
+
50
+ def response_status
51
+ response.status
52
+ end
53
+
54
+ def response_header(header)
55
+ response.headers[header]
56
+ end
57
+
58
+ def response_body_raw
59
+ response.body
60
+ end
61
+
62
+ def controller
63
+ request.params[:controller]
64
+ end
65
+
66
+ def action
67
+ request.params[:action]
68
+ end
69
+
70
+ def using_rack_test?
71
+ !!defined?(Rack::Test::Methods) && @context.class.ancestors.include?(Rack::Test::Methods)
72
+ end
73
+
74
+ def transaction
75
+ @transaction ||= Autodoc::Transaction.build(@context)
76
+ end
77
+
24
78
  def description
25
- "#{example.description.capitalize}."
79
+ "#{@context.example.description.capitalize}."
26
80
  end
27
81
 
28
82
  def path
29
- example.full_description[%r<(GET|POST|PUT|DELETE) ([^ ]+)>, 2]
83
+ @context.example.full_description[%r<(GET|POST|PUT|DELETE) ([^ ]+)>, 2]
30
84
  end
31
85
 
32
86
  def response_body
@@ -0,0 +1,16 @@
1
+ require "rspec"
2
+
3
+ RSpec.configuration.after(:each, autodoc: true) do
4
+ Autodoc.contexts << self.clone
5
+ end
6
+
7
+ RSpec.configuration.after(:suite) do
8
+ table = Autodoc.contexts.group_by {|context| context.example.file_path }
9
+ table.each do |path, contexts|
10
+ pathname = Autodoc.configuration.base_path + path.gsub("./spec/requests/", "").gsub("_spec.rb", ".md")
11
+ pathname.parent.mkpath
12
+ pathname.open("w") do |file|
13
+ file << contexts.map {|context| Autodoc::Document.render(context) }.join("\n").rstrip + "\n"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ <%# coding: UTF-8 -%>
2
+ ## <%= method %> <%= path %>
3
+ <%= description %>
4
+ <%= parameters_section %>
5
+ ### request
6
+ ```
7
+ <%= method %> <%= path %>
8
+ ```
9
+ <%= request_body_section %>
10
+ ### response
11
+ ```ruby
12
+ Status: <%= response_status %><%= response_headers %>
13
+ response: <%= response_body %>
14
+ ```
@@ -1,3 +1,3 @@
1
1
  module Autodoc
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -17,13 +17,14 @@ name=name&type=1
17
17
  ### response
18
18
  ```ruby
19
19
  Status: 201
20
+ Location: http://www.example.com/recipes/1
20
21
  response:
21
22
  {
22
23
  "id": 1,
23
24
  "name": "name",
24
25
  "type": 1,
25
- "created_at": "2013-10-22T22:41:07.886Z",
26
- "updated_at": "2013-10-22T22:41:07.886Z"
26
+ "created_at": "2013-11-30T13:59:21.061Z",
27
+ "updated_at": "2013-11-30T13:59:21.061Z"
27
28
  }
28
29
  ```
29
30
 
@@ -47,13 +48,13 @@ name=name&type=1
47
48
  ### response
48
49
  ```ruby
49
50
  Status: 201
50
- location: http://example.org/recipes/1
51
+ Location: http://example.org/recipes/1
51
52
  response:
52
53
  {
53
54
  "id": 1,
54
55
  "name": "name",
55
56
  "type": 1,
56
- "created_at": "2013-10-22T22:41:07.900Z",
57
- "updated_at": "2013-10-22T22:41:07.900Z"
57
+ "created_at": "2013-11-30T13:59:21.072Z",
58
+ "updated_at": "2013-11-30T13:59:21.072Z"
58
59
  }
59
60
  ```
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autodoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-29 00:00:00.000000000 Z
11
+ date: 2013-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -95,12 +109,11 @@ files:
95
109
  - Rakefile
96
110
  - autodoc.gemspec
97
111
  - lib/autodoc.rb
98
- - lib/autodoc/collector.rb
99
112
  - lib/autodoc/configuration.rb
100
113
  - lib/autodoc/document.rb
101
- - lib/autodoc/transaction.rb
114
+ - lib/autodoc/rspec.rb
115
+ - lib/autodoc/template.md.erb
102
116
  - lib/autodoc/version.rb
103
- - spec/autodoc/document_spec.rb
104
117
  - spec/dummy/Rakefile
105
118
  - spec/dummy/app/assets/javascripts/application.js
106
119
  - spec/dummy/app/assets/stylesheets/application.css
@@ -164,7 +177,6 @@ signing_key:
164
177
  specification_version: 4
165
178
  summary: Auto-generate JSON API documents from your request-specs.
166
179
  test_files:
167
- - spec/autodoc/document_spec.rb
168
180
  - spec/dummy/Rakefile
169
181
  - spec/dummy/app/assets/javascripts/application.js
170
182
  - spec/dummy/app/assets/stylesheets/application.css
@@ -203,3 +215,4 @@ test_files:
203
215
  - spec/dummy/script/rails
204
216
  - spec/requests/recipes_spec.rb
205
217
  - spec/spec_helper.rb
218
+ has_rdoc:
@@ -1,11 +0,0 @@
1
- module Autodoc
2
- class Collector
3
- def collect(example, txn)
4
- documents[example.file_path] << Autodoc::Document.render(example, txn)
5
- end
6
-
7
- def documents
8
- @documents ||= Hash.new {|hash, key| hash[key] = [] }
9
- end
10
- end
11
- end
@@ -1,45 +0,0 @@
1
- module Autodoc
2
- class Transaction
3
- def self.build(context)
4
- if defined?(Rack::Test::Methods) && context.class.ancestors.include?(Rack::Test::Methods)
5
- self.new(ActionDispatch::Request.new(context.last_request.env), context.last_response)
6
- else
7
- self.new(context.request, context.response)
8
- end
9
- end
10
-
11
- attr_accessor :request, :response
12
-
13
- def initialize(*args)
14
- @request, @response = *args
15
- end
16
-
17
- def method
18
- request.method
19
- end
20
-
21
- def request_body
22
- request.body.string
23
- end
24
-
25
- def response_status
26
- response.status
27
- end
28
-
29
- def response_header(header)
30
- response.headers[header]
31
- end
32
-
33
- def response_body_raw
34
- response.body
35
- end
36
-
37
- def controller
38
- request.params[:controller]
39
- end
40
-
41
- def action
42
- request.params[:action]
43
- end
44
- end
45
- end
@@ -1,124 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Autodoc::Document do
4
- describe ".render" do
5
- subject do
6
- described_class.render(example, transaction)
7
- end
8
-
9
- let(:example) do
10
- double(
11
- description: description,
12
- full_description: "#{method} #{path} #{description}",
13
- )
14
- end
15
-
16
- let(:description) do
17
- "returns a recipe"
18
- end
19
-
20
- let(:request) do
21
- double(
22
- body: StringIO.new,
23
- params: {},
24
- method: method,
25
- path: path,
26
- )
27
- end
28
-
29
- let(:response) do
30
- double(
31
- body: response_body,
32
- headers: {},
33
- status: 200,
34
- )
35
- end
36
-
37
- let(:transaction) do
38
- Autodoc::Transaction.new(request, response)
39
- end
40
-
41
- let(:method) do
42
- "GET"
43
- end
44
-
45
- let(:path) do
46
- "/recipes/1"
47
- end
48
-
49
- let(:response_body) do
50
- { name: "recipe 1" }.to_json
51
- end
52
-
53
- context "without Autodoc.configuration.template" do
54
- it "uses default template" do
55
- should == <<-EOF.strip_heredoc
56
- ## GET /recipes/1
57
- Returns a recipe.
58
-
59
- ### request
60
- ```
61
- GET /recipes/1
62
- ```
63
-
64
- ### response
65
- ```ruby
66
- Status: 200
67
- response:
68
- {
69
- "name": "recipe 1"
70
- }
71
- ```
72
-
73
- EOF
74
- end
75
- end
76
-
77
- context "with Autodoc.configuration.template" do
78
- before do
79
- Autodoc.configuration.template = <<-EOF.strip_heredoc
80
- ### request
81
- ```
82
- <%= method %> <%= path %>
83
- ```
84
- <%= request_body_section %>
85
- ### response
86
- ```ruby
87
- Status: <%= response_status %><%= response_headers %>
88
- response: <%= response_body %>
89
- ```
90
-
91
- ## <%= method %> <%= path %>
92
- <%= description %>
93
- <%= parameters_section %>
94
- EOF
95
- end
96
-
97
- after do
98
- Autodoc.configuration.reset
99
- end
100
-
101
- it "uses custom template" do
102
- should == <<-EOF.strip_heredoc
103
- ### request
104
- ```
105
- GET /recipes/1
106
- ```
107
-
108
- ### response
109
- ```ruby
110
- Status: 200
111
- response:
112
- {
113
- "name": "recipe 1"
114
- }
115
- ```
116
-
117
- ## GET /recipes/1
118
- Returns a recipe.
119
-
120
- EOF
121
- end
122
- end
123
- end
124
- end