autodoc 0.3.0 → 0.3.1

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: 7205ed893f52f005d0dd0da6f6ce4e4791bdcdc6
4
- data.tar.gz: 8bdb613feb1e8063aa1f859d0d7ab0d09f2229bb
3
+ metadata.gz: ac859dfcddee9d293290317ff4039da04edd3eed
4
+ data.tar.gz: f752329167fe44b3b7f6db9938bf2f80122239a7
5
5
  SHA512:
6
- metadata.gz: 366c405aaf5bd5413ac7bf06683770f345cdd9e5dcceadda5f7ff168c02a474b453019d8d051d8332bb9e3a52bdcbfe922ab8a0fe8e33f7ffba2054454464f39
7
- data.tar.gz: 42a6d8a5717c00d7b3bdb51147d7a3583b33e491cd4d2b46f098b2730d9c5e717bae950e1745c2a552f44d259df882e539325387273df77f222c68ce3819fe38
6
+ metadata.gz: af8b5f8a3d932fc51013b591ed3968648f512e0b4365ef96cfc526b57332ccd3f59d1b7e1dce0e64d6cbb9d3e3cdd87f7fe09fa60a969fa881391913e63b0600
7
+ data.tar.gz: b1f555f70b33527afd368f57ab1127b8ef2a882655240027627643748c1e5837cb3466de5c4960f3533033de42f9241082f7c2e7727a06fc9a1c5f37f32bdeaf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.1
2
+ * prettify JSON request body
3
+ * filter request & response header by Autodoc.configuration.suppress_{request,response}_header
4
+
1
5
  ## 0.3.0
2
6
  * change Autodoc::Document interface & template to mimic HTTP text
3
7
 
data/README.md CHANGED
@@ -49,7 +49,8 @@ end
49
49
  You can configure `Autodoc.configuration` to change its behavior:
50
50
 
51
51
  * path - [String] location to put files (default: ./doc)
52
- * headers - [Array] keys of documented response header (default: ["Location"])
52
+ * suppressed_request_header - [Strings] filtered request header keys
53
+ * suppressed_response_header - [Strings] filtered response header keys
53
54
  * template - [String] ERB template for each document (default: [document.md.erb](https://github.com/r7kamura/autodoc/blob/master/lib/autodoc/templates/document.md.erb))
54
55
  * toc_template - [String] ERB template for ToC (default: [toc.md.erb](https://github.com/r7kamura/autodoc/blob/master/lib/autodoc/templates/toc.md.erb))
55
56
  * toc - [Boolean] whether to generate toc.md (default: false)
@@ -18,8 +18,12 @@ module Autodoc
18
18
  "doc"
19
19
  end
20
20
 
21
- property :headers do
22
- %w[Location]
21
+ property :suppressed_request_header do
22
+ []
23
+ end
24
+
25
+ property :suppressed_response_header do
26
+ []
23
27
  end
24
28
 
25
29
  property :template do
@@ -1,4 +1,5 @@
1
1
  require "action_dispatch/http/request"
2
+ require "active_support/core_ext/hash/slice"
2
3
  require "active_support/core_ext/string/strip"
3
4
  require "uri"
4
5
  require "erb"
@@ -63,7 +64,9 @@ module Autodoc
63
64
  table = request_header_from_fixed_keys
64
65
  table.merge!(request_header_from_http_prefix)
65
66
  table.reject! {|key, value| value.blank? }
66
- table.map {|key, value| [key.split(?_).map(&:downcase).map(&:camelize).join(?-), value].join(": ") }.sort.join("\n")
67
+ table = Hash[table.map {|key, value| [key.split(?_).map(&:downcase).map(&:camelize).join(?-), value] }]
68
+ table.except!(*Autodoc.configuration.suppressed_request_header)
69
+ table.map {|key, value| [key, value].join(": ") }.sort.join("\n")
67
70
  end
68
71
 
69
72
  def request_header_from_http_prefix
@@ -89,11 +92,26 @@ module Autodoc
89
92
  end
90
93
 
91
94
  def request_body_section
92
- "\n\n#{request_body}" unless request_body.empty?
95
+ "\n\n#{request_body}" if request_body.present?
93
96
  end
94
97
 
95
98
  def request_body
96
- request.body.string
99
+ if instance_variable_defined?(:@request_body)
100
+ @request_body
101
+ else
102
+ @request_body = begin
103
+ if request.headers["Content-Type"].try(:include?, "application/json")
104
+ request_body_parsed_as_json
105
+ else
106
+ request.body.string
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ def request_body_parsed_as_json
113
+ JSON.pretty_generate(JSON.parse(request.body.string))
114
+ rescue JSON::ParserError
97
115
  end
98
116
 
99
117
  def response_http_version
@@ -101,7 +119,9 @@ module Autodoc
101
119
  end
102
120
 
103
121
  def response_header
104
- response.headers.map {|key, value| [key, value].join(": ") }.sort.join("\n")
122
+ table = response.headers.clone
123
+ table.except!(*Autodoc.configuration.suppressed_response_header)
124
+ table.map {|key, value| [key, value].join(": ") }.sort.join("\n")
105
125
  end
106
126
 
107
127
  def response_header_from_fixed_keys
@@ -117,12 +137,22 @@ module Autodoc
117
137
  end
118
138
 
119
139
  def response_body
120
- if response.header["Content-Type"].include?("application/json")
121
- JSON.pretty_generate(JSON.parse(response.body))
140
+ if instance_variable_defined?(:@response_body)
141
+ @response_body
122
142
  else
123
- response.body
143
+ @response_body = begin
144
+ if response.header["Content-Type"].try(:include?, "application/json")
145
+ response_body_parsed_as_json
146
+ else
147
+ response.body
148
+ end
149
+ end
124
150
  end
125
- rescue
151
+ end
152
+
153
+ def response_body_parsed_as_json
154
+ JSON.pretty_generate(JSON.parse(response.body))
155
+ rescue JSON::ParserError
126
156
  end
127
157
 
128
158
  def controller
@@ -1,3 +1,3 @@
1
1
  module Autodoc
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -5,7 +5,7 @@ class RecipesController < ApplicationController
5
5
  end
6
6
 
7
7
  def show
8
- respond_with Recipe.find(params[:id])
8
+ render json: Recipe.find(params[:id])
9
9
  end
10
10
 
11
11
  def create
@@ -1,7 +1,7 @@
1
1
  ## GET /entries
2
2
  Returns entries.
3
3
 
4
- ## Example
4
+ ### Example
5
5
  ```
6
6
  GET /entries HTTP/1.1
7
7
  Accept: application/json
@@ -12,4 +12,12 @@ Host: example.org
12
12
  ```
13
13
  HTTP/1.1 200
14
14
  Content-Length: 45
15
+ Content-Type: application/json
16
+
17
+ [
18
+ {
19
+ "title": "Test Title",
20
+ "body": "Lorem Ipsum"
21
+ }
22
+ ]
15
23
  ```
@@ -1,11 +1,11 @@
1
1
  ## GET /recipes/:id
2
2
  Returns the recipe.
3
3
 
4
- ## Example
4
+ ### Example
5
5
  ```
6
6
  GET /recipes/:id HTTP/1.1
7
- Accept: application/json
8
7
  Content-Length: 0
8
+ Content-Type: application/json
9
9
  Host: example.org
10
10
  ```
11
11
 
@@ -14,11 +14,11 @@ HTTP/1.1 200
14
14
  Cache-Control: max-age=0, private, must-revalidate
15
15
  Content-Length: 111
16
16
  Content-Type: application/json; charset=utf-8
17
- ETag: "349ed81d084907a8583a84c1643c4f26"
17
+ ETag: "082bd55a40d73aebcc3f6227e98dcebb"
18
18
  X-Content-Type-Options: nosniff
19
19
  X-Frame-Options: SAMEORIGIN
20
- X-Request-Id: 52ca31ee-4b32-4bdd-ae9e-fed8badaa9b7
21
- X-Runtime: 0.023516
20
+ X-Request-Id: 1ced421d-4c76-44cf-bf5b-241358f73311
21
+ X-Runtime: 0.038226
22
22
  X-UA-Compatible: chrome=1
23
23
  X-XSS-Protection: 1; mode=block
24
24
 
@@ -26,8 +26,8 @@ X-XSS-Protection: 1; mode=block
26
26
  "id": 1,
27
27
  "name": "test",
28
28
  "type": 2,
29
- "created_at": "2013-12-17T08:32:27.415Z",
30
- "updated_at": "2013-12-17T08:32:27.415Z"
29
+ "created_at": "2013-12-17T09:17:20.775Z",
30
+ "updated_at": "2013-12-17T09:17:20.775Z"
31
31
  }
32
32
  ```
33
33
 
@@ -42,15 +42,18 @@ recipe!
42
42
  * `name` string (required, except: `["alice", "bob"]`)
43
43
  * `type` integer (only: `1..3`)
44
44
 
45
- ## Example
45
+ ### Example
46
46
  ```
47
47
  POST /recipes HTTP/1.1
48
48
  Accept: application/json
49
- Content-Length: 16
50
- Content-Type: application/x-www-form-urlencoded
49
+ Content-Length: 24
50
+ Content-Type: application/json
51
51
  Host: www.example.com
52
52
 
53
- name=name&type=1
53
+ {
54
+ "name": "name",
55
+ "type": 1
56
+ }
54
57
  ```
55
58
 
56
59
  ```
@@ -58,12 +61,12 @@ HTTP/1.1 201
58
61
  Cache-Control: max-age=0, private, must-revalidate
59
62
  Content-Length: 111
60
63
  Content-Type: application/json; charset=utf-8
61
- ETag: "fca9b8e000ef821607eda6104ceec86b"
64
+ ETag: "1220735207c1ff5203a3c8975649d37a"
62
65
  Location: http://www.example.com/recipes/1
63
66
  X-Content-Type-Options: nosniff
64
67
  X-Frame-Options: SAMEORIGIN
65
- X-Request-Id: 27e3eef6-2d84-479c-999e-a1142f7ecc74
66
- X-Runtime: 0.005600
68
+ X-Request-Id: b1fded86-bbc2-4442-9fa2-d9536bd55dd4
69
+ X-Runtime: 0.006243
67
70
  X-UA-Compatible: chrome=1
68
71
  X-XSS-Protection: 1; mode=block
69
72
 
@@ -71,7 +74,7 @@ X-XSS-Protection: 1; mode=block
71
74
  "id": 1,
72
75
  "name": "name",
73
76
  "type": 1,
74
- "created_at": "2013-12-17T08:32:27.475Z",
75
- "updated_at": "2013-12-17T08:32:27.475Z"
77
+ "created_at": "2013-12-17T09:17:20.858Z",
78
+ "updated_at": "2013-12-17T09:17:20.858Z"
76
79
  }
77
80
  ```
@@ -15,7 +15,7 @@ describe "Entries" do
15
15
  lambda do |env|
16
16
  [
17
17
  200,
18
- {},
18
+ { "Content-Type" => "application/json" },
19
19
  [
20
20
  [
21
21
  {
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe "Recipes" do
4
4
  let(:env) do
5
- { "HTTP_ACCEPT" => "application/json" }
5
+ { "ACCEPT" => "application/json", "CONTENT_TYPE" => "application/json" }
6
6
  end
7
7
 
8
8
  let(:params) do
@@ -15,6 +15,10 @@ describe "Recipes" do
15
15
  end
16
16
 
17
17
  context "with valid condition (using Rack::Test)", :autodoc do
18
+ before do
19
+ env["Content-Type"] = "application/json"
20
+ end
21
+
18
22
  include Rack::Test::Methods
19
23
 
20
24
  it "returns the recipe" do
@@ -36,7 +40,7 @@ describe "Recipes" do
36
40
  end
37
41
 
38
42
  it "returns 400" do
39
- post "/recipes", params, env
43
+ post "/recipes", params.to_json, env
40
44
  response.status.should == 400
41
45
  end
42
46
  end
@@ -47,7 +51,7 @@ describe "Recipes" do
47
51
  end
48
52
 
49
53
  it "returns 400" do
50
- post "/recipes", params, env
54
+ post "/recipes", params.to_json, env
51
55
  response.status.should == 400
52
56
  end
53
57
  end
@@ -58,7 +62,7 @@ describe "Recipes" do
58
62
  end
59
63
 
60
64
  it "creates a new recipe" do
61
- post "/recipes", params, env
65
+ post "/recipes", params.to_json, env
62
66
  response.status.should == 201
63
67
  end
64
68
  end
@@ -74,7 +78,7 @@ describe "Recipes" do
74
78
  end
75
79
 
76
80
  it "creates a new recipe" do
77
- post "/recipes", params, env
81
+ post "/recipes", params.to_json, env
78
82
  response.status.should == 201
79
83
  end
80
84
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autodoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura