jdoc 0.2.0 → 0.2.1

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: c55cb2cd6e9fb7c413955cb27466ea7166210666
4
- data.tar.gz: 4dd21023b7af214e1eb448d3bd3410e00e4a0e36
3
+ metadata.gz: 3037f3d705868d91ab1a9760532a0c94b40443a9
4
+ data.tar.gz: 3902451a12fa080192ad162f505937ccaaade5ec
5
5
  SHA512:
6
- metadata.gz: cd723d33c3bfeff779db5a8725829c4895d38a63c7ea6ee787a6c31d6879e95c0e41577975115f2c8688f874325b9f6d353777e43451d6375e275f369654f0cb
7
- data.tar.gz: daba310565e86e8d309582676311b396a76bccbf48b9e4c932b0d616107db36c2d92a66ce62c26df4f1ad4bd00726f526f8fcba9ce7f188c8a07739fb568f1b6
6
+ metadata.gz: b013094bfbc4221fd7eb72d5ff4ef01ab6015dd08bd026702b3a32024dc65de55df2076cbb32854d7eace2958acc4538d9134b7286f4bf726a52bc1bb4cfcb9a
7
+ data.tar.gz: ea026e0adb173194b64df59fbfb60a47db6483f47f3c4a60f434192f821dc4611b90a63bac0ab1ef4221b614478eb610d1f394fafb2e6110dda30f7c1888042e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.2.1
2
+ * Support multipart/form-data
3
+
1
4
  ## 0.2.0
2
5
  * Show schema description on docs if exists
3
6
 
@@ -1,11 +1,13 @@
1
1
  # Example API
2
2
  A schema for a small example API.
3
+
3
4
  * [App](#app)
4
5
  * [POST /apps](#post-apps)
5
6
  * [DELETE /apps/:id](#delete-appsid)
6
7
  * [GET /apps/:id](#get-appsid)
7
8
  * [GET /apps](#get-apps)
8
9
  * [PATCH /apps/:id](#patch-appsid)
10
+ * [POST /apps/:id/files](#post-appsidfiles)
9
11
  * [Recipe](#recipe)
10
12
  * [GET /recipes](#get-recipes)
11
13
  * [User](#user)
@@ -190,6 +192,41 @@ Content-Type: application/json
190
192
  }
191
193
  ```
192
194
 
195
+ ### POST /apps/:id/files
196
+ Upload an attachment file for an app
197
+
198
+ ```
199
+ POST /apps/:id/files HTTP/1.1
200
+ Content-Type: multipart/form-data; boundary=---BoundaryX
201
+ Host: api.example.com
202
+
203
+ -----BoundaryX
204
+ Content-Disposition: form-data; name="[file]"
205
+
206
+ ... contents of file ...
207
+
193
- ----BoundaryX--
208
+ ```
209
+
210
+ ```
211
+ HTTP/1.1 201
212
+ Content-Type: application/json
213
+
214
+ {
215
+ "id": "01234567-89ab-cdef-0123-456789abcdef",
216
+ "name": "example",
217
+ "private": false,
218
+ "deleted_at": null,
219
+ "user_ids": [
220
+ 1
221
+ ],
222
+ "users": [
223
+ {
224
+ "name": "alice"
225
+ }
226
+ ]
227
+ }
228
+ ```
229
+
194
230
  ## Recipe
195
231
 
196
232
 
data/jdoc.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.add_dependency "activesupport"
20
20
  spec.add_dependency "erubis"
21
21
  spec.add_dependency "json_schema"
22
+ spec.add_dependency "rack"
22
23
  spec.add_dependency "redcarpet"
23
24
  spec.add_development_dependency "bundler", "~> 1.6"
24
25
  spec.add_development_dependency "pry"
data/lib/jdoc.rb CHANGED
@@ -4,10 +4,12 @@ require "cgi"
4
4
  require "erubis"
5
5
  require "json_schema"
6
6
  require "redcarpet"
7
+ require "rack"
7
8
  require "uri"
8
9
 
9
10
  require "jdoc/generator"
10
11
  require "jdoc/link"
12
+ require 'jdoc/request/multipart'
11
13
  require "jdoc/property"
12
14
  require "jdoc/resource"
13
15
  require "jdoc/schema"
data/lib/jdoc/link.rb CHANGED
@@ -62,6 +62,14 @@ module Jdoc
62
62
  end
63
63
  end
64
64
 
65
+ # @return [String] request content type
66
+ # @note default value is "application/json"
67
+ def content_type
68
+ type = @raw_link.enc_type
69
+ type += "; #{Request::Multipart.boundary}" if content_type_multipart?
70
+ type
71
+ end
72
+
65
73
  # Adds query string if a link has a schema property and method is GET
66
74
  # @return [String, nil] A query string prefixed with `?` only to GET request
67
75
  # @example
@@ -74,7 +82,25 @@ module Jdoc
74
82
 
75
83
  # @return [String, nil] Example request body in JSON format
76
84
  def request_body
77
- JSON.pretty_generate(request_parameters) + "\n"
85
+ body = case
86
+ when content_type_multipart?
87
+ request_body_in_multipart
88
+ when content_type_json?
89
+ request_body_in_json
90
+ else
91
+ ""
92
+ end
93
+ body + "\n"
94
+ end
95
+
96
+ # @return [true, false] True if encType of request is multipart/form-data
97
+ def content_type_multipart?
98
+ Rack::Mime.match?(@raw_link.enc_type, "multipart/form-data")
99
+ end
100
+
101
+ # @return [true, false] True if encType of request is multipart/form-data
102
+ def content_type_json?
103
+ Rack::Mime.match?(@raw_link.enc_type, "application/json")
78
104
  end
79
105
 
80
106
  # @return [Hash] Example request parameters for this endpoint
@@ -138,6 +164,16 @@ module Jdoc
138
164
  ResponseGenerator.call(response_schema.properties)
139
165
  end
140
166
 
167
+ # @return [String, nil] Example request body in Multipart
168
+ def request_body_in_multipart
169
+ Request::Multipart.new(request_parameters).dump
170
+ end
171
+
172
+ # @return [String, nil] Example request body in JSON format
173
+ def request_body_in_json
174
+ JSON.pretty_generate(request_parameters)
175
+ end
176
+
141
177
  # @return [Fixnum] Order score, used to sort links by preferred method order
142
178
  def method_order_score
143
179
  case method
@@ -0,0 +1,43 @@
1
+ module Jdoc
2
+ module Request
3
+ class Multipart
4
+ MULTIPART_BOUNDARY = "---BoundaryX"
5
+
6
+ # @return [String] returns boundary parameter for multipart content-type
7
+ def self.boundary
8
+ "boundary=#{MULTIPART_BOUNDARY}"
9
+ end
10
+
11
+ # @param params [Hash] request parameters
12
+ def initialize(params)
13
+ @params = params
14
+ end
15
+
16
+ # @return [String] request body of multipart/form-data request.
17
+ # @example
18
+ # -----BoundaryX
19
+ # Content-Disposition: form-data; name="file"
20
+ #
21
+ # ... contents of file ...
22
+ # -----BoundaryX--
23
+ def dump
24
+ contents = Rack::Multipart::Generator.new(@params, false).dump.map do |name, content|
25
+ content_part(content, name)
26
+ end.join
27
+ "#{contents}\r--#{MULTIPART_BOUNDARY}--\r"
28
+ end
29
+
30
+ private
31
+
32
+ # return [String] content part of multipart/form-data request
33
+ def content_part(content, name)
34
+ <<-EOF
35
+ --#{MULTIPART_BOUNDARY}\r
36
+ Content-Disposition: form-data; name="#{name}"\r
37
+ \r
38
+ #{content}
39
+ EOF
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/jdoc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jdoc
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -40,6 +40,11 @@ definitions:
40
40
  items:
41
41
  type: integer
42
42
  example: 1
43
+ file:
44
+ description: an attachment of app
45
+ example: '... contents of file ...'
46
+ readOnly: false
47
+ type: string
43
48
  links:
44
49
  - description: Create a new app.
45
50
  href: "/apps"
@@ -78,6 +83,18 @@ definitions:
78
83
  type:
79
84
  - object
80
85
  title: Update
86
+ - description: Upload an attachment file for an app
87
+ href: "/apps/{(%23%2Fdefinitions%2Fapp%2Fdefinitions%2Fid)}/files"
88
+ method: POST
89
+ rel: create
90
+ encType: multipart/form-data
91
+ schema:
92
+ properties:
93
+ file:
94
+ "$ref": "#/definitions/app/definitions/file"
95
+ type:
96
+ - object
97
+ title: Create
81
98
  properties:
82
99
  id:
83
100
  "$ref": "#/definitions/app/definitions/id"
data/template.md.erb CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
  ```
30
30
  <%= link.method %> <%= link.path %><%= link.query_string %> HTTP/1.1
31
- <%= "Content-Type: application/json\n" if link.has_request_body? -%>
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? -%>
34
34
  ```
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.0
4
+ version: 0.2.1
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-07-01 00:00:00.000000000 Z
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: redcarpet
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -143,6 +157,7 @@ files:
143
157
  - lib/jdoc/generator.rb
144
158
  - lib/jdoc/link.rb
145
159
  - lib/jdoc/property.rb
160
+ - lib/jdoc/request/multipart.rb
146
161
  - lib/jdoc/resource.rb
147
162
  - lib/jdoc/schema.rb
148
163
  - lib/jdoc/version.rb