jdoc 0.2.1 → 0.3.0

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: 3037f3d705868d91ab1a9760532a0c94b40443a9
4
- data.tar.gz: 3902451a12fa080192ad162f505937ccaaade5ec
3
+ metadata.gz: 882fb1ca942392f3f55bac3f59a40cb5b5fe6305
4
+ data.tar.gz: 273cefe1904b0e628430ff94716ccd8a7642ff0b
5
5
  SHA512:
6
- metadata.gz: b013094bfbc4221fd7eb72d5ff4ef01ab6015dd08bd026702b3a32024dc65de55df2076cbb32854d7eace2958acc4538d9134b7286f4bf726a52bc1bb4cfcb9a
7
- data.tar.gz: ea026e0adb173194b64df59fbfb60a47db6483f47f3c4a60f434192f821dc4611b90a63bac0ab1ef4221b614478eb610d1f394fafb2e6110dda30f7c1888042e
6
+ metadata.gz: 1c3bf5c1527a6a9e9c88e2fd16cc0c9c6c1d6504c920fb6346d783489b1ee89750e77ec8168ba93a9c38a74f87a9dcca5de7d00f3e347127bc1312c5d51e583a
7
+ data.tar.gz: c377a2e9e92a1252d338b671161c7cb8be598dd849920972262e6dea7685470db14e0cbf3478e90353c92699996f01c28dfa7a89d8cd34cf52b03c2d1362bea7
@@ -1,3 +1,7 @@
1
+ ## 0.3.0
2
+ * Change example response status and body to PUT & DELETE requests
3
+ * Improve request path example by resolving variable in path
4
+
1
5
  ## 0.2.1
2
6
  * Support multipart/form-data
3
7
 
@@ -56,7 +56,6 @@ Host: api.example.com
56
56
  ```
57
57
  HTTP/1.1 201
58
58
  Content-Type: application/json
59
-
60
59
  {
61
60
  "id": "01234567-89ab-cdef-0123-456789abcdef",
62
61
  "name": "example",
@@ -77,42 +76,25 @@ Content-Type: application/json
77
76
  Delete an existing app.
78
77
 
79
78
  ```
80
- DELETE /apps/:id HTTP/1.1
79
+ DELETE /apps/01234567-89ab-cdef-0123-456789abcdef HTTP/1.1
81
80
  Host: api.example.com
82
81
  ```
83
82
 
84
83
  ```
85
- HTTP/1.1 200
86
- Content-Type: application/json
87
-
88
- {
89
- "id": "01234567-89ab-cdef-0123-456789abcdef",
90
- "name": "example",
91
- "private": false,
92
- "deleted_at": null,
93
- "user_ids": [
94
- 1
95
- ],
96
- "users": [
97
- {
98
- "name": "alice"
99
- }
100
- ]
101
- }
84
+ HTTP/1.1 204
102
85
  ```
103
86
 
104
87
  ### GET /apps/:id
105
88
  Info for existing app.
106
89
 
107
90
  ```
108
- GET /apps/:id HTTP/1.1
91
+ GET /apps/01234567-89ab-cdef-0123-456789abcdef HTTP/1.1
109
92
  Host: api.example.com
110
93
  ```
111
94
 
112
95
  ```
113
96
  HTTP/1.1 200
114
97
  Content-Type: application/json
115
-
116
98
  {
117
99
  "id": "01234567-89ab-cdef-0123-456789abcdef",
118
100
  "name": "example",
@@ -140,7 +122,6 @@ Host: api.example.com
140
122
  ```
141
123
  HTTP/1.1 200
142
124
  Content-Type: application/json
143
-
144
125
  [
145
126
  {
146
127
  "id": "01234567-89ab-cdef-0123-456789abcdef",
@@ -163,7 +144,7 @@ Content-Type: application/json
163
144
  Update an existing app.
164
145
 
165
146
  ```
166
- PATCH /apps/:id HTTP/1.1
147
+ PATCH /apps/01234567-89ab-cdef-0123-456789abcdef HTTP/1.1
167
148
  Content-Type: application/json
168
149
  Host: api.example.com
169
150
 
@@ -175,7 +156,6 @@ Host: api.example.com
175
156
  ```
176
157
  HTTP/1.1 200
177
158
  Content-Type: application/json
178
-
179
159
  {
180
160
  "id": "01234567-89ab-cdef-0123-456789abcdef",
181
161
  "name": "example",
@@ -196,7 +176,7 @@ Content-Type: application/json
196
176
  Upload an attachment file for an app
197
177
 
198
178
  ```
199
- POST /apps/:id/files HTTP/1.1
179
+ POST /apps/01234567-89ab-cdef-0123-456789abcdef/files HTTP/1.1
200
180
  Content-Type: multipart/form-data; boundary=---BoundaryX
201
181
  Host: api.example.com
202
182
 
@@ -210,7 +190,6 @@ Content-Disposition: form-data; name="[file]"
210
190
  ```
211
191
  HTTP/1.1 201
212
192
  Content-Type: application/json
213
-
214
193
  {
215
194
  "id": "01234567-89ab-cdef-0123-456789abcdef",
216
195
  "name": "example",
@@ -247,7 +226,6 @@ Host: api.example.com
247
226
  ```
248
227
  HTTP/1.1 200
249
228
  Content-Type: application/json
250
-
251
229
  [
252
230
  {
253
231
  "name": "Sushi",
@@ -57,11 +57,20 @@ module Jdoc
57
57
  # @example
58
58
  # link.path #=> "GET /apps/:id"
59
59
  def path
60
- @path ||= @raw_link.href.gsub(/{(.+?)}/) do |matched|
60
+ @path ||= @raw_link.href.gsub(/{(.+?)}/) do
61
61
  ":" + CGI.unescape($1).gsub(/[()\s]/, "").split("/").last
62
62
  end
63
63
  end
64
64
 
65
+ # @returns [String] Path with embedded example variable
66
+ # @example
67
+ # link.example_path #=> "GET /apps/1"
68
+ def example_path
69
+ @example_path ||= @raw_link.href.gsub(/{\((.+?)\)}/) do
70
+ JsonPointer.evaluate(root_schema.data, CGI.unescape($1))["example"]
71
+ end
72
+ end
73
+
65
74
  # @return [String] request content type
66
75
  # @note default value is "application/json"
67
76
  def content_type
@@ -119,6 +128,12 @@ module Jdoc
119
128
  ["PATCH", "POST", "PUT"].include?(method)
120
129
  end
121
130
 
131
+ # We have a policy that we should not return response body to PUT and DELETE requests.
132
+ # @return [true, false] True if this endpoint must have response body
133
+ def has_response_body?
134
+ !["PUT", "DELETE"].include?(method)
135
+ end
136
+
122
137
  # @return [String] JSON response body generated from example properties
123
138
  def response_body
124
139
  object = has_list_data? ? [response_hash] : response_hash
@@ -127,7 +142,14 @@ module Jdoc
127
142
 
128
143
  # @return [Fixnum] Preferred respone status code for this endpoint
129
144
  def response_status
130
- method == "POST" ? 201 : 200
145
+ case method
146
+ when "POST"
147
+ 201
148
+ when "PUT", "DELETE"
149
+ 204
150
+ else
151
+ 200
152
+ end
131
153
  end
132
154
 
133
155
  # @return [JsonSchema::Schema] Response schema for this link
@@ -148,6 +170,17 @@ module Jdoc
148
170
 
149
171
  private
150
172
 
173
+ # @return [JsonSchema::Schema] Root schema object this link is associated to
174
+ def root_schema
175
+ @root ||= begin
176
+ schema = @raw_link
177
+ while schema.parent
178
+ schema = schema.parent
179
+ end
180
+ schema
181
+ end
182
+ end
183
+
151
184
  # @return [true, false] True if a given link has a schema property
152
185
  def has_schema_in_link?
153
186
  !!@raw_link.schema
@@ -1,3 +1,3 @@
1
1
  module Jdoc
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -27,7 +27,7 @@
27
27
  <%= link.description %>
28
28
 
29
29
  ```
30
- <%= link.method %> <%= link.path %><%= link.query_string %> HTTP/1.1
30
+ <%= link.method %> <%= link.example_path %><%= link.query_string %> HTTP/1.1
31
31
  <%= "Content-Type: #{link.content_type}\n" if link.has_request_body? -%>
32
32
  Host: <%= schema.host_with_port %>
33
33
  <%= "\n" + link.request_body if link.has_request_body? -%>
@@ -35,9 +35,8 @@ Host: <%= schema.host_with_port %>
35
35
 
36
36
  ```
37
37
  HTTP/1.1 <%= link.response_status %>
38
- Content-Type: application/json
39
-
40
- <%= link.response_body %>
38
+ <%= "Content-Type: application/json" if link.has_response_body? -%>
39
+ <%= "\n#{link.response_body}\n" if link.has_response_body? -%>
41
40
  ```
42
41
 
43
42
  <% end %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jdoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-04 00:00:00.000000000 Z
11
+ date: 2014-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport