rack-json_schema 1.0.7 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +1 -1
- data/bin/specup +0 -1
- data/config.ru +13 -0
- data/lib/rack/json_schema.rb +1 -0
- data/lib/rack/json_schema/docs.rb +99 -10
- data/lib/rack/json_schema/version.rb +1 -1
- data/rack-json_schema.gemspec +3 -1
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b28c973a0a7e88414ff495f23a4d4d74da9c3024
|
4
|
+
data.tar.gz: 17c6b63a58ad58f66db57f43266890ac633f67c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afcd17fe0f1bef5a94cf54a43deb42d6ddbb4b4d5db371e68571153ed12a76b11a75c9f3701eff176f789a60604dce4a397effba8d7beeb1ba77c0e20833ba28
|
7
|
+
data.tar.gz: 8bb15acd2a2f91be390215cf9a1c495e3f0982583ed62bda3bda7f4927c0682caca02aea73aa16d3161d044f0fe54c1815e0dc48664af281e3a07ca84372e526
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -149,7 +149,7 @@ Rack::JsonSchema::Error
|
|
149
149
|
```
|
150
150
|
|
151
151
|
### Rack::JsonSchema::Docs
|
152
|
-
Returns API documentation as
|
152
|
+
Returns API documentation as text/html (GET /docs) or text/plain (GET /docs.md).
|
153
153
|
|
154
154
|
* You can give `path` option to change default path: `GET /docs`
|
155
155
|
* API documentation is powered by [jdoc](https://github.com/r7kamura/jdoc) gem
|
data/bin/specup
CHANGED
data/config.ru
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "rack/json_schema"
|
2
|
+
|
3
|
+
path = File.expand_path("../spec/fixtures/schema.json", __FILE__)
|
4
|
+
str = File.read(path)
|
5
|
+
schema = JSON.parse(str)
|
6
|
+
|
7
|
+
use Rack::JsonSchema::Docs, schema: schema
|
8
|
+
use Rack::JsonSchema::SchemaProvider, schema: schema
|
9
|
+
use Rack::JsonSchema::ErrorHandler
|
10
|
+
use Rack::JsonSchema::RequestValidation, schema: schema
|
11
|
+
use Rack::JsonSchema::ResponseValidation, schema: schema
|
12
|
+
use Rack::JsonSchema::Mock, schema: schema
|
13
|
+
run ->(env) { [404, {}, ["Not found"]] }
|
data/lib/rack/json_schema.rb
CHANGED
@@ -10,21 +10,20 @@ module Rack
|
|
10
10
|
def initialize(app, path: nil, schema: nil)
|
11
11
|
@app = app
|
12
12
|
@path = path
|
13
|
-
@
|
13
|
+
@markdown = Jdoc::Generator.call(schema)
|
14
|
+
@html = Jdoc::Generator.call(schema, html: true)
|
14
15
|
end
|
15
16
|
|
16
17
|
# Returns rendered document for document request
|
17
18
|
# @param env [Hash] Rack env
|
18
19
|
def call(env)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
@app.call(env)
|
27
|
-
end
|
20
|
+
DocumentGenerator.call(
|
21
|
+
app: @app,
|
22
|
+
env: env,
|
23
|
+
html: @html,
|
24
|
+
markdown: @markdown,
|
25
|
+
path: path,
|
26
|
+
)
|
28
27
|
end
|
29
28
|
|
30
29
|
private
|
@@ -33,6 +32,96 @@ module Rack
|
|
33
32
|
def path
|
34
33
|
@path || DEFAULT_PATH
|
35
34
|
end
|
35
|
+
|
36
|
+
class DocumentGenerator
|
37
|
+
def self.call(*args)
|
38
|
+
new(*args).call
|
39
|
+
end
|
40
|
+
|
41
|
+
# @param app [Object] Rack application
|
42
|
+
# @param env [Hash] Rack env
|
43
|
+
# @param html [String] HTML rendered docs
|
44
|
+
# @param markdown [String] Markdown rendered docs
|
45
|
+
# @param path [String] Route for docs
|
46
|
+
def initialize(app: nil, env: nil, html: nil, markdown: nil, path: nil)
|
47
|
+
@app = app
|
48
|
+
@env = env
|
49
|
+
@html = html
|
50
|
+
@markdown = markdown
|
51
|
+
@path = path
|
52
|
+
end
|
53
|
+
|
54
|
+
# Generates suited response body from given env & document to docs request
|
55
|
+
# @return [Array] Rack response
|
56
|
+
def call
|
57
|
+
if has_docs_request?
|
58
|
+
if has_markdown_request?
|
59
|
+
markdown_response
|
60
|
+
else
|
61
|
+
html_response
|
62
|
+
end
|
63
|
+
else
|
64
|
+
delegate
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Delegates request to given rack app
|
71
|
+
def delegate
|
72
|
+
@app.call(@env)
|
73
|
+
end
|
74
|
+
|
75
|
+
# @return [true, false] True if docs are requested
|
76
|
+
def has_docs_request?
|
77
|
+
request_method == "GET" && path_without_extname == @path
|
78
|
+
end
|
79
|
+
|
80
|
+
# @return [true, false] True if raw markdown content are requested
|
81
|
+
def has_markdown_request?
|
82
|
+
extname == ".md"
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return [String] Extension name of request path
|
86
|
+
# @example
|
87
|
+
# extname #=> ".md"
|
88
|
+
def extname
|
89
|
+
::File.extname(path)
|
90
|
+
end
|
91
|
+
|
92
|
+
# @return [String] Request path
|
93
|
+
def path
|
94
|
+
@env["PATH_INFO"]
|
95
|
+
end
|
96
|
+
|
97
|
+
# @return [String]
|
98
|
+
def request_method
|
99
|
+
@env["REQUEST_METHOD"]
|
100
|
+
end
|
101
|
+
|
102
|
+
# @return [String]
|
103
|
+
def path_without_extname
|
104
|
+
path.gsub(/\..+\z/, "")
|
105
|
+
end
|
106
|
+
|
107
|
+
# @return [Array] Rack response of raw markdown text
|
108
|
+
def markdown_response
|
109
|
+
[
|
110
|
+
200,
|
111
|
+
{ "Content-Type" => "text/plain; charset=utf-8" },
|
112
|
+
[@markdown],
|
113
|
+
]
|
114
|
+
end
|
115
|
+
|
116
|
+
# @return [Array] Rack response of human readable HTML document, rendered from given document
|
117
|
+
def html_response
|
118
|
+
[
|
119
|
+
200,
|
120
|
+
{ "Content-Type" => "text/html; charset=utf-8" },
|
121
|
+
[@html],
|
122
|
+
]
|
123
|
+
end
|
124
|
+
end
|
36
125
|
end
|
37
126
|
end
|
38
127
|
end
|
data/rack-json_schema.gemspec
CHANGED
@@ -17,7 +17,8 @@ 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 "
|
20
|
+
spec.add_dependency "erubis"
|
21
|
+
spec.add_dependency "jdoc", ">= 0.1.4"
|
21
22
|
spec.add_dependency "json_schema"
|
22
23
|
spec.add_dependency "rack"
|
23
24
|
spec.add_development_dependency "bundler", "~> 1.5"
|
@@ -27,4 +28,5 @@ Gem::Specification.new do |spec|
|
|
27
28
|
spec.add_development_dependency "rspec", "2.14.1"
|
28
29
|
spec.add_development_dependency "rspec-console"
|
29
30
|
spec.add_development_dependency "rspec-json_matcher"
|
31
|
+
spec.add_development_dependency "shotgun"
|
30
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
@@ -10,20 +10,34 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2014-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: erubis
|
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: jdoc
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - ">="
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
33
|
+
version: 0.1.4
|
20
34
|
type: :runtime
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
40
|
+
version: 0.1.4
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: json_schema
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -150,6 +164,20 @@ dependencies:
|
|
150
164
|
- - ">="
|
151
165
|
- !ruby/object:Gem::Version
|
152
166
|
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: shotgun
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
153
181
|
description:
|
154
182
|
email:
|
155
183
|
- r7kamura@gmail.com
|
@@ -165,6 +193,7 @@ files:
|
|
165
193
|
- README.md
|
166
194
|
- Rakefile
|
167
195
|
- bin/specup
|
196
|
+
- config.ru
|
168
197
|
- lib/rack-json_schema.rb
|
169
198
|
- lib/rack/json_schema.rb
|
170
199
|
- lib/rack/json_schema/base_request_handler.rb
|